-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
144 lines (96 loc) · 2.47 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- ESP-01 GPIO Mapping
gpio0 = 4
power = 2
powerADC= 1
gpio.mode(power,gpio.OUTPUT)
gpio.write(power,gpio.LOW)
gpio.mode(powerADC,gpio.OUTPUT)
gpio.write(powerADC,gpio.LOW)
mqtt_connect = 0
tmr.delay(1000000)
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("dacha11","paradox1234567")
wifi.setphymode(wifi.PHYMODE_B)
wifi.sta.connect()
function getSensors()
temp1 = 0
t = require("ds18b20")
gpio.write(power,gpio.HIGH)
gpio.write(powerADC,gpio.HIGH)
tmr.delay(800000)
t.setup(gpio0)
addrs = t.addrs()
if (addrs ~= nil) then
print("Total DS18B20 sensors: "..table.getn(addrs))
end
tmr.delay(800000)
temp1 = t.read()
tmr.delay(800000)
temp1 = t.read()
-- Just read temperature
-- Get temperature of first detected sensor in Fahrenheit
-- print("Temperature: "..t.read(nil,t.F).."'F")
-- Query the second detected sensor, get temperature in Kelvin
-- if (table.getn(addrs) >= 2) then
-- print("Second sensor: "..t.read(addrs[1],t.K).."'K")
-- end
humidy=0
for i=1,100 do
humidy = humidy + adc.read(0)
tmr.delay(100000)
end
humidy = humidy / 100
print("Temperature: "..temp1.."'C")
print("Humidy: "..humidy)
gpio.write(power,gpio.LOW)
gpio.write(powerADC,gpio.LOW)
-- Don't forget to release it after use
t = nil
ds18b20 = nil
package.loaded["ds18b20"]=nil
end
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
pl = "/dacha11/sensors/"..wifi.sta.getmac()
m=mqtt.Client("Sensor",60,"dacha11","qwerty")
m:on("connect",function(m)
print("connection "..node.heap())
m:publish(pl,"hello",0,0)
m:publish(pl.."/temp1",temp1,0,0,function()
print(pl.."/temp1 "..temp1)
end )
m:publish(pl.."/humidy",humidy,0,0, function ()
print(pl.."/humidy "..humidy)
end )
mqtt_connect = 1
end )
m:on("offline", function(conn)
print("disconnect to broker...")
print(node.heap())
mqtt_connect = 0
end)
m:on("message", function(m, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- m:on("message",dispatch )
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
getSensors()
m:connect("192.168.1.10",8883,0,1)
tmr.alarm(5,3000,tmr.ALARM_AUTO,function()
if mqtt_connect then
print("Connect to mqtt")
tmr.stop(5)
else
print("Not yet connected to brocker")
end
end )
end
end)