2015-12-23 22:49:57 +01:00
|
|
|
#include <ESP8266WiFi.h>
|
2020-09-26 17:34:24 +02:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <SoftwareSerial.h>
|
2022-03-15 16:29:17 +01:00
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <SimpleTimer.h>
|
2015-12-23 22:49:57 +01:00
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
WiFiClient wifiClient;
|
|
|
|
PubSubClient mqttClient;
|
|
|
|
SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX);
|
2022-03-15 16:29:17 +01:00
|
|
|
SimpleTimer timer;
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
String serialInput = "";
|
|
|
|
char serialInputHelper[RECV_LINE_SIZE];
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
int lastCPM = 0, currentCPM = 0;
|
|
|
|
float lastuSv = 0, currentuSv = 0;
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2020-06-28 14:36:37 +02:00
|
|
|
void setup() {
|
2017-03-11 11:39:51 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
// power up wait
|
|
|
|
delay(3000);
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
|
|
|
delay(10);
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
WiFi.hostname(WIFI_HOSTNAME);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
Serial.print(".");
|
|
|
|
delay(500);
|
|
|
|
}
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
mqttClient.setClient(wifiClient);
|
|
|
|
mqttClient.setServer(MQTT_HOST, 1883);
|
|
|
|
|
|
|
|
ArduinoOTA.setHostname(WIFI_HOSTNAME);
|
|
|
|
ArduinoOTA.setPassword(OTA_PASSWORD);
|
|
|
|
ArduinoOTA.begin();
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
timer.setInterval(UPDATE_INTERVAL_SECONDS * 1000L, updateRadiationValues);
|
|
|
|
}
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
void updateRadiationValues() {
|
2020-06-28 15:58:05 +02:00
|
|
|
|
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
char tmp[8];
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
if (currentCPM != lastCPM) {
|
|
|
|
String(currentCPM).toCharArray(tmp, 8);
|
|
|
|
Serial.print("Sending CPM");
|
|
|
|
Serial.println(tmp);
|
|
|
|
mqttClient.publish(MQTT_TOPIC_CPM, tmp, true);
|
|
|
|
}
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
if (currentuSv != lastuSv) {
|
|
|
|
String(currentuSv).toCharArray(tmp, 8);
|
|
|
|
Serial.print("Sending uSv");
|
|
|
|
Serial.println(tmp);
|
|
|
|
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
|
|
|
|
}
|
2020-09-26 17:34:24 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
lastCPM = currentCPM;
|
|
|
|
lastuSv = currentuSv;
|
2015-12-23 22:49:57 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
void connectMqtt() {
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
while (!mqttClient.connected()) {
|
|
|
|
if (mqttClient.connect(WIFI_HOSTNAME, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) {
|
|
|
|
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
|
|
|
|
} else {
|
|
|
|
delay(1000);
|
|
|
|
}
|
2020-09-26 17:34:24 +02:00
|
|
|
}
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2020-09-26 17:34:24 +02:00
|
|
|
}
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
void parseReceivedLine(char* input) {
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
char segment = 0;
|
|
|
|
char *token;
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
float uSv = 0;
|
|
|
|
float cpm = 0;
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
token = strtok(input, delimiter);
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
while (token != NULL) {
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
switch (segment) {
|
2020-06-28 14:36:37 +02:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
// 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;
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
case IDX_CPM:
|
|
|
|
Serial.printf("Current CPM: %s\n", token);
|
|
|
|
cpm = String(token).toInt();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDX_uSv:
|
|
|
|
Serial.printf("Current uSv/hr: %s\n", token);
|
|
|
|
uSv = String(token).toFloat();
|
|
|
|
break;
|
2020-06-28 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
if (segment > 7) {
|
|
|
|
// Invalid! There should be no more than 7 segments
|
|
|
|
return;
|
|
|
|
}
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
token = strtok(NULL, delimiter);
|
|
|
|
segment++;
|
|
|
|
}
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
currentuSv = uSv;
|
|
|
|
currentCPM = cpm;
|
|
|
|
}
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
void loop() {
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
connectMqtt();
|
|
|
|
timer.run();
|
|
|
|
mqttClient.loop();
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
if (geigerCounterSerial.available()) {
|
|
|
|
char in = (char) geigerCounterSerial.read();
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
serialInput += in;
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
if (in == '\n') {
|
|
|
|
serialInput.toCharArray(serialInputHelper, RECV_LINE_SIZE);
|
|
|
|
parseReceivedLine(serialInputHelper);
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
serialInput = "";
|
|
|
|
}
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
// Just in case the buffer gets to big, start from scratch
|
|
|
|
if (serialInput.length() > RECV_LINE_SIZE + 10) {
|
|
|
|
serialInput = "";
|
|
|
|
}
|
2015-12-23 22:49:57 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
Serial.write(in);
|
|
|
|
}
|
2016-02-20 22:21:44 +01:00
|
|
|
|
2022-03-15 16:29:17 +01:00
|
|
|
ArduinoOTA.handle();
|
2020-06-28 15:58:05 +02:00
|
|
|
}
|