Last active
March 22, 2016 12:11
-
-
Save perusio/b1f06280a3f52a60a942 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en | |
wifi.setmode(wifi.STATION) | |
wifi.sta.config("iotlab1","IoTInnoLab") | |
mqttOnline = false | |
mqttSubSuccess = false | |
-- timer vars | |
mainTimerId = 0 -- we have seven timers! 0..6 | |
mainInterval = 1000 -- milliseconds | |
statusTimerID = 1 | |
statusInterval = 10000 | |
hackTimerID = 2 | |
hackTimerInterval = 100 | |
-- client ID, keepalive (seconds), user, password | |
m = mqtt.Client("ESP8266_dev", 30, "305f9533-6d4a-4c2d-886a-f7fbcb4a08f5", "qmtpIXtIgeQW") | |
m:lwt("/v1/305f9533-6d4a-4c2d-886a-f7fbcb4a08f5/data", cjson.encode({txt = "offline"}), 0, 0) | |
m:on("connect", function(con) | |
print("connected") | |
mqttOnline = true | |
local msg = cjson.encode({{ meaning = 'string', value = 'hello world'}, {meaning = 'integer', value = tmr.now()} }) | |
m:publish("/v1/305f9533-6d4a-4c2d-886a-f7fbcb4a08f5/data", msg) | |
-- hack to prevent stdio bomb calling subscribe in this callback | |
tmr.alarm(hackTimerID, hackTimerInterval, 0, function() | |
m:subscribe("/v1/305f9533-6d4a-4c2d-886a-f7fbcb4a08f5/data",0, function(con) | |
print("subscribe success") | |
mqttSubSuccess = true | |
end) | |
end) | |
end) | |
function updateMQTT() | |
if wifi.sta.status() == 5 then | |
print("calling m:connect()") | |
m:connect("mqtt.relayr.io", 1883) | |
end | |
end | |
m:on("offline", function(con) | |
print("offline") | |
mqttOnline = false | |
mqttSubSuccess = false | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment