-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtcp_server_temp.lua
More file actions
41 lines (38 loc) · 1.02 KB
/
tcp_server_temp.lua
File metadata and controls
41 lines (38 loc) · 1.02 KB
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
gpio.mode(5, gpio.OUTPUT)
gpio.write(5, 0)
port = 8888
local humidity = 0
local temperature = 0
PIN = 0
DHT= require("dht_lib")
function readTemp()
DHT.read22(PIN)
temperature = DHT.getTemperature()
humidity = DHT.getHumidity()
print(temperature)
print(humidity)
end
function open()
print("Open connection...")
srv=net.createServer(net.TCP)
srv:listen(port,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
_, _, method, pin = string.find(payload, "([A-Z]+[0-9]+),([0-9]+)")
print(method)
print(pin)
if (method == 'D5') then
gpio.write(5, tonumber(pin))
end
if (method == 'READ1') then
readTemp()
if (humidity ~= nil) then
conn:send('{ "Type": "TEMP", "SensorID":'.. node.chipid() .. ', "temperature": '..temperature..', "humidity": '..humidity..'}')
else
conn:send('{"error": 100}')
end
end
end)
end)
end
open()