-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
77 lines (65 loc) · 2.43 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- Starts the module and connect to server.
print("DHT11 Domoticz v0.1")
-- variables
local dht11 = require("dht11")
local deviceID = "20"
local server_ip = "192.168.0.148"
local server_port = 8080
local deep_sleep_time = 180 --seconds
-- wifi connection config
wifi.setmode(wifi.STATION)
wifi.sta.config("yourSSID","yourpassword")
local cfg =
{
ip="192.168.0.1"..deviceID, --static ip based on id
netmask="255.255.255.0",
gateway="192.168.0.1"
}
-- check every 0.5seconds if we can get an ip, once we get it start the TCP client
tmr.alarm(0, 500, 1, function()
gotIP = wifi.sta.setip(cfg)
if ( wifi.sta.status() == 5 ) then
print("connected to WiFi")
tmr.stop(0)
-- restart the module after 30seconds if it's still ON:
-- it probably means it's stalled
tmr.alarm(1, 30000, 1, function()
node.restart()
end)
initSocketAndTransmitData()
end
end)
-- initializes the connection with the DOmoticz server and sends the current data.
-- Once data is sent and an answer is recieved, either restart the node or go into deep sleep
-- (depends on what's the answer from Domoticz)
function initSocketAndTransmitData()
local socket = net.createConnection(net.TCP, 0)
socket:connect(server_port, server_ip)
--once we're connected, send the data
socket:on("connection", function(conn)
print("Connected to Domoticz")
sendStatus(socket)
end)
-- once we get an answer from Domoticz, either:
-- go into deep sleep if the command succeeded
-- restart the node (and send another packet immediatly) if there was an error
socket:on("receive", function(conn, message)
if string.match(message, "\"status\" : \"OK\",") then
print ("Got OK, going to sleep for a while now")
node.dsleep(deep_sleep_time * 1000000)
else
print ("Got something else: "..message)
node.restart()
end
end)
end
-- sends the sensor data (temperature and humidity) to the Domoticz server.
function sendStatus(socket)
local temperatureAndHumidity = dht11.getData()
print("got "..temperatureAndHumidity)
local json = "GET /json.htm?type=command¶m=udevice&idx="..deviceID..
"&nvalue=0&svalue="..temperatureAndHumidity..
";0 HTTP/1.1\r\nHost: www.local.lan\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
socket:send(json)
end