Post your best Lua script examples here

User avatar
By fcattaneo
#59023 Dear ..

I'm would design a software that do these things ( in order ) :

1- wake up from deepslip without RF (called like this: node.dsleep (600 million, 4)
2- DHT22 sensor reading,
3- Turn on WiFi and connect to AP
4- Data sent via MQTT
5- goes back into deepslip. ( no rf, option ,4 )

I am having great difficulty for the step 3 ...
my software wake up properly without wifi (drain only 15 mA ..), then properly get data from DHT22, but i do not know how to turn on WiFi and connect to AP.

I'm looking for this type of design because I prefer to read DHT22 with no noise in power supply (normaly the WiFi features cause a lot of HiFreq ripple on line 3.3 volts.)

Can anyone help me and give an example of how you wake the wifi inside a lua script?

Thanks in advence,
Fabrizio.
User avatar
By glcos
#59038 I have found this workaround that seems to work to me:

Wifi is up and module connected to the router

Code: Select all> =wifi.sta.status()
5

Switch off WiFi before going to sleep

Code: Select all> wifi.setmode(wifi.NULLMODE)

Module goes into an undocumented status 255

Code: Select all> =wifi.sta.status()
255

I verified that the module disassociates from router

Going to deep-sleep for 1 second with no additional parameters

Code: Select all> node.dsleep(1000000)

Module goes to sleep and wakes up after 1 second.

Code: Select all> =wifi.sta.status()
255

WiFi is still off, I verified that module is still not connected to router

Setting WiFi mode to station

Code: Select all> wifi.setmode(wifi.STATION)
> =wifi.sta.status()
0

WiFi goes to the documented status of 0: STA_IDLE
I verified that module is not connected to router, so I expect that WiFi si still off

Connecting WiFi

Code: Select all> wifi.sta.connect()

WiFi switches on and module gets connected to router

Code: Select all> =wifi.sta.status()
5

Please notice that I made all my tests with an ESP12F module supplied with a soldered 3V3 regulator, therefore I could not test the actual current draw in mA.
My router was running OpenWrt latest release 15.05.1

Hope this helps
User avatar
By torntrousers
#59236 I've done similar in the past with the clunky approach of doing a deepSleep(1, RF_DEFAULT) to re-enable the radio and having flags and values saved in the RTC memory so it knows what to do when it wakes up. I've never found any way to programatically switch back on the radio after waking up from deepSleep with radio off, would be happy to be shown how to do that.