Multitasking for single-core microcontrollers

Hi,
In my thermostat project based on ESP8266 I need to be able to run a simple web server to serve the user form (providing temperature and hysteresis information) and in a separate thread run a temperature control loop that drives the relay to which I connect the heating device. It is expected to work uninterruptedly for weeks.

Unfortunately, Arduino IDE does not seem to support multithreading as most of the 16 bit microcontrollers are single-core.

I have succesfully created this project in micropython with uasyncio (to emulate multitasking) and picoweb (GitHub - pfalcon/picoweb: Really minimal web application framework for the Pycopy project (minimalist Python dialect) and its "uasyncio" async framework) but it is unstable on ESP8266 due to high footprint of the uasyncio - as a result there are intermittent heap overwritings occurring that crash the program. One of the options is going to ESP32 instead but I really like the ESP8266 and would like to give it a try so I'd like to have it done in C++.

What options do I have? I'm not interested in nodemcu projects, I want specifically C/C++.

Why does it need to be multithreaded? Does it matter if the thermostat turns the AC off at the exact instant the temperature measures above the setpoint? Or could it go maybe a second or two? Really a few milliseconds should be all you need to wrap up your web serving code and get back around to checking temperature. Just make sure you aren't blocking anywhere.

Thanks for replying. Time is not critical at all, the temperature changes are slow, measured in minutes...

However, with single-threaded design I get stuck with the web server that waits for the http request in an infinite loop - therefore the program cannot take care for the temperature control procedure.
Or I am in fundamental mind trap :slight_smile:

Don't wait in an infinite loop then. Or wait in a loop that also checks the temperature and handles the thermostat.

Remember, you have the loop function repeating over and over. That keeps looping. So you can let that be the loop you're using instead of setting up some while loop blocking for the http request.

Taking the example from this webpage: Create ESP8266 Web Server - Arduino Project Hub

I assume now (do not have the device now to try out) that the void handleClient() function from ESP8266WebServer.h is non-blocking and callsback the the handle_index() subfunction when the client connects, otherwise exits and allows the next iteration of void loop() function - if that is correct them I am sadly a moron to assume it was a blocking function and not really try it......

1 Like

Those example codes are there to show you how things work. They are often not very useful for adding to some other project. Instead of copying and pasting from the example, you should study how it works and what it uses and then apply that knowledge to your program. Your program may not look anything like the example.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

1 Like