-
Notifications
You must be signed in to change notification settings - Fork 3
/
bme280-oled.lua
231 lines (207 loc) · 5.57 KB
/
bme280-oled.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
dofile("config.lua")
mqtt_connected = false
data_send = false
count = 0
temp = 0
humi = 0
press = 0
rssi = 0
local ledpin = 4
gpio.mode(ledpin, gpio.OUTPUT)
gpio.write(ledpin, 1)
i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once
bme280.setup()
disp = u8g2.ssd1306_i2c_128x64_noname(0, 0x3c)
disp:setFont(u8g2.font_unifont_t_symbols)
disp:setFontRefHeightExtendedText()
disp:setDrawColor(1)
disp:setFontPosTop()
disp:setFontDirection(0)
function blinkled(t)
if not flash_led then
return
end
gpio.write(ledpin, 0)
tmr.alarm(0, t, 0, function ()
gpio.write(ledpin, 1)
end)
end
function mqtt_connect()
m:connect(mqtt_host, mqtt_port, 0, function(c)
mqtt_connected = true
print("mqtt online")
if mqtt_update then
m:subscribe("/cmd/"..node.chipid(),0,function(conn)
print("subscribe to cmd topic")
end)
end
end)
end
wifi_connect_event = function(T)
print("AP("..T.SSID..") connected!")
end
wifi_got_ip_event = function(T)
print("IP is: "..T.IP)
if (send_mqtt and not mqtt_connected) then
print("mqtt try connect to "..mqtt_host..":"..mqtt_port)
mqtt_connect()
end
end
wifi_disconnect_event = function(T)
print("wifi disc")
end
laststr = '|/-\\'
last_index = 1
function updatedisplay()
if data_send then
last_index = last_index + 1
if last_index == 5 then
last_index = 1
end
end
disp:clearBuffer()
if data_send then
disp:drawStr(1, 1, "BME280 "..laststr.sub(laststr, last_index, last_index).." BG6CQ")
else
disp:drawStr(1, 1, "BME280 BG6CQ")
end
disp:drawStr(1, 16, "Temp: "..string.format("%.1f", temp).."C")
disp:drawStr(1, 31, "Humi: "..string.format("%.1f", humi).."%")
disp:drawStr(1, 47, "Press:"..string.format("%.1f", press/10.0).."mpar")
disp:sendBuffer()
end
function send_data()
if send_aprs then
print("aprs send "..aprs_host)
str = aprs_prefix.."000/000g000t"..string.format("%03d", temp*9/5+32).."r000p000h"..string.format("%02d",humi).."b"..string.format("%05d", press)
str = str.."ESP8266 MAC "..wifi.sta.getmac().." RSSI: "..rssi
print(str)
conn = net.createUDPSocket()
conn:send(aprs_port,aprs_host,str)
conn:close()
data_send = true
end
if send_http then
req_url = http_url.."?mac="..wifi.sta.getmac().."&"..string.format("temp=%.1f&humi=%.1f&press=%.2f&rssi=%d",temp,humi,press,rssi)
print("http send "..req_url)
http.get(req_url, nil, function(code, data)
if code < 0 then
print("HTTP req err")
else
print(code, data)
data_send = true
end
end)
end
end
read_error = 0
wifi_error = 0
function func_read_bme280()
count = count + 1
if count*3 >= send_interval then
count = 0
end
P, T = bme280.baro()
H, T = bme280.humi()
if P == nil or H == nil then
print("bme280 read error")
read_error = read_error + 1
if read_error > 100 then
node.restart()
end
return
end
press = P / 100
temp = T / 100
humi = H / 1000
updatedisplay()
if wifi.sta.status() ~= 5 then
wifi_error = wifi_error + 1
if wifi_error > 200 then
node.restart()
end
print("wifi not ready")
blinkled(100)
return
end
rssi = wifi.sta.getrssi()
if rssi == nil then
rssi = -100
end
data_send = false
print("read count="..string.format("%d: t=%.1f, h=%.1f, p=%.1f, rssi=%d, uptime=%d",
count,temp,humi,press,rssi,tmr.time()))
if mqtt_connected then
print("mqtt publish")
m:publish(mqtt_topic, string.format("{\"temperature\": %.1f, \"humidity\": %.1f, \"press\": %.1f, \"rssi\": %d, \"uptime\": %d}",
temp, humi, press, rssi, tmr.time()),0,0)
data_send = true
elseif send_mqtt then
print("mqtt try connect to "..mqtt_host..":"..mqtt_port)
mqtt_connect()
end
if count == 4 then
send_data()
end
if data_send then
wifi_error = 0
blinkled(500)
else
wifi_error = wifi_error + 1
if wifi_error > 200 then
node.restart()
end
blinkled(100)
end
end
if send_interval < 15 then
send_interval = 15
elseif send_interval > 300 then
send_interval = 300
end
if send_mqtt then
print("init mqtt ChipID"..node.chipid().." "..mqtt_user.." "..mqtt_password)
_, reset_reason = node.bootreason()
if reset_reason == nil then reset_reason = 255 end
m = mqtt.Client("ESP8266Sensor_"..node.chipid()..string.format("_%d",reset_reason),180,mqtt_user,mqtt_password)
if mqtt_update then
m:on("message",function(conn, topic, data)
if data ~= nil then
print(topic .. ": " .. data)
if data == "update" then
print("reboot into update mode")
file.open("update.txt","w")
file.close()
node.restart()
end
end
end)
end
m:on("offline", function(c)
print("mqtt offline, try connect to "..mqtt_host..":"..mqtt_port)
mqtt_connected = false
mqtt_connect()
end)
end
print("My MAC is: "..wifi.sta.getmac())
print("Trying AP...")
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)
wifi.setmode(wifi.STATION)
wifi.sta.config({ssid=wifi_ssid, pwd=wifi_password})
wifi.sta.autoconnect(1)
wifi.sta.connect()
flashkeypressed = false
function flashkeypress()
if flashkeypressed then
return
end
flashkeypressed = true
print("flash key pressed, boot into config mode")
file.open("flashkey.txt","w")
file.close()
end
gpio.mode(3, gpio.INPUT, gpio.PULLUP)
gpio.trig(3, "low", flashkeypress)
tmr.alarm(1,3000,tmr.ALARM_AUTO,func_read_bme280)