-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtcp_client_temp.lua
More file actions
93 lines (86 loc) · 2.3 KB
/
tcp_client_temp.lua
File metadata and controls
93 lines (86 loc) · 2.3 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
SensorID = node.chipid()
local humidity = 0
local temperature = 0
server = "192.168.1.36"
port = 8888
PIN = 5
DHT= require("dht_lib")
function init_i2c_display()
-- SDA and SCL can be assigned freely to available GPIOs
sda = 3 -- GPIO14
scl = 4 -- GPIO12
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(sla)
end
function prepare()
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
function readTemp()
DHT.read11(PIN)
local t = DHT.getTemperature()
local h = DHT.getHumidity()
if h == nil then
print("Error reading from DHT11/22")
else
-- temperature in degrees Celsius and Farenheit
humidity = h --((h - (h % 10)) / 10).."."..(h % 10)
temperature = t --((t-(t % 10)) / 10).."."..(t % 10)
print("Temperature: "..temperature.." deg C")
print("Humidity: "..humidity.."%")
end
end
function open()
print("Open connection...")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:on("disconnection", function()
print("Disconnection..")
tmr.stop(0)
end)
conn:on("reconnection", function()
print("Reconnection..")
end)
conn:on("connection", function()
print("Connected..")
start()
--conn:send("{ \"Type\":\"TEMP\",\"SensorID\":\"".. SensorID .. "\"}")
end)
conn:connect(port, server)
end
function send()
readTemp()
if (humidity ~= nil) then
file.open("temp.MONO", "r")
xbm_data = file.read()
file.close()
disp:firstPage()
repeat
disp:drawXBM( 0, 0, 60, 60, xbm_data )
disp:setScale2x2()
disp:drawStr(35,5, temperature.."C")
disp:drawStr(35,15, humidity.."%")
disp:undoScale()
until disp:nextPage() == false
xbm_data = nil
conn:send('{ "Type": "TEMP", "SensorID":'.. SensorID .. ', "temperature": '..temperature..', "humidity": '..humidity..'}')
end
end
function close()
conn:close()
end
init_i2c_display()
prepare()
tmr.alarm(0, 2000, 0, function()
open()
end)
function start()
send()
tmr.alarm(0, 5000, 1, function() -- Set alarm to one second
send()
end)
end
--tmr.stop(0)