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.
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
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.
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......
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.
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.