This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalmode.ino
334 lines (262 loc) · 7.19 KB
/
normalmode.ino
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <Tsl2561.h>
#include <MHZ19_uart.h>
// using sodaq_lps22hb ver 1.0.0
#include <Sodaq_LPS22HB.h>
WiFiClient net;
MQTTClient mqttClient;
const int mhz_rx_pin = 12; //Serial rx pin no
const int mhz_tx_pin = 14; //Serial tx pin no
MHZ19_uart mhz19;
Sodaq_LPS22HB lps22hb;
bool useLPS22HB = false;
int counter;
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
// Address 0x76
Tsl2561 Tsl(Wire);
// 最後に取得した温度
float lastTemp;
float lastHumidity;
float lastPressure;
float lastLuxFull;
float lastLuxIr;
int lastPpm;
void readConfig() {
// 設定を読んで
File f = SPIFFS.open(settings, "r");
ssid = f.readStringUntil('\n');
password = f.readStringUntil('\n');
mDNS = f.readStringUntil('\n');
mqttBroker = f.readStringUntil('\n');
mqttName = f.readStringUntil('\n');
f.close();
ssid.trim();
password.trim();
mDNS.trim();
mqttBroker.trim();
mqttName.trim();
Serial.println("SSID: " + ssid);
Serial.println("PASS: " + password);
Serial.println("mDNS: " + mDNS);
Serial.println("MQTT Broker: " + mqttBroker);
Serial.println("MQTT Name : " + mqttName);
}
void initializeWiFi() {
WiFi.begin(ssid.c_str(), password.c_str());
int retryCount = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
retryCount++;
if (retryCount % 10 == 0) {
Serial.println("");
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
setup_ota(mDNS);
Serial.println("Arduino OTA Ready");
Serial.println("MQTT Connect");
mqttClient.begin(mqttBroker.c_str(), net);
Serial.println("MQTT Begin");
while (!mqttClient.connect(mDNS.c_str(), "", "")) { // username and password not support
Serial.print(".");
delay(1000);
}
Serial.println("MQTT Started successfully.");
}
void setup_normal() {
readConfig();
// setupモードに入りやすくするための処理
Serial.println("==== Reset to reconfig start.");
String filename = configured_file;
filename = filename + "tmp";
SPIFFS.rename(configured_file, filename);
WiFi.softAPdisconnect(true);
WiFi.enableAP(false);
initializeWiFi();
// init BME
while(!bme.begin()){
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
delay(500);
// init LPS22HB if found (0x5d)
if (lps22hb.init()) {
Serial.println("Barometric sensor initialization succeeded!");
lps22hb.enableSensor(Sodaq_LPS22HB::OdrOneShot);
useLPS22HB = true;
} else {
useLPS22HB = false;
Serial.println("Barometric sensor initialization failed!");
}
// init TSL2561
Tsl.begin();
if( !Tsl.available() ) {
Serial.println("No Tsl2561 found. Tsl2561 disabled.");
}
// init LPS22HB if found (0x5d)
if (lps22hb.init()) {
Serial.println("Barometric sensor initialization succeeded!");
lps22hb.enableSensor(Sodaq_LPS22HB::OdrOneShot);
useLPS22HB = true;
} else {
useLPS22HB = false;
Serial.println("Barometric sensor initialization failed!");
}
// init MH-Z19B
mhz19.begin(mhz_rx_pin, mhz_tx_pin);
mhz19.setAutoCalibration(false);
int wait = 0;
while( mhz19.isWarming() ) {
Serial.print(wait);
Serial.print(" MH-Z19 now warming up... status:");
Serial.println(mhz19.getStatus());
delay(1000);
wait++;
if (wait > 3) {
Serial.println("warmup timeout continue anyway");
break;
}
}
// reconfig timeout
SPIFFS.rename(filename, configured_file);
Serial.println("=== reconfigure timeout. continue.");
}
void loop_normal() {
// WiFiが繋がってなければ意味がないので接続チェック
checkAndReconnectWifi();
loop_ota();
// MQTT
mqttClient.loop();
delay(10); // <- fixes some issues with WiFi stability
readData();
delay(10000);
}
void mqttPublish(String topic, String value) {
mqttClient.publish("/" + mqttName + "/" + topic, value);
}
void readData() {
if (true) {
readDataBme280();
if (useLPS22HB) {
readDataLPS22HB();
}
// MQTT
char buf[24] = "";
if (lastTemp != NAN) {
sprintf(buf, "%2f", lastTemp);
mqttPublish("temp", buf);
}
if (lastHumidity != NAN) {
sprintf(buf, "%2f", lastHumidity);
mqttPublish("humi", buf);
}
if (lastPressure != NAN) {
sprintf(buf, "%2f", lastPressure);
mqttPublish("pres", buf);
}
} else {
lastTemp = 0;
lastHumidity = 0;
lastPressure = 0;
}
if (Tsl.available()) {
readDataTsl2561();
// MQTT
char buf[24] = "";
sprintf(buf, "%2f", lastLuxFull);
mqttPublish("lux", buf);
sprintf(buf, "%2f", lastLuxIr);
mqttPublish("luxIr", buf);
} else {
lastLuxFull = 0;
lastLuxIr = 0;
}
if (true) {
lastPpm = 0;
lastPpm = mhz19.getPPM();
Serial.printf("MH-Z19B: PPM=");
Serial.print(lastPpm);
Serial.print("\n");
// MQTT: if ppm == -1 , MH-Z19 error.
if (lastPpm > 0) {
char buf[24] = "";
sprintf(buf, "%2f", lastPpm);
mqttPublish("ppm", buf);
}
}
}
void readDataLPS22HB() {
float tempPres(NAN);
tempPres = lps22hb.readPressureHPA();
if (tempPres != 0) {
lastPressure = tempPres;
Serial.print("Pressure (by LPS22HB): ");
Serial.print(tempPres, 2);
Serial.println("hPa");
}
}
void readDataBme280() {
float temp(NAN), hum(NAN), pres(NAN);
// unit: B000 = Pa, B001 = hPa, B010 = Hg, B011 = atm, B100 = bar, B101 = torr, B110 = N/m^2, B111 = psi
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
// Parameters: (float& pressure, float& temp, float& humidity, bool celsius = false, uint8_t pressureUnit = 0x0)
bme.read(pres, temp, hum, tempUnit, presUnit);
pres = pres / 100;
Serial.print("BME280: Temp: ");
Serial.print(temp, 2);
Serial.print("C");
Serial.print("\t\tHumidity: ");
Serial.print(hum, 2);
Serial.print("%");
Serial.print("\t\tPressure: ");
Serial.print(pres, 2);
Serial.println("hPa");
lastTemp = temp;
lastHumidity = hum;
lastPressure = pres;
}
void readDataTsl2561() {
uint8_t id;
uint16_t full = 0;
uint16_t ir = 0;
Tsl.on();
Tsl.setSensitivity(false, Tsl2561::EXP_402);
//delay(16);
//delay(110);
delay(402 + 10);
Tsl.id(id);
Tsl.fullLuminosity(full);
Tsl.irLuminosity(ir);
Serial.printf("TSL2561: at 0x%02x(id=0x%02x) luminosity is %5u (full) and %5u (ir)\n", Tsl.address(), id, full, ir);
lastLuxFull = full;
lastLuxIr = ir;
Tsl.off();
}
void checkAndReconnectWifi() {
if (WiFi.status() == WL_CONNECTED) {
return;
}
Serial.print("WiFi is down. restart");
WiFi.disconnect();
int retryCount = 0;
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
retryCount++;
if (retryCount % 15 == 0) {
retryCount = 0;
WiFi.disconnect();
WiFi.begin(ssid.c_str(), password.c_str());
Serial.println("");
Serial.print("Still reconnecting WiFi");
}
}
Serial.println("");
Serial.println("WiFi reconnected.");
}