I just started using nodemcu with lua. As a starter I am trying to make a simple wifi controlled Relay with Nodemcu as a UDP server. The problem is, after running for several hours, I cant make connection to the board. I tried ping ing the board using CMD ping command but got no response. If I restart the board, it works again.. Any ideas why? Thanks
Here is my Lua script:
pin_relay = 1
port = 1310
state = 0
gpio.mode(pin_relay, gpio.OUTPUT)
gpio.write(pin_relay, gpio.HIGH)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID", "password")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.1.200",netmask="255.255.255.0",gateway="192.168.1.1"})
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
srv=net.createServer(net.UDP)
srv:on("receive", function(srv, pl)
if pl=="switch" then
if state == 0 then
gpio.write(pin_relay,gpio.LOW)
state = 1
elseif state == 1 then
gpio.write(pin_relay,gpio.HIGH)
state = 0
end
end
end)
srv:listen(port)