-- 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 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 id2 = 0 switch_state = 0 gpio.mode(SWITCH_PIN, gpio.OUTPUT) gpio.write(SWITCH_PIN, gpio.LOW) -- connect to the broker print "Connecting to MQTT broker. Please wait..." m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD) m:connect( BROKER , BRPORT, 0, function(conn) print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID ) mqtt_sub() --run the subscription function 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 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 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 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. m:on("message", function(conn, topic, data) print(topic .. ":" ) if (data ~= nil ) then print ( data ) if (data == "switch on") then switch_state = 1 gpio.write(SWITCH_PIN, gpio.HIGH) elseif (data == "switch off") then switch_state = 0 gpio.write(SWITCH_PIN, gpio.LOW) end end end ) end