nodemcu how to cut down the time required to acquire ip address - lua

With NodeMCU in station mode with the following snippet of code in init.lua it still takes on average about 6 iterations of the loop before an IP address is reported (or IP stack ready state is achieved)
wifi.sta.disconnect()
--settings.lua
SSID = "xxxx"
APPWD = "yyyy"
cfg =
{
ip="192.168.0.85",
netmask="255.255.255.0",
gateway="192.168.0.1"
}
wifi.sta.setip(cfg)
wifi.sta.config(SSID,APPWD)
wifi.sta.autoconnect(1)
-- wait for WIFI ----
function checkWIFI()
print("Waiting for WIFI...")
ipAddr = wifi.sta.getip()
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
print("IP Address: " ..ipAddr)
else
-- schedule try again
tmr.alarm( 0 , 1000 , 0 , checkWIFI)
end
end
tmr.alarm( 0 , 1000 , 0 , checkWIFI)
Tried with and without static IP configuration, seems no different
Is this normal?
Is there a way to make faster?
Am I just doing it wrong?

The following is my practical experience, which may be out of date now. I need to retest these...
I do a similar thing and it works well. However, while WiFi is not available quickly after a "reset", it is available very quickly on wakeup from deep sleep.
After first use (settings are saved automatically) I later simply do this
lua
wifi.sta.setip(cfg)
wifi.sta.status() -- this used to speed things up
I also set wifi.sta.autoconnect(0) to avoid dhcp delays.
BTW, to check for a connection it is best to wait for wifi.sta.status() == 5.
HTH

Based on #Eyal's answer, I was able to get an IP address typically in 415-470ms from deep sleep (based on the timer), or 3.25 seconds from cold power on.
NOTE: There is no DNS resolution with this solution
Save your network config to flash
wifi.sta.clearconfig()
wifi.setmode(wifi.STATION)
station_cfg={}
station_cfg.ssid="MyWiFiNetwork"
station_cfg.pwd="MyWiFiPassword"
station_cfg.save=true
station_cfg.auto=false
wifi.sta.config(station_cfg)
On every boot
cfg =
{
ip="192.168.0.99",
netmask="255.255.255.0",
gateway="192.168.0.1"
}
wifi.sta.setip(cfg)
wifi.sta.connect()
Test it with
function checkWIFI()
ipAddr = wifi.sta.getip()
if ( wifi.sta.status() == wifi.STA_GOTIP )then
--LED On
gpio.write(4, gpio.LOW)
gpio.mode(4, gpio.OUTPUT)
print("Time " .. tmr.now()/1000 .. "ms")
print("IP Address: " ..ipAddr)
else
-- schedule try again
tmr.create():alarm(50, tmr.ALARM_SINGLE, checkWIFI)
end
print("WiFi Status " .. wifi.sta.status())
end
tmr.create():alarm(50, tmr.ALARM_SINGLE, checkWIFI)

Related

Cannot connect to wi-fi network from NodeMCU board

I am trying to connect to my wifi network on my NodeMCU board. Not sure if it's a hardware or software problem, but I could not find any help on the issue.
I am trying to use this code for connecting to the WiFi:
wifi.setmode(wifi.STATION)
station_cfg={};
station_cfg.ssid="netia9000";
station_cfg.pwd="mywifipassword";
wifi.sta.config(station_cfg)
wifi.sta.connect()
status_of_wifi = wifi.sta.status()
if status_of_wifi == wifi.STA_IDLE then print("IDLE") end;
if status_of_wifi == wifi.STA_CONNECTING then print("CONNECTING") end;
if status_of_wifi == wifi.STA_WRONGPWD then print("WRONG PS") end;
if status_of_wifi == wifi.STA_APNOTFOUND then print("404") end;
if status_of_wifi == wifi.STA_FAIL then print("500") end;
if status_of_wifi == wifi.STA_GOTIP then print("IP GOT") end;
print(wifi.sta.getip())
But on console I can read following:
CONNECTING
nil
I tried to put the wrong data - a WiFi SSID that does not exist, a wrong `password, but no matter what I am still getting the same output: "CONNECTING" and "nil".
I used this code to check for available networks:
wifi.setmode(wifi.STATION)
-- print ap list
function listap(t)
for ssid,v in pairs(t) do
authmode, rssi, bssid, channel =
string.match(v, "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
print(ssid,authmode,rssi,bssid,channel)
end
end
wifi.sta.getap(listap)
And this worked perfectly fine. I got on the console:
netia9000 3 -52 e8:11:23:43:bf:a2:8f 10
-- other wi fi networks available nearby --
So it looks like the wifi module is fine and it's a software problem. I wrote the code according to the documentation. At this point I have no idea what's wrong. Any suggestions?
wifi.sta.connect() is not synchronous, so there's no guarantee that the AP would be done connecting by the time your .status() code runs. Indeed, the docs say it should be unnecessary unless .config()'s auto value is set false.
You could, however, add a callback to .config() like this:
function showip(params)
print("Connected to Wifi. Got IP: " .. params.IP)
end
...
station_cfg.got_ip_cb = showip
wifi.sta.config(station_cfg)
Keep in mind that wifi can go up and down all the time. If you need to pounce on connect (one-time or every connect), you really want to register a callback rather than assuming that there will be one constant connection.
The callback will have access to all your globals, so you can store software state there, just make sure you're OK with any possible race conditions you may conjure up (locking/sync is a discussion for another thread).

nodemcu lua dofile - how to invoke another lua file?

I have a working init.lua on my nodemcu esp8266:
-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")
function startup()
if file.open("init.lua") == nil then
print("init.lua deleted or renamed")
else
print("Running")
file.close("init.lua")
-- the actual application is stored in 'application.lua'
-- dofile("application.lua")
end
end
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(cb_timer)
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
cb_timer:unregister()
print("WiFi connection established, IP address: " .. wifi.sta.getip())
print("You have 3 seconds to abort")
print("Waiting...")
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end
end)
It runs without errors and the wireless connection is established.
Now I have written a second bme280_mqtt.lua that I want to run automatically:
alt=45 -- altitude of the measurement place
bme280.init(3, 4)
P, T = bme280.baro()
-- convert measure air pressure to sea level pressure
QNH = bme280.qfe2qnh(P, alt)
ldk=string.format("Ld=%d.%03d ", QNH/1000, QNH%1000)
H, T = bme280.humi()
if T<0 then
temp=string.format("T=-%d.%02d°C ", -T/100, -T%100)
else
temp=string.format("T=%d.%02d°C ", T/100, T%100)
end
luftf=string.format("Lf=%d%% ", H/1000, H%1000)
D = bme280.dewpoint(H, T)
if D<0 then
taupu=string.format("Tp=-%d.%02d°C ", -D/100, -D%100)
else
taupu=string.format("Tp=%d.%02d°C ", D/100, D%100)
end
m = mqtt.Client("wetterstation", 120)
m:connect("192.168.1.116", 1883)
m:publish("wetterstation",temp .. taupu .. luftf .. ldk,0,0)
node.dsleep(10*1000000)
Called by hand in ESPlorer via Send to ESP Button everything works fine.
But with
dofile(bme280_mqtt.lua)
I get:
dofile('bme280_mqtt.lua')
bme280_mqtt.lua:25: not connected
stack traceback:
[C]: in function 'publish'
bme280_mqtt.lua:25: in main chunk
[C]: in function 'dofile'
stdin:1: in main chunk
What is the mistake here? And how do I call the bme280_mqtt.lua from init.lua correctly?
Kind regards
how do I call the bme280_mqtt.lua from init.lua correctly?
You do invoke it correctly.
bme280_mqtt.lua:25: not connected
Means that there's an error on/from line 25 from bme280_mqtt.lua.
I didn't count the lines but the problem is right here
m:connect("192.168.1.116", 1883)
m:publish("wetterstation",temp .. taupu .. luftf .. ldk,0,0)
You can only publish once the connection to the broker was established. Look at the example at http://nodemcu.readthedocs.io/en/latest/en/modules/mqtt/#example. You can either use the callback function in the connect function to publish or register an on-connect event handler before you call connect like so:
m:on("connect", function(client)
-- publish here
end)

NodeMCU WiFi auto connect

I am trying to resolve wifi connectivity using Lua language. I have been combing through the api to find a solution but nothing solid yet. I asked a previous question, dynamically switch between wifi networks and the answer did address the question in the way I asked it, but it didn't accomplish what I expected.
Basically, I have two different networks from two different providers. All I want the ESP8266 12e to do is detect when or if the current network has no internet access and automatically switch to the next network. It must continuously try to connect at say a 3 minute interval until it is successful and not just give up.
For testing purposes I tried this code below. The plan is to use the variable "effectiveRouter" and write some logic to switch based on the current router.
effectiveRouter = nil
function wifiConnect(id,pw)
counter = 0
wifi.sta.config(id,pw)
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 10 then
if wifi.sta.getip() == nil then
print("NO IP yet! Trying on "..id)
tmr.start(1)
else
print("Connected, IP is "..wifi.sta.getip())
end
end
end)
end
wifiConnect("myNetwork","myPassword")
print(effectiveRouter)
When I run that code, I get effectiveRouter as nil on the console. This tells me that the print statement ran before the method call was complete, print(effectiveRouter). I am very very new to lua as this is my first time with the language. I am certain this boiler plate code must have been done before. Can someone please point me in the right direction? I am open to shifting to the arduino IDE as I already have it set up for the NodeMCU ESP8266. May be I can follow the logic better as I come from a java-OOP background.
I eventually sat down and tested my sketch from the previous answer. Two additional lines and we're good to go...
What I missed is that wifi.sta.config() resets the connection attempts if auto connect == true (which is the default). So, if you call it to connect to AP X while it's in the process of connecting to X it will start from scratch - and thus usually not get an IP before it's called again.
effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 30 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to dlink")
tmr.start(1) -- restart
else
print("Connected to dlink, IP is "..wifi.sta.getip())
effectiveRouter = "dlink"
--startProgram()
end
elseif counter == 30 then
wifi.sta.config("cisco", "password2")
-- there should also be tmr.start(1) in here as suggested in the comment
elseif counter < 60 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to cisco")
tmr.start(1) -- restart
else
print("Connected to cisco, IP is "..wifi.sta.getip())
effectiveRouter = "cisco"
--startProgram()
end
else
print("Out of options, giving up.")
end
end)
You better to migrate a callback based architecture to be sure that you have successfully connected. Here is doc for it :
https://nodemcu.readthedocs.io/en/master/en/modules/wifi/#wifistaeventmonreg
You can listen for
wifi.STA_GOTIP
And make your custom operations in it. Do not forget to start eventmon.
P.s. I am not able to see your variable effectiveRouter in related function.

dynamically switch between wifi networks

I have two WiFi networks at home where I want to use my NodeMCU ESP8266 V1 to control several relays remotely over the web from anywhere in the world.To accomplish this I was thinking to test for WiFi connectivity and if I don't get an IP within 1-minute try the other network until I get an IP. Here is the API docs for tmr which I followed in the code below.
Is there a way to switch between two or more wifi networks programatically using Lua? I am using the Lua language, however I can move to arduino IDE, if required.
wifi.setmode(wifi.STATION)
myRouter = "dlink"
tmr.alarm(1, 60000, tmr.ALARM_SINGLE, function()
if myRouter=="dlink" then
print("Dlink selected")
wifi.sta.config("dlink","password1")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
elseif myRouter=="cisco" then
print("Cisco selected")
wifi.sta.config("cisco","passoword2")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
else
print("No network is giving an ip")
end
end)
What I am looking for is a callback which fires whenever the timer "tmr" expires. This way I can change the variable to myRouter="cisco". Notice in the code above i was unable to change the "myRouter" variable.
I considered using a software watchdog to monitor the conectivity all the time so if or when the WiFi drops on one network, it will trigger a reconnect by running the code above. I am not sure how to do this or how its usually done, since I am very new to lua. Please advise or point me to a resource which can help in this regard. Thanks guys.
This is an untested piece of code quickly put together.
effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 60 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to dlink")
tmr.start(1) -- restart
else
print("Connected to dlink, IP is "..wifi.sta.getip())
effectiveRouter = "dlink"
startProgram()
end
elseif counter < 120 then
wifi.sta.config("cisco", "password2")
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to cisco")
tmr.start(1) -- restart
else
print("Connected to cisco, IP is "..wifi.sta.getip())
effectiveRouter = "cisco"
startProgram()
end
else
print("Out of options, giving up.")
end
end)
It'll first try to connect to 'dlink' for 60s, then to 'cisco' for another 60s, and will eventually give up after that if neither attempts was successful. It uses a semi-automatic timer which is only restarted if there's no IP yet.

NodeMCU timeout when using while loop

I have a Lua script that sends an email to myself via SMTP. Everything works fine when uploading to the NodeMCU and saying dofile("sendemail.lua").
-- sendmail.lua
-- The email and password from the account you want to send emails from
MY_EMAIL = "REDACTED"
EMAIL_PASSWORD = "REDACTED"
-- The SMTP server and port of your email provider.
-- If you don't know it google [my email provider] SMTP settings
SMTP_SERVER = "isp.smtp.server"
SMTP_PORT = 25
-- The account you want to send email to
mail_to = "REDACTED"
-- Your access point's SSID and password
SSID = "REDACTED"
SSID_PASSWORD = "REDACTED"
-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)
email_subject = ""
email_body = ""
count = 0
local smtp_socket = nil -- will be used as socket to email server
-- The display() function will be used to print the SMTP server's response
function display(sck,response)
print(response)
end
-- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
-- I was going to use socket callbacks but the code would not run callbacks after the first 3.
function do_next()
if(count == 0)then
count = count+1
IP_ADDRESS = wifi.sta.getip()
smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
elseif(count==1) then
count = count+1
smtp_socket:send("AUTH LOGIN\r\n")
elseif(count == 2) then
count = count + 1
smtp_socket:send("REDACTED".."\r\n")
elseif(count == 3) then
count = count + 1
smtp_socket:send("REDACTED".."\r\n")
elseif(count==4) then
count = count+1
smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
elseif(count==5) then
count = count+1
smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
elseif(count==6) then
count = count+1
smtp_socket:send("DATA\r\n")
elseif(count==7) then
count = count+1
local message = string.gsub(
"From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" ..
"To: \"".. mail_to .. "\"<".. mail_to..">\r\n"..
"Subject: ".. email_subject .. "\r\n\r\n" ..
email_body,"\r\n.\r\n","")
smtp_socket:send(message.."\r\n.\r\n")
elseif(count==8) then
count = count+1
tmr.stop(0)
smtp_socket:send("QUIT\r\n")
print("msg sent")
else
smtp_socket:close()
end
print(count)
end
-- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
-- This function will create a timer to call the do_next function which will send the SMTP commands
-- in sequence, one by one, every 5000 seconds.
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
function connected(sck)
tmr.alarm(0,5000,1,do_next)
end
-- #name send_email
-- #description Will initiated a socket connection to the SMTP server and trigger the connected() function
-- #param subject The email's subject
-- #param body The email's body
function send_email(subject,body)
count = 0
email_subject = subject
email_body = body
smtp_socket = net.createConnection(net.TCP,0)
smtp_socket:on("connection",connected)
smtp_socket:on("receive",display)
smtp_socket:connect(SMTP_PORT, SMTP_SERVER)
end
-- Send an email
send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")
However, I want to use a loop to monitor an analog input value and only send the email when certain analog input values are detected. Therefore, I added this code at the end of the script, after the sendemail() function definition and immediately before the function sendmail('subject', 'body') is called
vp = 0
gpio.mode(vp, gpio.INPUT)
while true do
local v = adc.read(vp)
if v < 840 or v > 870 then
print(v)
break
end
tmr.wdclr()
end
sendmail('subject', 'body')
The while loop works perfectly, waiting indefinitely for input from the analog pin. Once that input is found, it breaks correctly and calls the sendmail function. However, once that function is called, NodeMCU eventually resets. Sometimes it will get as far as successfully authenticating the SMTP credentials with the server, and sometimes it will not even make the HELO before it shuts down. What could possibly be causing this? Why would the sendmail.lua script work fine then suddenly decide not to work when adding this one small while loop that appears to work perfectly fine on its own?
A little quote from the NodeMCU reference:
tmr.wdclr() Feed the system watchdog.
In general, if you ever need to use this function, you are doing it
wrong.
The event-driven model of NodeMCU means that there is no need to be
sitting in hard loops waiting for things to occur. Rather, simply use
the callbacks to get notified when somethings happens. With this
approach, there should never be a need to manually feed the system
watchdog.
Please note the second line. :)
Not sure what your problem is, but why do you use a while loop in the first place? Why not use timer events to poll your ADC regularly?
Maybe the watchdog is triggered because your feed comes to late for some reason. In the if case you don't feed it at all befor you leave the loop.
Even if it may not be the definite answer I post it as such since the comment input is too small.
First, I suggest you use the script I posted for your previous question. This one isn't handling WiFi setup correctly. You need to wait in a timer until the device got an IP before you can continue. Remember, wifi.sta.config is non-blocking. And since it uses auto-connect=true if not set explicitly it'll try to connect to the AP immediately. That's also the reason why wifi.sta.autoconnect(1) is superfluous.
I don't understand the ADC reading code you posted.
vp = 0
gpio.mode(vp, gpio.INPUT)
Seems unnecessary to me because a) you don't do anything with GPIO 0 and b) adc.read only supports 0.
Rather than using a busy loop and constantly feeding the watch dog, which is a very bad sign, I suggest you use an interval based timer. Furthermore, I guess you don't wanna break the loop the first time the condition is met and never come back? So, you need to stay in the loop and keep triggering send mail, no? Something like this maybe (untested):
tmr.alarm(0, 200, tmr.ALARM_AUTO, function()
local v = adc.read(0)
if v < 840 or v > 870 then
node.task.post(function()
send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")
end)
end
end)

Resources