-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does ESP8266 with Arduino IDE support multi-threading? #1870
Comments
ESP8266 is single-core, so you can't run stuff simultaneously anyways. |
Thanks for the advice. wondering if ESP8266 with Arduino IDE supports allocating processing resources for a function for specific time before it runs next function. // During the 5000ms allocation, processing resource should be available any advise is highly appreciated. |
I'm not entirely sure what you're asking. You could do timestamps by tracking time spent with millis() and then running one or another function when a certain amount of time has passed, or you could use timers to set a flag when a certain amount of time has passed and just check whether the flag is true or flag in loop and run your functions accordingly -- the latter solution makes for cleaner code, even if there is no practical difference. I might be able to offer more if you explained what you're trying to achieve. |
I guess you may be looking for a cooperative scheduler like this one prepared for Arduino sam and samd architectures. Out of curiosity I made a quick check and sure enough MultipleBlinks sketch does not compile for ESP. It is complaining a lot about incompatibility of boards and by providing several other reasons it will not make it 😄 I like s/w that is saying in plan text what the problem is 👍 Krzysztof |
@geoffreyhhh Hi, I recently found this article: https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc?utm_source=Hackster.io+newsletter&utm_campaign=9665148c8a-2015_4_17_Top_projects4_16_2015&utm_medium=email&utm_term=0_6ff81e3e5b-9665148c8a-140388981 - but have not tried it with ESP yet. |
Thanks for the inputs and sorry for the late reply. Below is the portion of the code relating to the question. it's a code for demo purpose, loop1() is to demo turning on/off esp8266 onboard led through web browser. ESP8266WebServer server(80); void loop(){ void loop1(void){ when there's no loop2() loop3() in loop(), and loop1() code is in loop(), I used millis() to measure the time required to complete the code, it gave between 3second ~ 5second, each time is slightly different. in this way, the web browser can get complete response message from the esp8266. when loop1() loop2() loop3() are in loop(), once web browser sends commands to esp8266, it's very hard to get complete response message from esp8266 to display in web browser the response message from esp8266. the web browser will get the response message after 2nd or 3rd running of the loop1() after the web browser sends out the command to the esp8266. that's why I was thinking to be able to allocate 5000ms to the loop1(), so that there's no need to wait for 2nd or 3rd running of the loop1() for the web browser to receive the response. I tried to use delay(), which is not right way to do so since delay() holds processing resource. Could you advise further to offer bit code example to illustrate your timer to resolve. Thank you in advance. |
This looks like the main issue here. Could you please post the minimal code sample which demonstrates the issue? Thanks. |
I have to say, I still don't understand your problem. Are your loop2() and loop3() terribly slow, then? |
As for cooperative scheduling, the ESP8266 Arduino code already has the coroutine functions, I wonder if they can be exposed somehow to the users. I would love to make use of them for some of my code instead of the state machines I need to code now. |
FYI: Github/Makuna/Task is a non-preemptive multitasking library support that cross Arduino compatible. |
While makuna/task does help a bit it isn't much different then having a set of loop functions and checking for triggers. A coroutine approach is much more convenient and while it is not portable I don't really care that much about portability anyway in my project. |
+1 on this topic; I have a lot of use cases for cooperative multitasking on the ESP |
its already done, unlike the AVR core, every modern core like ARM and extensa can prioritize its interrupts which is resulted to multi-threading task, but in old Arduino board which is base on ATMEGA core , many developer have to calculate precise timing sequence for multi tasking and in fact impossible because each event (interrupt) have nor priority over another event (interrupt) |
Take a look on https://github.com/anmaped/esp8266-scheduler it is a preemptive scheduler for esp8266 Arduino. I have to submit the changes to Arduino esp8266 to weak the definition of some functions. It is not stable, but for now it is working very well. @igrr Could I submit this pull resquest to weak the function 'esp_schedule', 'optimistic_yield', 'loop_task', and 'delay' ? |
Let me get back to this thread. Example:
Instead of just putting the LED on, I would like to do something that lasts forever - unless I stop it intentionally.
Well, do no just focus on blinking, it might be something more complex. However, the second routine blocks the webserver and clicking on the button at the html-page to trigger ledOff therefore does not show any result than waiting. How could I overcome that? Would Makuna/Task solve that problem? |
In such a case, I would just do something like:
The idea is that the loop lets the code "breathe", and the handlers just set indicator variables that can be tested in the main loop. Vicne |
And yes the makuna/task would handle this problem. It will take the example Vicne above to the next level and allow you to get rid of delay completely. For most Arduinos delay is blocking, so nothing else happens while you wait. If you are using the Async Html Server object for Esp8266, then this isn't a problem though. Some reference reading Adafruit Multitasking that will help you understand the concept. |
@ vicnevicne: @ Makuna: @ all: Could anybody recommend me which path to follow? |
Yeah, I omitted many things related to the server to make the case simpler.
|
Is this supported on esp? |
Any more discussions? |
@geoffreyhhh et al |
Thanks! Let me go there to take a look. |
@anmaped i got compile errors for nodemcu 1.0 |
Here you can find a full blown up tutorial for multitasking with ESP8266: Without WiFi: And a Demo with Wifi Have fun ;) |
Great Scott. |
Dears,
was wondering if ESP8266 with Arduino IDE supports multi-threading to run multiple tasks/functions simultaneously? If it does, please help to provide the working example code.
any advise is highly appreciated.
Geoffrey
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: