Building the right ESP32 based application

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Building the right ESP32 based application

Postby mcmega » Sat Dec 26, 2020 11:27 am

Hello everyone!
I would like to get some tips on how to create a complete firmware for ESP32.
It is the general concept and used approaches to programming using ESP-IDF.

There is a lot of documentation and examples too, but they do not show the whole concept of building a correct application.
Just blinking an LED or some other small task is not for such microcontroller resources.
I think this thread will be very relevant for all developers who plan to make good applications based on ESP32.

Tasks:
1. WiFi - full automation and logic for switching between modes, handling all dangerous events that affect the performance of the application.
2. File system - based on LittleFS for storage and settings and other information.
3. WebServer - designed for easy initial configuration of the device.
4. WebClient (HTTPS) - connections to external resources to receive updates (possibly to check for Internet availability)
5. UDPServer + UDPClient - designed to search for similar devices in the local network
6. Socket - to connect to a cloud resource for data exchange and backup settings at the time of firmware update
7. UART - an interface for data exchange with the STM32 microcontroller
8. Updating the STM32 firmware, as well as the WiFi module itself, the firmware is located on a remote server as BIN files.
9. Getting time from the Internet.

It turns out a lot of modules that need to be linked together and ensure good work for all tasks, as well as handle possible options for modules if disconnection from an access point or the Internet is disconnected.

I opened this topic due to the fact that many modules I have already done, but questions arise that force us to reconsider the whole approach of creating a reliable firmware.
For example, I ran into a UDP module (both server and client) problem. When sending a broadcast request, I want to receive responses for some time (because there can be many responses and all of them need to be processed) and exit the loop in order to send the request again. But now, if there is no answer, then I am forever in this cycle and there is no way to send a request. I also found a memory leak in the response loop when using cJSON in this loop. Functionally everything is done correctly, but the memory is not freed. To exit the loop, it was suggested to use select (), but there is very little information on it and there is no recommendation on the cases of its use.

Give a general concept of building an application for such a list of tasks.

excitedbox
Posts: 2
Joined: Sun Dec 27, 2020 8:15 pm

Re: Building the right ESP32 based application

Postby excitedbox » Sun Dec 27, 2020 8:35 pm

I thought little FS is depreciated?

Anyway you could create each of these as a module and an event listener in a main loop.

The main loop runs repeatedly and checks the value of a common variable (action_performed=) Your modules should set that variable to some value that indicates which module has responded (this can be done with a switch for simplicity).

Your main loop can call your modules (functions such as connect wifi) and they will perform an action and store data into your data structure (connection type, IP, etc). Then they set your listener variable (in this case action_performed=wifi connected) to let the main loop know how to continue. Then your main loop can use a switch to execute the next module (ie send data, check connection) which then responds with data sent or connection status information.

This structure means your main loop is never blocked by a loop because everything is performed as a set of actions which tell the main loop that they are done. Each time the main loop checks the action_performed variable it will know what module was just executed.

Obviously this is a very basic example because I am not going to write your entire program for you. This should help you on your way though with writing a non blocking event listener. There are better ways to send return values but without spending a ton of time studying your specific case it is just not possible to say what will be best for you.

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Re: Building the right ESP32 based application

Postby mcmega » Mon Dec 28, 2020 5:20 am

I repeat, I've already done almost all the modules. I used ready-made examples and created a task in FreeRTOS for each module.
I just wanted to know if there are any other approaches to writing applications. This post is not specifically about my requirements, but as a general approach to writing a complex type application.

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: Building the right ESP32 based application

Postby Victoria Nope » Mon Dec 28, 2020 7:58 am

mcmega wrote:
Mon Dec 28, 2020 5:20 am
I repeat, I've already done almost all the modules. I used ready-made examples and created a task in FreeRTOS for each module.
I just wanted to know if there are any other approaches to writing applications. This post is not specifically about my requirements, but as a general approach to writing a complex type application.

What more complexity than implementing FreeRTOS tasks you want to gain? Modern OSs offer the same pattern for even complex applications; they create threads and switch between their contexts on CPU cores that are available.

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Re: Building the right ESP32 based application

Postby mcmega » Wed Dec 30, 2020 6:02 am

Tell me how to work with UDP Client correctly?
I want it to stop if it works like AP or APSTA. Delete the task FreeRTOS at night and then re-create it, or what's the right way?

I am also interested in the operation of WebSocket, I cannot stop it (esp_websocket_client_destroy (client)) through the WEBSOCKET_EVENT_DISCONNECTED event handler. How to properly manage this component?

Who is online

Users browsing this forum: No registered users and 433 guests