-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairsensor.ino
384 lines (306 loc) · 7.99 KB
/
airsensor.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#include <Adafruit_BME280.h>
#include <Wire.h>
#include <SSD1306.h>
#include "settings.h"
const int baudrate = 115200;
#define SDA D3
#define SDC D4
#ifdef MQTT
WiFiClient espClient;
PubSubClient client(espClient);
#endif
SSD1306 display(0x3c, SDC, SDA);
bool has_display;
// BME280, Luftdruck-Sensor
Adafruit_BME280 bme280;
struct {
bool read;
bool valid;
int ppm;
} zh18_result = { .read = true };
struct {
bool read;
bool valid;
float t; // Temperature
float h; // Humidity
float p; // Pressure
} bme280_result = { .read = true };
char hostString[20] = {0};
// All timestamps/periods in milliseconds
unsigned long uptime = 0;
unsigned long last_send = 0;
const unsigned long sending_interval = 10*1000;
void debugf(char *fmt, ... ){
char buf[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(buf, 128, fmt, args);
va_end (args);
Serial.print(buf);
}
void debugf_float(char *fmt, float val){
char buf[9];
dtostrf(val, 6, 2, buf);
debugf(fmt, buf);
}
String Float2String(const float value) {
// Convert a float to String with two decimals.
char temp[12];
String s;
dtostrf(value, 8, 2, temp);
s = String(temp);
s.trim();
return s;
}
bool initBME280(char addr) {
debugf("[bme280] Trying on 0x%02X ... ", addr);
if (bme280.begin(addr)) {
debugf("found\n");
return true;
} else {
debugf("not found\n");
return false;
}
}
void readBME280() {
bme280_result.t = bme280.readTemperature();
bme280_result.h = bme280.readHumidity();
bme280_result.p = bme280.readPressure();
bme280_result.valid = !isnan(bme280_result.t) && !isnan(bme280_result.h) && !isnan(bme280_result.p);
if (bme280_result.valid) {
debugf_float("Temperature: %s C\n", bme280_result.t);
debugf_float("Humidity: %s %%\n", bme280_result.h);
debugf_float("Pressure: %s hPa\n", bme280_result.p/100);
} else {
debugf("[bme280] reading failed\n");
}
}
void readZH18()
{
zh18_result.valid = false;
// command to ask for data
const byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
char response[9]; // for answer
Serial.flush();
delay(100);
Serial.begin(9600);
Serial.swap();
Serial.write(cmd, sizeof(cmd));
int read = Serial.readBytes(response, sizeof(response));
Serial.swap();
Serial.begin(baudrate);
if (read != sizeof(response)) {
if (!read){
Serial.println("[zh18] no bytes received");
} else {
Serial.print("[zh18] received ");
Serial.print(read);
Serial.println(" bytes");
}
return;
}
if (response[0] != 0xFF)
{
Serial.println("[zh18] Wrong starting byte received");
return;
}
if (response[1] != 0x86)
{
Serial.println("[zh18] Wrong command received");
return;
}
char checksum = 0;
for (char i = 1; i < 8; i++)
{
checksum += response[i];
}
checksum = 0xff - checksum + 1;
if (checksum != response[8])
{
Serial.printf("[zh18] Checksum invalid: expected=%02x is=%02x\n", checksum, response[8]);
return;
}
bool preheating = uptime < 1000*60*3; // up to three minutes preheat time
zh18_result.ppm = response[3] | response[2] << 8;
zh18_result.valid = !preheating || (preheating && zh18_result.ppm != 400);
debugf("CO2: %04d ppm\n", zh18_result.ppm);
}
char* Statuses[] = {
"Idle",
"SSID not available",
"Scan completed",
"Connected",
"Connect failed",
"Connection lost",
"Disconnected"
};
void displayWifiStatus(String status) {
if (!has_display)
return;
display.clear();
display.drawString(0, 0, hostString);
display.drawString(0, 10, String("SSID: ") + wifi_ssid);
if (status != "")
display.drawString(0, 20, status);
display.display();
}
void connectWifi() {
Serial.printf("Connecting to %s ", wifi_ssid);
displayWifiStatus("");
WiFi.begin(wifi_ssid, wifi_pass);
while (true) {
Serial.print(".");
int status;
int new_status = WiFi.status();
if (new_status != status) {
status = new_status;
Serial.printf("\nWifi status: %s\n", Statuses[status]);
displayWifiStatus(Statuses[status]);
}
if (status == WL_CONNECTED)
break;
if (status == WL_NO_SSID_AVAIL) {
delay(3000);
ESP.restart();
}
delay(100);
}
Serial.println(" done");
}
void initWifi() {
int status = -1;
WiFi.hostname(hostString);
WiFi.softAPdisconnect(true);
ESP.wdtEnable(1000);
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Wifi already connected");
} else {
connectWifi();
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void displayData() {
display.clear();
if (bme280_result.valid) {
display.drawString(0, 0, String("Temperature: ") + Float2String(bme280_result.t) + String(" °C"));
display.drawString(0, 10, String("Humidity: ") + Float2String(bme280_result.h) + String(" %"));
display.drawString(0, 20, String("Pressure: ") + Float2String(bme280_result.p/100) + String(" hPa"));
}
if (zh18_result.valid) {
display.drawString(0, 40, String("CO2: ") + zh18_result.ppm + String(" ppm"));
}else{
display.drawString(0, 40, String("CO2: -"));
}
display.display();
}
void sendData() {
String data, url;
// Build data
data = "";
data += F("sensors,host=");
data += hostString;
data += F(",location=");
data += location;
data += " ";
if (bme280_result.valid) {
data += F("temperature=");
data += Float2String(bme280_result.t);
data += F(",humidity=");
data += Float2String(bme280_result.h);
data += F(",pressure=");
data += Float2String(bme280_result.p);
data += F(",");
}
if (zh18_result.valid) {
data += F("co2=");
data += Float2String(zh18_result.ppm);
data += F(",");
}
if (!data.endsWith(",")){
debugf("No data available\n");
return;
}
data += F("uptime=");
data += String(uptime/1000);
// Build URL
url = String(influx_url);
url += F("write?precision=s&db=");
url += influx_database;
#ifdef MQTT
// Send data to MQTT
client.publish(mqtt_topic, String(data).c_str(), true);
#endif
// Send data to InfluxDB
HTTPClient http;
http.begin(url);
http.setAuthorization(influx_user, influx_pass);
http.addHeader("Content-Type", "text/plain");
int status = http.POST(data);
if (status != 204) {
Serial.printf("[http] unexpected status code: %d\n", status);
http.writeToStream(&Serial);
}
http.end();
}
void initDisplay(){
Wire.beginTransmission (0x3C);
if (Wire.endTransmission () != 0) {
has_display = false;
Serial.printf("[display] not found on: 0x3c\n");
}
Serial.printf("[display] found on: 0x3c\n");
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
has_display = true;
}
void setup() {
Serial.begin(baudrate);
Wire.pins(SDC, SDA);
Wire.begin(SDC, SDA);
initDisplay();
sprintf(hostString, "esp8266-%06X", ESP.getChipId());
Serial.printf("\nHostname: %s\n", hostString);
if (has_display){
display.clear();
display.drawString(0, 0, "Booting ...");
display.drawString(0, 10, hostString);
display.display();
}
// Serial.setDebugOutput(true);
initWifi();
#ifdef MQTT
client.setServer(mqtt_server, 1883);
#endif
// BME280 initialisieren
if (bme280_result.read) {
if (!initBME280(0x76) && !initBME280(0x77)) {
debugf("Check BME280 wiring\n");
bme280_result.read = false;
}
}
delay(2500);
}
void loop() {
uptime = millis();
// uptime restarted at zero? (overflow after 50 days)
if (last_send > uptime)
last_send = 0;
// Sending now?
if (last_send == 0 || last_send + sending_interval < uptime){
debugf("\nreading sensors ...\n");
debugf("Uptime: %04d\n", uptime/1000);
if (bme280_result.read) readBME280();
if (zh18_result.read) readZH18();
if (has_display) displayData();
if (WiFi.status() != WL_CONNECTED)
initWifi();
sendData();
last_send = uptime;
debugf("done\n");
}
}