ESP-IDF support for Water level monitor project

23/06/2021

The ESP32 microcontroller can be programmed in various development environments. Arduino IDE is one of the most popular, mainly due to easy program implementation even for beginners. However, there is also the ESP-IDF framework from Espressif Systems, which manufactures these microcontrollers. It allows closer access to the physical layer of the microcontroller, has various development tools via Python scripts and implemented tools for running system functionalities.

Framework ESP-IDF contains various sample implementations of projects for bus service, communication ..., which use FreeRTOS - a real-time operating system that greatly simplifies project development, as it allows you to run subroutines as processes that are associated with the kernel that handles it. The advantage compared to programming in the Arduino IDE is the fact that the kernel can operate several functions at the same time, so in standard programming, for example, when using the delay() function, the entire program is stopped.

In this case, only the process is stopped for a given time, while other processes can continue to work. Each task has an assigned stack, priority with the option to select the ESP32 kernel on which the task will be performed. I used FreeRTOS for the last year to create a demonstration application of a sensor node on ESP32 in my Diploma Thesis. I decided to create a sample implementation into the Hladinomer project, which uses ESP32, but the program was created only in the Arduino IDE environment. When creating the application, I focused and set myself the goal of creating a total of 2 tasks, using inter-task communication using the queue - Queue.

Using a Queue allows one of the tasks to wait to receive a value sent to it by another task (or more tasks, as can be seen from the image above). In my case, one task will operate the ultrasonic distance sensor and perform measurements, and the other task will wait in a cycle for the value from the measurement, which it will then send to the server, sample HTTP POST method. In practice, 

Queues are also used to lock access to the bus (lock is also called MUTEX), a peripheral that can be used in real time only by the active Queue, other tasks are in "block" mode, as they wait for a value from the active Queue that allows them execute a program block while the bus or peripheral is free. When creating the program, I first created a test "offline" program, which allows only reading the measured value from the ultrasonic sensors HC-SR04, or waterproof version JSN-SR04T and write it to the UART monitor. 

I have found library for these sensors at Github, implementation consist of header file and library file that is linked from same directory (it is not created as ESP-IDF component). The second step was the implementation of the HTTP request, which is available in ESP-IDF as a demonstration project. The programmatic implementation was only available for the static path and the GET method. For this reason, I had to change the existing HTTP request header, add encoding of data transmitted by the POST method, so that they are properly downloaded on the web server side.

The combination of both projects was simple, as both used FreeRTOS, so the function was run via this scheduler. So it was enough to copy the functions and one line of initialization of the task into the .c program. The first test program involved sending data to a web server every 5 minutes and measuring the data every 5 seconds, with the data being written to a global variable - data. the uint32 type to which both tasks had access. The task for the HTTP request also included the initialization of the WiFi adapter together with the possibility of configuring the SSID and password via Menuconfig.

After connecting to the WiFi network, ESP32 obtains, in addition to IPv4, also an IPv6 link-local address. Subsequently, I implemented a simple Queue according to the pattern for Inter-task communication: https://icircuit.net/esp32-inter-task-communication-using-freertos-queues/1946 

In this case, the producer task was a measurement task and the consumer task was a sending task. The HTTP request task thus did not use vTaskDelay, but cyclic verification of the arrival of data from the second task, which is operated by the ultrasonic distance sensor. The sending job sends the data to the server immediately after receiving the data. Waits for data up to 300 seconds. 

Try the Water Level project for free with your hardware:
https://arduino.clanweb.eu/studna_s_prekladom/?lang=en
with your hardware for implementation in the Arduino IDE environment, or ESP-IDF for the ESP32 microcontroller. Implementations are also available on Github in the project repository, where you can find extended version of Water Level project with Hydreon RG-11 rainfall meter:
https://github.com/martinius96/hladinomer-studna-scripty

© 2021 Martin Chlebovec - Arduino Blog - . All rights reserved
Powered by Webnode Cookies
Create your website for free! This website was made with Webnode. Create your own for free today! Get started