diff --git a/init.lua b/init.lua index 05ad260..e25b863 100644 --- a/init.lua +++ b/init.lua @@ -1,18 +1,16 @@ -- Constants -SSID = "MYWIWIF" -APPWD = "QuantumPassword" -CMDFILE = "mqtt.lua" -- File that is executed after connection +SSID = "MessWLAN" +APPWD = "UNVmpwbr6heQnMQ7ykXT" +CMDFILE = "mqtt.lua" -- Some control variables -wifiTrys = 0 -- Counter of trys to connect to wifi -NUMWIFITRYS = 200 -- Maximum number of WIFI Testings while waiting for connection +wifiTrys = 0 +NUMWIFITRYS = 200 --- Change the code of this function that it calls your code. function launch() print("Connected to WIFI!") print("IP Address: " .. wifi.sta.getip()) - -- Call our command file every minute. - tmr.alarm(0, 60000, 0, function() dofile(CMDFILE) end ) + tmr.alarm(0, 5000, 0, function() dofile(CMDFILE) end ) end function checkWIFI() @@ -21,10 +19,8 @@ function checkWIFI() else ipAddr = wifi.sta.getip() if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then - -- lauch() -- Cannot call directly the function from here the timer... NodeMcu crashes... tmr.alarm( 1 , 500 , 0 , launch ) else - -- Reset alarm again tmr.alarm( 0 , 2500 , 0 , checkWIFI) print("Checking WIFI..." .. wifiTrys) wifiTrys = wifiTrys + 1 @@ -34,17 +30,13 @@ end print("-- Starting up! ") --- Lets see if we are already connected by getting the IP ipAddr = wifi.sta.getip() if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then - -- We aren't connected, so let's connect print("Configuring WIFI....") wifi.setmode( wifi.STATION ) wifi.sta.config( SSID , APPWD) print("Waiting for connection") - tmr.alarm( 0 , 2500 , 0 , checkWIFI ) -- Call checkWIFI 2.5S in the future. + tmr.alarm( 0 , 2500 , 0 , checkWIFI ) else - -- We are connected, so just run the launch code. launch() end --- Drop through here to let NodeMcu run \ No newline at end of file diff --git a/mqtt.lua b/mqtt.lua index 760784e..8f26797 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -1,92 +1,73 @@ --- Configuration to connect to the MQTT broker. -BROKER = "172.16.2.15" -- Ip/hostname of MQTT broker -BRPORT = 1883 -- MQTT broker port -BRUSER = "" -- If MQTT authenitcation is used then define the user -BRPWD = "" -- The above user password -CLIENTID = "ESP8266-" .. node.chipid() -- The MQTT ID. Change to something you like +BROKER = "172.16.2.15" +BRPORT = 1883 +BRUSER = "" +BRPWD = "" +CLIENTID = "ESP8266y-" .. node.chipid() SWITCH_PIN = 0 SWITCH_ID = 0 --- MQTT topics to subscribe -topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID} -- Add/remove topics to the array - --- Control variables. -pub_sem = 0 -- MQTT Publish semaphore. Stops the publishing whne the previous hasn't ended -current_topic = 1 -- variable for one currently being subscribed to -topicsub_delay = 50 -- microseconds between subscription attempts, worked for me (local network) down to 5...YMMV -id1 = 0 +topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID} +pub_sem = 0 +current_topic = 1 +topicsub_delay = 50 id2 = 0 switch_state = 0 -gpio.mode(SWITCH_PIN, gpio.OUTPUT) -gpio.write(SWITCH_PIN, gpio.LOW) +-- gpio.mode(SWITCH_PIN, gpio.OUTPUT) +-- gpio.write(SWITCH_PIN, gpio.LOW) --- connect to the broker -print "Connecting to MQTT broker. Please wait..." +print("Connecting to MQTT broker. Please wait...") m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD) +print("step 1") +tmr.alarm(4, 15, 1 mqtt_watchdog_expired) +print("step 2") m:connect( BROKER , BRPORT, 0, function(conn) print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID ) - mqtt_sub() --run the subscription function + mqtt_sub() end) +print("step 3") + + + +function mqtt_watchdog_expired() + print("mqtt watchdog expired") +end function mqtt_sub() if table.getn(topics) < current_topic then - -- if we have subscribed to all topics in the array, run the main prog run_main_prog() else - --subscribe to the topic m:subscribe(topics[current_topic] , 0, function(conn) print("Subscribing topic: " .. topics[current_topic - 1] ) end) - current_topic = current_topic + 1 -- Goto next topic - --set the timer to rerun the loop as long there is topics to subscribe + current_topic = current_topic + 1 tmr.alarm(5, topicsub_delay, 0, mqtt_sub ) end end --- Sample publish functions: -function publish_heartbeat() - if pub_sem == 0 then -- Is the semaphore set= - pub_sem = 1 -- Nop. Let's block it - local uptime = tmr.time() - local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. "}}" - m:publish("IoT/Heartbeat/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn) - -- Callback function. We've sent the data - print("Heartbeat sent: " .. id1) - pub_sem = 0 -- Unblock the semaphore - id1 = id1 +1 -- Let's increase our counter - end) - end -end function publish_status() - if pub_sem == 0 then -- Is the semaphore set= - pub_sem = 1 -- Nop. Let's block it + if pub_sem == 0 then + pub_sem = 1 local uptime = tmr.time() local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. ", \"state\":" .. switch_state .. "}}" m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn) - -- Callback function. We've sent the data print("Status sent: " .. id2) print("State: " .. switch_state) - pub_sem = 0 -- Unblock the semaphore - id2 = id2 +1 -- Let's increase our counter + pub_sem = 0 + id2 = id2 +1 end) end end ---main program to run after the subscriptions are done function run_main_prog() - print("Main program") - - tmr.alarm(2, 1000, 1, publish_heartbeat ) - tmr.alarm(3, 1346, 1, publish_status ) - -- Callback to receive the subscribed topic messages. + tmr.alarm(3, 1000, 1, publish_status ) m:on("message", function(conn, topic, data) print(topic .. ":" ) if (data ~= nil ) then