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 dofile("synctime.lua"); mdns.register("water", {hardware='NodeMCU'}); print("mdns:water.local/");
print("connecting mqtt server"); dofile("mqtt.lua") end
-- Define WiFi station event callbacks wifi_connect_event = function(T) print("Connection to AP("..T.SSID..") established!") print("Waiting for IP address...") if disconnect_ct ~= nil then disconnect_ct = nil end end
wifi_got_ip_event = function(T) -- Note: Having an IP address does not mean there is internet access! -- Internet connectivity can be determined with net.dns.resolve(). print("Wifi connection is ready! IP address is: "..T.IP) print("Startup will resume momentarily, you have 3 seconds to abort.") print("Waiting...") tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup) end
wifi_disconnect_event = function(T) if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then --the station has disassociated from a previously connected AP return end -- total_tries: how many times the station will attempt to connect to the AP. Should consider AP reboot duration. local total_tries = 5000 print("\nWiFi connection to AP("..T.SSID..") has failed!")
wifi_connetion1=0; --There are many possible disconnect reasons, the following iterates through --the list and returns the string corresponding to the disconnect reason. for key,val in pairs(wifi.eventmon.reason) do if val == T.reason then print("Disconnect reason: "..val.."("..key..")") break end end
--if disconnect_ct == nil then -- disconnect_ct = 1 -- else --disconnect_ct = disconnect_ct + 1 --end --if disconnect_ct < total_tries then -- print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")") --else -- wifi.sta.disconnect() --print("Aborting connection to AP!") --disconnect_ct = nil --end end
print("Connecting to WiFi access point...") wifi.setmode(wifi.STATION) wifi.sta.config(station_cfg) wifi.sta.autoconnect(1) -- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
function stopReconnect() -- clear connecting state and clear connect timer! mqtt_connecting = 0 tmr.stop(3) end
-- 重连方法 function reconnect() if mqtt_connecting == 0 then tmr.alarm(3, 20000, 1, function() print("start mqtt server reconnect!") mq:close() mq:connect("x.x.x.x", 1883, 0) end) mqtt_connecting = 1 end end
-- 收到消息时触发事件 mq:on("message", function(client, topic, data) print(topic .. ":" ) if data ~= nil then if topic == "tai3_water" then print(data) if data == "water1min" then dofile("water.lua") --mq:publish("report_info","tai3_water1",0,0) elseif data == "water2min" then dofile("water2.lua") --mq:publish("report_info","tai3_water2",0,0) elseif data == "water4min" then dofile("water4.lua") --mq:publish("report_info","tai3_water4",0,0) end end
--if topic == "rpt_water" and tonumber(data) > 12 then --print("aaa"..data) --end end end)