nodemcu-firmware icon indicating copy to clipboard operation
nodemcu-firmware copied to clipboard

exemple of init.lua with wifi portal

Open blobule opened this issue 4 years ago • 5 comments

Fixes no specific issue,

  • [x] This PR is for the dev branch rather than for the release branch.
  • [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.

blobule avatar Feb 02 '21 05:02 blobule

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.

pjsg avatar Feb 02 '21 13:02 pjsg

  • [x] This PR is for the dev branch rather than for the release branch.

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

marcelstoer avatar Feb 02 '21 15:02 marcelstoer

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 avatar Feb 03 '21 00:02 blobule

@blobule "I don't see any way around this" -- Use a tmr. ;-)

jmd13391 avatar Feb 03 '21 10:02 jmd13391

You can disable checks in luacheck by adding a comment. See the luacheck documentation

HHHartmann avatar Dec 28 '21 05:12 HHHartmann