Created
April 4, 2019 13:05
-
-
Save FGasper/d48a3d14020e2de7f460ccc391784ba4 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
BROADCAST_PORT = 65000 | |
SDA, SCL = 3, 4 | |
i2c.setup(0, SDA, SCL, i2c.SLOW) -- call i2c.setup() only once | |
bme280.setup() | |
function get_iso_time() | |
tm = rtctime.epoch2cal(rtctime.get()) | |
timestr = string.format("%04d-%02d-%02dT%02d:%02d:%02dZ", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]) | |
return timestr | |
end | |
sntp.sync( nil, function() | |
uskt = net.createUDPSocket() | |
tmr.create():alarm( 5000, tmr.ALARM_AUTO, function(mytmr) | |
--[[ | |
local P, T = bme280.baro() | |
P = P/1000 | |
T = T/100 | |
local H = bme280.humi() | |
H = H/1000 | |
envjson = "{\"temp\":" .. T .. ", \"humi\":" .. H .. ", \"baro\":" .. P .. ", \"time\":\"" .. get_iso_time() .. "\"}" | |
-- Switch back to UDP? | |
local udp = net.createUDPSocket() | |
udp:send(BROADCAST_PORT, wifi.sta.getbroadcast(), envjson) | |
udp:close() | |
-- MQTT is reliable but apparently unnecessary since | |
-- D-Bus is being used anyway. | |
local mq = mqtt.Client("nursery", 120) | |
mq:connect("thegaspers.org", function(mc) | |
mc:publish("gasperhome/nursery", envjson, 0, 1, function(mc) | |
mc:close() | |
end) | |
end) | |
]] | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment