Post your best Lua script examples here

User avatar
By gwizz
#6409 Hiya everybody

I've got a classic use-case here, one that I imagine almost everybody using these chips will understand and probably want to solve elegantly somehow. It's the initial configuration problem - how do you get a wifi ssid and password onto a device that you don't necessarily want to equip with both a keyboard and a screen?

I've seen a number of approaches over the years from the discovery via a custom protocol to the more modern QR code on the device to the downright strange (but cool) optical handshaking of the electric imp.

But leveraging the power of the wifi chip to host a little configuration web page seems like the way to go for me, so let me outline my user story. I open the box of my lovely new sensor for my home aquaponics lab, and I insert the batteries. Now the device is primed for configuration, and it broadcasts an Access Point called 'Configure My Sensor-xx-xx' (last bit of MAC address).

If you connect your laptop, phone etc to this access point, you can visit a webpage that displays a little list of access points that have been found in the area recently. You can choose one, type in a password and click 'Connect'. After testing for connectivity, the webpage says 'Success' and then shuts down the AP.

So I need a way to get minimal functionality onto this webpage - passing data back and forth to get the scan results and set the password etc.

Looking at various web server projects, I am impressed and very happy to be participating with people sharing their code - but I'm having trouble knowing which code to try. I guess that many of the fine features people are struggling to implement are redundant in my use-case. Instead, a more pressing concern might be available memory.

Can anyone help me choose which project code to evaluate first? (I'm working in nodemcu lua in case it isn't obvious btw).

Many thanks in advance
User avatar
By lightbulb
#6445 gwizz,

don't know about other people, but here's what I do (I have only had my modules for about 5 days....):

I have different "sets" of lua and/or firmware for each task my modules will perform. In the case of nodemcu, its sets of lua files + specific lua firmware with various modules enabled/disabled.

1. I take a module, plug it into my "programmer", jumper my programmers gpio0+ch_pd (linux with FTDI + scripts) and burn base firmware.
2. unjumper, then load my lua "set" via scripts, in reverse order, init.lua last.

Now, my lua "sets" have a generic pattern, depending upon use-case they either startup in AP or station. If AP they run a telnetd and wait for a connection to set their new SSID/Pwd (I could use httpd and use GET params but dont see point. Telnetd sets flag after getting cmd (file) and restarts. Init check wifi connects and now starts httpd that does real work.
If I know target SSID I skip the init switching fuss and go straight for station with httpd if ip valid.

Why, because I always need to burm firmware anyhow, and at that point I know 95% time, the target SSID/pwd details. For other 5% the telnetd works after a quick scan in the target location.

If I were producing for generic "user", i guess I would build in the AP/Station switching into the httpd, but memory is big issue, so httpd is kept as small as poss, keeping all other less used code on the flash file system rather than on heap.
User avatar
By gwizz
#6456 Thanks for the reply - but I think you have answered a slightly different question.

I can see that for your needs your workflow is fine, and you are comfortable with programing firmware etc. as am I.

But - I am interested in designing devices that may be replicated by others even less skilled than I am.

Certainly I hope that the audience for my devices can be larger than our own lovely community of hackers.

So I (and my suggestion is that many or even most of us) need a way to ship devices with their final code on-board - that can be configured by end-users.

But perhaps I am wrong - is no-one else thinking of making cool stuff with these chips for non-hackers to use?
User avatar
By lightbulb
#6557 gwizz,

pardon me - let me be a little more specific to your use-case ;)

If you had a little more ram and a more featureful ip stack, you may look at something like upnp/zeroconf but i dont think you will have the horsepower/ram to implement.
I have not looked at IP stack in depth (looks like its based upon contiki ...), so you missing a few bits probably for going that way.

You may need to implement this via something like COAP, but you will need low level support that lua can call upon i feel.

Of course, any approach using internet falls into catch 22 if relying on wifi with key with no local app support.

I don't want to produce "shipping" products, but here is what I am toying with until I (or someone else) looks at coap:
(NB: my family run a property company where these module offer important benefits for us, but we dont need to "sell" products based upon them...)
1. Module boots into AP with UDP server that listens on port.
2. Small App that runs on Linux/OSX/Windows (once client connected to AP). Sends packet to AP with UDP server running, returns visible networks.
3. App asks user for password of selected Wifi and sends to module (or provide hidden SSID+password).
4. Module logs in, restarts.
You could use a GPIO with button to bring the module into AP/UDP mode to repeat.

As for "shipping" [products] based upon this module, please check your FCC compliance...;)

If I make progress I will post an update for you.

Kind Regards