The following example contains a C program for ESP32 which utilizes FreeRTOS and an interrupt, two tasks to test system resources like CPU information, and reacts to 'gp', 'tm' and 'am' commands through serial port. The thermistor and mic are connected to pins 33 and 34 respectively. In the Python script, replace '/dev/ttyUSB0' with 'COM3' if you are on a Windows system.
Note: The calculations depend on the specific model and the B value of the thermistor. Be sure to adjust it according to your specific thermistor.
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include <esp_log.h>
#include <driver/uart.h>
#include <string.h>
#define THERMISTORNOMINAL 100000
#define TEMPERATURENOMINAL 25
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000
void app_main(void)
{
/* Initialization of UART, ADC, and interrupts should be done here */
esp_adc_cal_characteristics_t *adc_characteristics =
(esp_adc_cal_characteristics_t*)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12,
DEFAULT_VREF, adc_characteristics);
char d[10];
/* Create the tasks */
xTaskCreate(serialInputTask, "serialInput", 8000, NULL, 1, NULL);
xTaskCreate(temperatureTask, "temperature", 8000, adc_characteristics, 1, NULL);
}
void serialInputTask(void *parameters)
{
char command[BUFFER_SIZE];
while (1)
{
/* Read and process commands from serial */
int num_bytes = uart_read_bytes(UART_NUM_1, (uint8_t*)command,
BUFFER_SIZE, 100 / portTICK_RATE_MS);
/* Implement commands 'gp', 'tm 0', 'tm 1', 'am 0', 'am 1' */
/* 'gp' shows basic system info */
if (strcmp(command, "gp") == 0)
{
printf("ESP32 Chip model: %s\n", CHIP_MODEL);
printf("Core count: %d\n", xPortGetCoreID());
printf("Flash chip size: %d\n", spi_flash_get_chip_size());
printf("Free heap size: %d\n", esp_get_free_heap_size());
printf("Free internal heap size: %d\n", esp_get_minimum_free_heap_size());
}
}
}
void temperatureTask(void *parameters)
{
esp_adc_cal_characteristics_t *adc_chars = (esp_adc_cal_characteristics_t*)parameters;
while(1)
{
uint32_t adc_reading = adc1_get_raw(ADC1_CHANNEL_5);
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
float resistance = (float)(1023-voltage) * 10000 / voltage;
float temperature = 1.0 / (log(resistance/10000.0) / BCOEFFICIENT + 1/298.15) - 273.15;
printf("Temperature: %.02f\n", temperature);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
For the Python script, we can use the built-in module 'serial'. Here is a simple, but effective, terminal chat.
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600)
while True:
user_input = input('> ')
ser.write(user_input.encode())
time.sleep(2)
while ser.in_waiting:
print(ser.readline().decode('utf-8'), end='')
Note that this python code is expected to be run in a python environment that supports the 'input' function, such as a terminal or command prompt.
Modify according to your use case and circuit configuration. Please ensure that the correct machines are being used, respect the UART interfaces, and adjust the B value and analog pins according to the specific component being utilized.