-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patht6613-esp8266.ino
75 lines (54 loc) · 1.49 KB
/
t6613-esp8266.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
#include <stdio.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
WiFiClient wifi;
PubSubClient mqtt(wifi);
char id[9];
char debugtopic[128];
char sensortopic[128];
void setup() {
Serial.begin(19200);
WiFi.begin("revspace-dingen", "$GeheimWachtwoord");
mqtt.setServer("mosquitto.space.revspace.nl", 1883);
sprintf(id, "%06x", ESP.getChipId());
sprintf(debugtopic, "revdebug/co2/%06x", ESP.getChipId());
sprintf(sensortopic, "revspace/sensors/co2/%06x", ESP.getChipId());
}
void loop() {
int co2 = 0;
while (!mqtt.connected()) {
mqtt.connect(id);
mqtt.publish(debugtopic, "connected");
delay(10);
}
mqtt.loop();
if (WiFi.status() != WL_CONNECTED) ESP.restart();
Serial.write("\xff\xfe\x02\x02\x03");
delay(400);
char buf[5];
for (int i = 0; i < 5; i++) {
unsigned int timeout = millis() + 1000;
while (! Serial.available()) {
if (millis() > timeout) {
mqtt.publish(debugtopic, "No response from sensor, restarting");
delay(1000);
ESP.reset(); // XXX this sometimes hangs :(
}
}
buf[i] = Serial.read();
}
if (buf[0] != 0xFF || buf[1] != 0xFA || buf[2] != 2) {
mqtt.publish(debugtopic, "Weird output from sensor, restarting");
delay(1000);
ESP.reset();
}
co2 = 256 * buf[3] + buf[4];
char message[128];
sprintf(message, "%d PPM", co2);
if (co2) {
mqtt.publish(sensortopic, message, 1);
} else {
mqtt.publish(debugtopic, "No reading yet");
}
delay(4000);
}