Cleanup code; Don't use String, SimpleTimer anymore

This commit is contained in:
schinken 2022-03-15 17:40:28 +01:00
parent 975ba1cd34
commit 9217483abc
2 changed files with 42 additions and 48 deletions

View file

@ -2,28 +2,27 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <SimpleTimer.h>
#include "settings.h" #include "settings.h"
WiFiClient wifiClient; WiFiClient wifiClient;
PubSubClient mqttClient; PubSubClient mqttClient;
SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX); SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX);
SimpleTimer timer;
String serialInput = ""; uint8_t idx = 0;
char serialInputHelper[RECV_LINE_SIZE]; char buffer[64];
int lastCPM = 0, currentCPM = 0; int lastCPM = 0, currentCPM = 0;
float lastuSv = 0, currentuSv = 0; float lastuSv = 0, currentuSv = 0;
unsigned long lastPublishMs = 0;
void setup() { void setup() {
// power up wait // power up wait
delay(3000); delay(3000);
Serial.begin(115200); Serial.begin(115200);
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER); geigerCounterSerial.begin(9600);
delay(10); delay(10);
WiFi.hostname(WIFI_HOSTNAME); WiFi.hostname(WIFI_HOSTNAME);
@ -42,25 +41,18 @@ void setup() {
ArduinoOTA.setPassword(OTA_PASSWORD); ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.begin(); ArduinoOTA.begin();
timer.setInterval(UPDATE_INTERVAL_SECONDS * 1000L, updateRadiationValues);
} }
void updateRadiationValues() { void publishValues() {
char tmp[8]; char tmp[8];
if (currentCPM != lastCPM) { if (currentCPM != lastCPM) {
String(currentCPM).toCharArray(tmp, 8); sprintf(tmp, "%d", currentCPM);
Serial.print("Sending CPM");
Serial.println(tmp);
mqttClient.publish(MQTT_TOPIC_CPM, tmp, true); mqttClient.publish(MQTT_TOPIC_CPM, tmp, true);
} }
if (currentuSv != lastuSv) { if (currentuSv != lastuSv) {
String(currentuSv).toCharArray(tmp, 8); sprintf(tmp, "%.2f", currentuSv);
Serial.print("Sending uSv");
Serial.println(tmp);
mqttClient.publish(MQTT_TOPIC_USV, tmp, true); mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
} }
@ -73,7 +65,9 @@ void connectMqtt() {
while (!mqttClient.connected()) { while (!mqttClient.connected()) {
if (mqttClient.connect(WIFI_HOSTNAME, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) { if (mqttClient.connect(WIFI_HOSTNAME, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) {
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true); mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
Serial.println("MQTT connected");
} else { } else {
Serial.println("MQTT connect failed");
delay(1000); delay(1000);
} }
} }
@ -81,14 +75,13 @@ void connectMqtt() {
} }
void parseReceivedLine(char* input) { void parseReceivedLine(char* input) {
char segment = 0; char segment = 0;
char *token; char *token;
float uSv = 0; float uSv = 0;
float cpm = 0; float cpm = 0;
token = strtok(input, delimiter); token = strtok(input, ", ");
while (token != NULL) { while (token != NULL) {
@ -101,12 +94,12 @@ void parseReceivedLine(char* input) {
case IDX_CPM: case IDX_CPM:
Serial.printf("Current CPM: %s\n", token); Serial.printf("Current CPM: %s\n", token);
cpm = String(token).toInt(); cpm = atoi(token);
break; break;
case IDX_uSv: case IDX_uSv:
Serial.printf("Current uSv/hr: %s\n", token); Serial.printf("Current uSv/hr: %s\n", token);
uSv = String(token).toFloat(); uSv = atof(token);
break; break;
} }
@ -115,7 +108,7 @@ void parseReceivedLine(char* input) {
return; return;
} }
token = strtok(NULL, delimiter); token = strtok(NULL, ", ");
segment++; segment++;
} }
@ -126,27 +119,30 @@ void parseReceivedLine(char* input) {
void loop() { void loop() {
connectMqtt(); connectMqtt();
timer.run();
mqttClient.loop(); mqttClient.loop();
if (millis() - lastPublishMs > MQTT_PUBLISH_INTERVAL_MS) {
lastPublishMs = millis();
publishValues();
}
if (geigerCounterSerial.available()) { if (geigerCounterSerial.available()) {
char in = (char) geigerCounterSerial.read(); char input = geigerCounterSerial.read();
buffer[idx++] = input;
serialInput += in; // Echo Serial-Data from GeigerCounter to USB-Debug
Serial.write(input);
if (in == '\n') { if (input == '\n') {
serialInput.toCharArray(serialInputHelper, RECV_LINE_SIZE); parseReceivedLine(buffer);
parseReceivedLine(serialInputHelper); idx = 0;
serialInput = "";
} }
// Just in case the buffer gets to big, start from scratch // Just in case the buffer gets to big, start from scratch
if (serialInput.length() > RECV_LINE_SIZE + 10) { if (idx > 42) {
serialInput = ""; idx = 0;
} }
Serial.write(in);
} }
ArduinoOTA.handle(); ArduinoOTA.handle();

View file

@ -6,24 +6,22 @@
#define IDX_uSv 5 #define IDX_uSv 5
#define IDX_MODE 6 #define IDX_MODE 6
#define RECV_LINE_SIZE 37 #define PIN_UART_RX D1 // 4
#define PIN_UART_RX 0 // 4
#define PIN_UART_TX 13 // UNUSED #define PIN_UART_TX 13 // UNUSED
#define UPDATE_INTERVAL_SECONDS 300L
#define BAUD_GEIGERCOUNTER 9600 #define BAUD_GEIGERCOUNTER 9600
const char* HOSTNAME = "ESP-GeigerCounter"; const char* WIFI_HOSTNAME = "ESP-GeigerCounter";
const char* WIFI_SSID = "----"; const char* WIFI_SSID = "xxxxxxxxxx";
const char* WIFI_PASSWORD = "----"; const char* WIFI_PASSWORD = "xxxxxxxxxxx";
const char* OTA_PASSWORD = "foobar"; const char* OTA_PASSWORD = "foobar";
#define MQTT_TOPIC_CPM "sensor/radiation/cpm" #define MQTT_HOST "mqtt.xxxxxxxxx.org"
#define MQTT_TOPIC_USV "sensor/radiation/uSv"
#define MQTT_TOPIC_LAST_WILL "state/sensor/geigercounter"
const char* mqttHost = "mqtt.core.bckspc.de"; #define MQTT_TOPIC_CPM "sensor/geigercounter/cpm"
const char* delimiter = ", "; #define MQTT_TOPIC_USV "sensor/geigercounter/uSv"
#define MQTT_TOPIC_LAST_WILL "sensor/geigercounter/status"
// Only publishes values if changed since last publish
#define MQTT_PUBLISH_INTERVAL_MS (10 * 1000)