-
Notifications
You must be signed in to change notification settings - Fork 5
/
esp8266-geigercounter.ino
172 lines (132 loc) · 3.45 KB
/
esp8266-geigercounter.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include "settings.h"
#ifdef OTA_PASSWORD
#include <ArduinoOTA.h>
#endif
WiFiClient wifiClient;
PubSubClient mqttClient;
SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX);
uint8_t idx = 0;
char buffer[64];
char hostname[24];
int lastCPM = 0, currentCPM = 0;
float lastuSv = 0, currentuSv = 0;
unsigned long lastPublishMs = 0;
void setup() {
// power up wait
delay(3000);
Serial.begin(115200);
geigerCounterSerial.begin(9600);
delay(10);
#ifdef WIFI_HOSTNAME
strncpy(hostname, WIFI_HOSTNAME, sizeof(hostname));
#else
snprintf(hostname, sizeof(hostname), "ESP-GEIGER-%X", ESP.getChipId());
#endif
WiFi.hostname(hostname);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
mqttClient.setClient(wifiClient);
mqttClient.setServer(MQTT_HOST, 1883);
connectMqtt();
#ifdef OTA_PASSWORD
ArduinoOTA.setHostname(hostname);
ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.begin();
#endif
#ifdef PIN_PULSE
pinMode(PIN_PULSE, INPUT);
attachInterrupt(PIN_PULSE, onPulse, RISING);
#endif
}
#ifdef PIN_PULSE
ICACHE_RAM_ATTR void onPulse() {
mqttClient.publish(MQTT_TOPIC_PULSE, "true");
}
#endif
void publishValues() {
char tmp[8];
if (currentCPM != lastCPM) {
sprintf(tmp, "%d", currentCPM);
mqttClient.publish(MQTT_TOPIC_CPM, tmp, true);
}
if (currentuSv != lastuSv) {
sprintf(tmp, "%.2f", currentuSv);
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
}
lastCPM = currentCPM;
lastuSv = currentuSv;
}
void connectMqtt() {
while (!mqttClient.connected()) {
if (mqttClient.connect(hostname, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) {
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
Serial.println("MQTT connected");
} else {
Serial.println("MQTT connect failed");
delay(1000);
}
}
}
void parseReceivedLine(char* input) {
char segment = 0;
char *token;
float uSv = 0;
float cpm = 0;
token = strtok(input, ", ");
while (token != NULL) {
switch (segment) {
// This is just for validation
case IDX_CPS_KEY: if (strcmp(token, "CPS") != 0) return; break;
case IDX_CPM_KEY: if (strcmp(token, "CPM") != 0) return; break;
case IDX_uSv_KEY: if (strcmp(token, "uSv/hr") != 0) return; break;
case IDX_CPM:
Serial.printf("Current CPM: %s\n", token);
cpm = atoi(token);
break;
case IDX_uSv:
Serial.printf("Current uSv/hr: %s\n", token);
uSv = atof(token);
break;
}
if (segment > 7) {
// Invalid! There should be no more than 7 segments
return;
}
token = strtok(NULL, ", ");
segment++;
}
currentuSv = uSv;
currentCPM = cpm;
}
void loop() {
connectMqtt();
mqttClient.loop();
if (millis() - lastPublishMs > MQTT_PUBLISH_INTERVAL_MS) {
lastPublishMs = millis();
publishValues();
}
if (geigerCounterSerial.available()) {
char input = geigerCounterSerial.read();
buffer[idx++] = input;
// Echo Serial-Data from GeigerCounter to USB-Debug
Serial.write(input);
if (input == '\n') {
parseReceivedLine(buffer);
idx = 0;
}
// Just in case the buffer gets to big, start from scratch
if (idx > 42) {
idx = 0;
}
}
#ifdef OTA_PASSWORD
ArduinoOTA.handle();
#endif
}