#include <Semaphore.h>
#include <TaskScheduler.h>
TaskScheduler scheduler;
SemaphoreHandle_t tempSemaphore = xSemaphoreCreateMutex();
SemaphoreHandle_t micSemaphore = xSemaphoreCreateMutex();
const int TEMPERATURE_PIN = 32;
const int MIC_PIN = 34;
void printSystemInfo() {
Serial.printf("Core count: %d\n",资源配置::getCoreCount());
Serial.printf("Clock speed: %d MHz\n", ESP.getCpuFreqMHz());
Serial.printf("Free heap: %d bytes\n", ESP.getFreeHeap());
}
void temperatureTask(void *pvParameters) {
while (true) {
if (Serial.available()) {
char cmd = Serial.read();
if (cmd == 'tm') {
int arg = Serial.parseInt();
if (arg == 1) {
xSemaphoreTake(tempSemaphore, portMAX_DELAY);
int temp = readTemperature(TEMPERATURE_PIN);
Serial.println(temp);
xSemaphoreGive(tempSemaphore);
vTaskDelay(pdMS_TO_TICKS(1000));
} else if (arg == 0) {
// Do nothing, temperature print stopped
}
}
}
}
}
void micTask(void *pvParameters) {
while (true) {
if (Serial.available()) {
char cmd = Serial.read();
if (cmd == 'am') {
int arg = Serial.parseInt();
if (arg == 1) {
xSemaphoreTake(micSemaphore, portMAX_DELAY);
int micValue = readMicLevel(MIC_PIN);
Serial.println(micValue);
xSemaphoreGive(micSemaphore);
vTaskDelay(pdMS_TO_TICKS(1000));
} else if (arg == 0) {
// Do nothing, mic level print stopped
}
}
}
}
}
int readTemperature(int pin) {
int result;
// Add code to read temperature from thermistor
return result;
}
int readMicLevel(int pin) {
int result;
// Add code to read analog value from electret mic
return result;
}
void setup() {
Serial.begin(115200);
scheduler.addTask(temperatureTask);
scheduler.addTask(micTask);
scheduler.start();
printSystemInfo();
}
void loop() {
// Managed by TaskScheduler
}
For the Python script, you can use the serial library to send commands to the ESP-WROOM-32:
import serial
import time
ser = serial.Serial('COM3', 115200)
def send_command(command, arg=None):
ser.write((command + ' ' + str(arg) + '\n').encode())
def get_gp():
send_command('gp')
response = ser.readline().decode().strip()
return response
def start_tempMonitoring():
send_command('tm', 1)
def stop_tempMonitoring():
send_command('tm', 0)
def start_micMonitoring():
send_command('am', 1)
def stop_micMonitoring():
send_command('am', 0)
# Example usage
print("System Info:", get_gp())
start_tempMonitoring()
time.sleep(5)
stop_tempMonitoring()
start_micMonitoring()
time.sleep(5)
stop_micMonitoring()
ser.close()
Note: This code assumes you've connected a thermistor to an ADC-compatible pin as TEMPERATURE_PIN and an electret mic to an analog input pin as MIC_PIN. You will need to fill in the readTemperature() and readMicLevel() functions with proper ADC conversion code for your hardware setup.