// Forum: https://forum.arduino.cc/t/vsnprintf-random-core-panic/1286637
// This Wokwi project: https://wokwi.com/projects/404875843203421185
//
// Note: This is not good code.
// FreeRTOS is a pre-emptive multitasking system.
// A change of the active task can happen during the
// writing of a variable.
// Turning off the interrupts in the loop() is not enough.
// A semaphore/mutex should be used.
//
// Enable or disable the WDEBUG
#define WDEBUG
// The maximum wind for 3.3V.
#define SENSOR_MAX 30.0
// Temperature / Windspeed value indicating a sensor failure.
// A value above 5000 is invalid.
#define INVALID (6000.0)
// ESP32 board pin where LM35DZ sensor is connected
const int temperaturePin = 33;
// SN-30000-FSJT-V05 windspeed sensor is connected
const int windspeedPin = 34;
// Global variables that are updated in the tasks.
volatile double windspeed = INVALID;
volatile double temperature = INVALID;
void setup()
{
Serial.begin(115200);
Serial.println("=======================================================");
xTaskCreate(windspeed_task , "Wind" , 8000, NULL, 1, NULL);
xTaskCreate(temperature_task, "Temperature", 8000, NULL, 1, NULL);
}
// The loop() is a FreeRTOS task,
// running at priority 1.
void loop()
{
// Turning off the interrupts is not enough for valid data.
noInterrupts();
double wCopy = windspeed;
double tCopy = temperature;
interrupts();
Serial.printf("LOOP windspeed = %.2lf, temperature = %.2lf\r\n", wCopy, tCopy);
delay(2000);
}
void windspeed_task(void *arg)
{
while(1)
{
int raw = analogRead(windspeedPin);
double v = (double) raw / 4096.0 * 3.3;
double s = v / 3.3 * SENSOR_MAX;
// update the global variable
windspeed = s;
#ifdef WDEBUG
Serial.printf("WIND windspeed = %.2lf (voltage = %.2lf)\r\n",s,v);
#endif
delay(5000);
}
}
void temperature_task(void *arg)
{
while(1)
{
int raw = analogRead(temperaturePin);
double v = (double)raw / 4096.0 * 3.3;
double t = v * 100.0; // 0.32v = 32 degrees Celsius
// update the global variable
temperature = t;
#ifdef WDEBUG
Serial.printf("TEMP temperature = %.2lf (voltage = %.2lf)\r\n",t,v);
// Anything above 35 degrees is suspicous but OK.
// Above 99 is steam.
if(t > 100.0)
Serial.printf("TEMP Sensor failure?\r\n");
#endif
delay(3000);
}
}
Temperature
Windspeed