esp8266-geigercounter/GeigerCounter.ino

157 lines
3.1 KiB
Arduino
Raw Normal View History

2015-12-23 22:49:57 +01:00
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include "settings.h"
WiFiClient wifiClient;
PubSubClient mqttClient;
SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX);
SimpleTimer timer;
String serialInput = "";
char serialInputHelper[RECV_LINE_SIZE];
int lastCPM = 0, currentCPM = 0;
float lastuSv = 0, currentuSv = 0;
void setup() {
2016-02-20 22:21:44 +01:00
WiFi.mode(WIFI_STA);
2015-12-23 22:49:57 +01:00
Serial.begin(115200);
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
2016-02-20 22:21:44 +01:00
2015-12-23 22:49:57 +01:00
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
2015-12-23 23:18:40 +01:00
timer.setInterval(UPDATE_INTERVAL_SECONDS * 1000L, updateRadiationValues);
2015-12-23 22:49:57 +01:00
}
void updateRadiationValues() {
2016-02-20 22:21:44 +01:00
char tmp[8];
if (currentCPM != lastCPM) {
String(currentCPM).toCharArray(tmp, 8);
Serial.print("Sending CPM");
Serial.println(tmp);
mqttClient.publish(MQTT_TOPIC_CPM, tmp, true);
}
if (currentuSv != lastuSv) {
String(currentuSv).toCharArray(tmp, 8);
Serial.print("Sending uSv");
Serial.println(tmp);
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
}
2015-12-23 22:49:57 +01:00
2016-02-20 22:21:44 +01:00
lastCPM = currentCPM;
lastuSv = currentuSv;
2015-12-23 22:49:57 +01:00
}
void connectMqtt() {
2016-02-20 22:21:44 +01:00
bool newConnection = false;
2015-12-23 22:50:48 +01:00
2015-12-23 22:49:57 +01:00
while (!mqttClient.connected()) {
mqttClient.setClient(wifiClient);
mqttClient.setServer(mqttHost, 1883);
mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
2016-02-20 22:21:44 +01:00
2015-12-23 22:49:57 +01:00
delay(1000);
newConnection = true;
}
2016-02-20 22:21:44 +01:00
if (newConnection) {
2015-12-23 22:49:57 +01:00
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
}
}
void parseReceivedLine(char* input) {
2016-02-20 22:21:44 +01:00
char segment = 0;
char *token;
float uSv = 0;
float cpm = 0;
token = strtok(input, delimiter);
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 = String(token).toInt();
break;
case IDX_uSv:
Serial.printf("Current uSv/hr: %s\n", token);
uSv = String(token).toFloat();
break;
2015-12-23 22:49:57 +01:00
}
2016-02-20 22:21:44 +01:00
if (segment > 7) {
// Invalid! There should be no more than 7 segments
return;
}
token = strtok(NULL, delimiter);
segment++;
}
currentuSv = uSv;
currentCPM = cpm;
2015-12-23 22:49:57 +01:00
}
void loop() {
2016-02-20 22:21:44 +01:00
2015-12-23 22:49:57 +01:00
connectMqtt();
timer.run();
mqttClient.loop();
if (geigerCounterSerial.available()) {
char in = (char) geigerCounterSerial.read();
serialInput += in;
if (in == '\n') {
serialInput.toCharArray(serialInputHelper, RECV_LINE_SIZE);
parseReceivedLine(serialInputHelper);
serialInput = "";
}
// Just in case the buffer gets to big, start from scratch
2016-02-20 22:21:44 +01:00
if (serialInput.length() > RECV_LINE_SIZE + 10) {
2015-12-23 22:49:57 +01:00
serialInput = "";
}
Serial.write(in);
2016-02-20 22:21:44 +01:00
}
2015-12-23 22:49:57 +01:00
}