-->
Page 1 of 1

NodeMCU and Lua coroutines

PostPosted: Fri Jan 06, 2017 11:54 pm
by alex_g
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!

Re: NodeMCU and Lua coroutines

PostPosted: Sun Jan 08, 2017 10:20 pm
by alex_g
From the unoficial FAQ:
The ESP8266 is an IoT device and not a general purpose system. [...] We commonly come across developers who are trying to treat the ESP8266 as a general purpose device and can't understand why their application can't run.

Ok, fair enough, but where's the fun in that? :P

Seriously though, a lot of my questions have been answered reading the said FAQ, great work, whoever put it together. I still need to sort out a lot of details, but at least I have a direction to follow. Daniel Casner's short guideline doc was helpful, too - it gave me an idea of the parameters I was facing at least, and why I had to put 1μs delays in all my big loops to stop them crashing...

More specifically, it says in the FAQ:
You therefore have to implement ESP8266 Lua applications using an event driven approach. You have to understand which SDK API requests schedule asynchronous processing, and which define event actions through Lua callbacks.


Is there a quick way to understand which is which, short of delving into the source? Some example programs, perhaps? At leas an indication of how the wifi server connections are implemented?

Any suggestions most welcome...

Re: NodeMCU and Lua coroutines

PostPosted: Mon Jan 09, 2017 3:14 am
by alex_g
Hi again!

While on the subject of documentation for Lua event-driven programming on NodeMCU, I wonder if the mods here have noticed that some of the most critical information on the subject, the four pages or so of the "Unofficial FAQ", between
"So how does the SDK event / tasking system work in Lua?" and
"When and why should I avoid using tmr.delay()?" are misformatted. I was going squint deciphering that lot line-by-line before I noticed that it also exists (properly formatted) on https://nodemcu.readthedocs.io/en/maste ... ork-in-lua. Phew, that was a relief, but perhaps someone would care to straighten out here, too.

Musing further on the subject I can see that while Lua will be GREAT for rapid prototyping, and for learning the basic ins and out of where everything is, once the serious messing around starts, the road to C seems inevitable. So I just wanted to ask, how do you folk program the ESP8266 in C? Do you use the Arduino IDE? I have never used Arduino for anything, but the the IDE sounds good. Are there any other IDEs, comilers or toolchains that are of interest here? Many thanks!

Re: NodeMCU and Lua coroutines

PostPosted: Wed Mar 08, 2017 10:55 am
by cwr
I've use Arduino stuff quite a bit, though I avoid the Arduino IDE. However, the Arduino libraries are useable but rough, so care is needed.

The AVR's API is very different from that of the ESP 8266; I'd avoid using code meant for an AVR on an ESP. NodeMCU is actually pretty reliable, provided you accept that it doesn't leave much space for your code. Beyond that, I'd use C and the SDK directly.

Will