A simple ESP32 project that uses FreeRTOS to log temperature and humidity from a DHT11 sensor and display it on the Blynk IoT platform with live graphs.
A dedicated FreeRTOS task samples the sensor and pushes the latest reading into a length-1 FreeRTOS queue; the main task runs the Blynk client and uploads that reading on a timer. Keeping every Blynk call on a single task makes the cross-task hand-off thread-safe.
- ESP32 (38-pin) NodeMCU Development Board
- DHT11 Temperature & Humidity Sensor
- Jumper wires + breadboard
- Arduino IDE (or
arduino-cli) with:- the ESP32 board package (
esp32by Espressif) - the Blynk library
- the DHT sensor library (Adafruit) + its Adafruit Unified Sensor dependency
- the ESP32 board package (
| ESP32 GPIO Pin | Component |
|---|---|
| GPIO 4 | DHT11 Data |
| 3.3V | DHT11 VCC |
| GND | DHT11 GND |
The full sketch lives in RTOS-Weather-Logger.ino. The core of it: a FreeRTOS task reads the DHT11 (skipping failed NaN reads) and publishes to a length-1 queue, while a BlynkTimer on the main task uploads the freshest sample.
// FreeRTOS task: sample the DHT11 and publish the latest valid reading.
void sensorTask(void *pvParameters) {
for (;;) {
Reading r;
r.temperature = dht.readTemperature(); // Celsius
r.humidity = dht.readHumidity();
if (isnan(r.temperature) || isnan(r.humidity)) {
Serial.println("DHT read failed; skipping sample");
} else {
xQueueOverwrite(readingQueue, &r); // keep only the freshest sample
}
vTaskDelay(pdMS_TO_TICKS(SAMPLE_PERIOD_MS));
}
}
// Runs on the main (Blynk-safe) task: push the most recent sample.
void uploadReading() {
Reading r;
if (xQueuePeek(readingQueue, &r, 0) == pdTRUE) {
Blynk.virtualWrite(V5, r.temperature);
Blynk.virtualWrite(V6, r.humidity);
}
}
void loop() {
Blynk.run();
timer.run();
}-
Install the ESP32 board package in the Arduino IDE (Boards Manager β "esp32"), plus the Blynk and DHT sensor library packages (Library Manager).
-
Open
RTOS-Weather-Logger.inoand fill in yourBLYNK_TEMPLATE_ID,BLYNK_TEMPLATE_NAME,BLYNK_AUTH_TOKEN, Wi-Fissid, andpass. -
Select Tools β Board β ESP32 Dev Module, pick the serial port, and click Upload.
Or from the command line with
arduino-cli:arduino-cli compile --fqbn esp32:esp32:esp32 . arduino-cli upload --fqbn esp32:esp32:esp32 -p /dev/ttyUSB0 . arduino-cli monitor -p /dev/ttyUSB0 -c baudrate=115200
-
In the Blynk app, add a widget bound to V5 (temperature) and another to V6 (humidity).
π Click here to watch the demonstration on LinkedIn
This project is open-source and available under the MIT License.
β¨ Happy coding with ESP32 & FreeRTOS! π