From b6d40408e0129340de92653e8ac7bf36a78ee3ab Mon Sep 17 00:00:00 2001 From: schinken Date: Wed, 23 Dec 2015 22:49:57 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + GeigerCounter.ino | 159 ++++++++++++++++++++++++++++++++++++++++++++++ settings.h | 21 ++++++ 3 files changed, 181 insertions(+) create mode 100644 .gitignore create mode 100644 GeigerCounter.ino create mode 100644 settings.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42202cd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +wifi.h diff --git a/GeigerCounter.ino b/GeigerCounter.ino new file mode 100644 index 0000000..0393a9a --- /dev/null +++ b/GeigerCounter.ino @@ -0,0 +1,159 @@ +#include +#include +#include +#include + +#include "settings.h" +#include "wifi.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() { + + Serial.begin(115200); + geigerCounterSerial.begin(BAUD_GEIGERCOUNTER); + + 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()); + + timer.setInterval(300000, updateRadiationValues); +} + +void updateRadiationValues() { + + + 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); + } + + lastCPM = currentCPM; + lastuSv = currentuSv; +} + +void connectMqtt() { + + bool newConnection = false; + while (!mqttClient.connected()) { + mqttClient.setClient(wifiClient); + mqttClient.setServer(mqttHost, 1883); + mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected"); + + delay(1000); + newConnection = true; + } + + if(newConnection) { + mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true); + } + +} + +void updateMqttTopics(int cpm, float uSv) { + + +} + +void parseReceivedLine(char* input) { + + 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; + } + + if(segment > 7) { + // Invalid! There should be no more than 7 segments + return; + } + + token = strtok(NULL, delimiter); + segment++; + } + + currentuSv = uSv; + currentCPM = cpm; +} + +void loop() { + + 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 + if(serialInput.length() > RECV_LINE_SIZE + 10) { + serialInput = ""; + } + + Serial.write(in); + } +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..047d587 --- /dev/null +++ b/settings.h @@ -0,0 +1,21 @@ +#define IDX_CPS_KEY 0 +#define IDX_CPM_KEY 2 +#define IDX_uSv_KEY 4 + +#define IDX_CPM 3 +#define IDX_uSv 5 +#define IDX_MODE 6 + +#define RECV_LINE_SIZE 37 + +#define PIN_UART_RX 2 // 4 +#define PIN_UART_TX 13 // UNUSED + +#define BAUD_GEIGERCOUNTER 9600 + +#define MQTT_TOPIC_CPM "sensor/radiation/cpm" +#define MQTT_TOPIC_USV "sensor/radiation/uSv" +#define MQTT_TOPIC_LAST_WILL "state/sensor/geigercounter" + +const char* mqttHost = "mqtt.core.bckspc.de"; +const char* delimiter = ", ";