nodemcu-firmware
nodemcu-firmware copied to clipboard
exemple of init.lua with wifi portal
Fixes no specific issue,
- [x] This PR is for the
devbranch rather than for thereleasebranch. - [x] This PR is compliant with the other contributing guidelines as well (if not, please describe why).
- [x] I have thoroughly tested my contribution.
- [ ] The code changes are reflected in the documentation at
docs/*.
Following a suggestion in #3374 , this is an example for a init.lua that uses a wifi portal to configure wifi if needed. I put it inside lua_examples, but I don't know how and where to refer to this in the docs. Suggestions?
Also, I wonder if it would be interesting to provide complete examples for some common IOT setups:
- robust mqtt to light up a LED remotely
- robust mqtt sensor reading (temperature) every 10 minutes, with deep sleep in between
- robust mqtt IOT identification using Homie
- ... ( Here robust means that it will survive power outage, network outage, etc, and always restart properly. ) Such examples could make it much simpler to setup IOT by providing a good template. Juste wondering if this is something that would be considered useful.
As a stylistic point, I'd make t a local and then you don't need to set it to nil in order to free it.
In my code, I always start the enduser_setup -- it exits once the connection is up -- no matter whether the user configured it, or whether a previously set SSID was found.
- [x] This PR is for the
devbranch rather than for thereleasebranch.
Now it is - I just changed the base branch.
Also, please fix the CI build issues: https://travis-ci.org/github/nodemcu/nodemcu-firmware/jobs/757193097
To answer, @pjsg, we can't make t a local since it must be possible to type t:stop() during the initial 10 seconds delay. Anything local will disappear as soon as init.lua is done executing, so t must stay global. This is why luacheck fails, even if this is the correct code, IMO. I don' t see any way around this.
Unfortunately, enduser_setup will always create a wifi access points even when wifi station is already configured. The acces point will be gone in less than 10 seconds, but still I consider this less than ideal. So I prefer to check wifi status before any call to enduser_setup. Maybe enduser_setup should check wifi before setting up the access point?
@blobule "I don't see any way around this" -- Use a tmr. ;-)
You can disable checks in luacheck by adding a comment. See the luacheck documentation