NodeMCU and Lua coroutines
Posted: Fri Jan 06, 2017 11:54 pm
Hi All
My question lies on the borderline between esp8266-specific and Lua, so I hope I'm not too off-topic. Please feel free to point me in the right direction if I am.
I am using my nodemcu to perform some external activities, say to switch LEDs on and of, all carried out by a loop of Lua code. At the same time I have a web-server running, as per the omnipresent examples.
The problem is the following: The webserver seems to listen ONLY when the Lua code loop is finished. Otherwise it is not responsive, although soon as the loop performing the LED activity is finished, it then comes back online.
I would like to have a very long (possibly infinite) loop active, in parallel with the web-server. Incoming commands will then influence the behaviour of the of the device. For this, I believe that the best thing to do is to use Lua coroutines, which I have tried, but so far have not had much luck, even with really elementary examples.
Is there some tutorial that runs through the basics of this that I could follow? Perhaps with some examples?
In particular, how would you wrap the server in a routine, since it already seems to be running in the background at some level?
I am happy to put what I have tried on here, if there is anyone who is willing to look at it, but for the moment let's just say that I have tried peppering my "activity loop" with lots of coroutine.yield()s, and then try to resume them at various stages. I find the the coroutine often goes "dead" long before its activity is finished, typically in under 20 calls to coroutine.resume(). I use quite a few tmr.delay() statements I wonder if they get in each other's way.
Basically, I not really familiar with the collaborative multitasking model, even less so when it's combined with web programming, and probably totally clueless when the embedded dimension is added. So any information at all would be more than welcome.
Cheers!
My question lies on the borderline between esp8266-specific and Lua, so I hope I'm not too off-topic. Please feel free to point me in the right direction if I am.
I am using my nodemcu to perform some external activities, say to switch LEDs on and of, all carried out by a loop of Lua code. At the same time I have a web-server running, as per the omnipresent examples.
The problem is the following: The webserver seems to listen ONLY when the Lua code loop is finished. Otherwise it is not responsive, although soon as the loop performing the LED activity is finished, it then comes back online.
I would like to have a very long (possibly infinite) loop active, in parallel with the web-server. Incoming commands will then influence the behaviour of the of the device. For this, I believe that the best thing to do is to use Lua coroutines, which I have tried, but so far have not had much luck, even with really elementary examples.
Is there some tutorial that runs through the basics of this that I could follow? Perhaps with some examples?
In particular, how would you wrap the server in a routine, since it already seems to be running in the background at some level?
I am happy to put what I have tried on here, if there is anyone who is willing to look at it, but for the moment let's just say that I have tried peppering my "activity loop" with lots of coroutine.yield()s, and then try to resume them at various stages. I find the the coroutine often goes "dead" long before its activity is finished, typically in under 20 calls to coroutine.resume(). I use quite a few tmr.delay() statements I wonder if they get in each other's way.
Basically, I not really familiar with the collaborative multitasking model, even less so when it's combined with web programming, and probably totally clueless when the embedded dimension is added. So any information at all would be more than welcome.
Cheers!