Home Cory{dot}tech Logo

Complete • Feb 2024 – Mar 2024

C

ESP32

FreeRTOS

UDP

Temperature and Humidity Sensor on GitHub →

Project History and Development

Reading from a sensor is a fairly straightforward embedded project, but I wanted to take this project in more of a fullstack/IoT direction. With the requirement to have WiFi connectivity, I decided to use the ESP32-S3 DevkitC-1. This is a fairly powerful microcontroller, with a 32-bit dual-core Xtensa processor and built-in 802.11 b/g/n WiFi module. For the sensor, I used the AHT20 I2C temperature/humidity sensor.

The code was written in C using the ESP-IDF framework, which includes FreeRTOS. For those of you familiar with FreeRTOS but not with ESP-IDF, you may notice a few differences, as the ESP32 uses a slightly modified version of FreeRTOS. 

There are two tasks—one reads from the sensor and stores the reading in a queue, and the other retrieves data from the queue, encodes it using CBOR, and sends it to a server through a UDP socket. I considered using a JSON encoding and sending the data to a REST API endpoint directly, but sending CBOR over UDP resulted in code that was both simpler and more lightweight.

Future Plans

As it currently is, this project is designed primarily to work with one sensor. I would like to expand it to be more flexible, and to work with multiple sensors to gather additional environmental data, such as air pressure and quality.

Another improvement would be to add local storage using an SD card. The main motivation for this is to add resiliency in the event that the WiFi connection is lost. Once reconnected, then data saved on the SD card that has not yet been uploaded can be sent to the server.

Adding a small display would help to provide a real-time data visualization interface.

Specific to the code, I plan to look into using the FreeRTOS + TCP library to handle the UDP socket connection, rather than the base socket.h library.