mirror of
https://github.com/schinken/esp8266-geigercounter.git
synced 2024-11-21 16:10:20 +01:00
Cleanup code; Don't use String, SimpleTimer anymore
This commit is contained in:
parent
975ba1cd34
commit
9217483abc
2 changed files with 42 additions and 48 deletions
|
@ -2,28 +2,27 @@
|
|||
#include <PubSubClient.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include <ArduinoOTA.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];
|
||||
uint8_t idx = 0;
|
||||
char buffer[64];
|
||||
|
||||
int lastCPM = 0, currentCPM = 0;
|
||||
float lastuSv = 0, currentuSv = 0;
|
||||
unsigned long lastPublishMs = 0;
|
||||
|
||||
|
||||
void setup() {
|
||||
|
||||
// power up wait
|
||||
// power up wait
|
||||
delay(3000);
|
||||
|
||||
Serial.begin(115200);
|
||||
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
||||
geigerCounterSerial.begin(9600);
|
||||
delay(10);
|
||||
|
||||
WiFi.hostname(WIFI_HOSTNAME);
|
||||
|
@ -42,25 +41,18 @@ void setup() {
|
|||
ArduinoOTA.setPassword(OTA_PASSWORD);
|
||||
ArduinoOTA.begin();
|
||||
|
||||
timer.setInterval(UPDATE_INTERVAL_SECONDS * 1000L, updateRadiationValues);
|
||||
}
|
||||
|
||||
void updateRadiationValues() {
|
||||
|
||||
|
||||
void publishValues() {
|
||||
char tmp[8];
|
||||
|
||||
if (currentCPM != lastCPM) {
|
||||
String(currentCPM).toCharArray(tmp, 8);
|
||||
Serial.print("Sending CPM");
|
||||
Serial.println(tmp);
|
||||
sprintf(tmp, "%d", currentCPM);
|
||||
mqttClient.publish(MQTT_TOPIC_CPM, tmp, true);
|
||||
}
|
||||
|
||||
if (currentuSv != lastuSv) {
|
||||
String(currentuSv).toCharArray(tmp, 8);
|
||||
Serial.print("Sending uSv");
|
||||
Serial.println(tmp);
|
||||
sprintf(tmp, "%.2f", currentuSv);
|
||||
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
|
||||
}
|
||||
|
||||
|
@ -73,7 +65,9 @@ void connectMqtt() {
|
|||
while (!mqttClient.connected()) {
|
||||
if (mqttClient.connect(WIFI_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);
|
||||
}
|
||||
}
|
||||
|
@ -81,14 +75,13 @@ void connectMqtt() {
|
|||
}
|
||||
|
||||
void parseReceivedLine(char* input) {
|
||||
|
||||
char segment = 0;
|
||||
char *token;
|
||||
|
||||
float uSv = 0;
|
||||
float cpm = 0;
|
||||
|
||||
token = strtok(input, delimiter);
|
||||
token = strtok(input, ", ");
|
||||
|
||||
while (token != NULL) {
|
||||
|
||||
|
@ -101,12 +94,12 @@ void parseReceivedLine(char* input) {
|
|||
|
||||
case IDX_CPM:
|
||||
Serial.printf("Current CPM: %s\n", token);
|
||||
cpm = String(token).toInt();
|
||||
cpm = atoi(token);
|
||||
break;
|
||||
|
||||
case IDX_uSv:
|
||||
Serial.printf("Current uSv/hr: %s\n", token);
|
||||
uSv = String(token).toFloat();
|
||||
uSv = atof(token);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -115,7 +108,7 @@ void parseReceivedLine(char* input) {
|
|||
return;
|
||||
}
|
||||
|
||||
token = strtok(NULL, delimiter);
|
||||
token = strtok(NULL, ", ");
|
||||
segment++;
|
||||
}
|
||||
|
||||
|
@ -126,27 +119,30 @@ void parseReceivedLine(char* input) {
|
|||
void loop() {
|
||||
|
||||
connectMqtt();
|
||||
timer.run();
|
||||
mqttClient.loop();
|
||||
|
||||
if (millis() - lastPublishMs > MQTT_PUBLISH_INTERVAL_MS) {
|
||||
lastPublishMs = millis();
|
||||
publishValues();
|
||||
}
|
||||
|
||||
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') {
|
||||
serialInput.toCharArray(serialInputHelper, RECV_LINE_SIZE);
|
||||
parseReceivedLine(serialInputHelper);
|
||||
|
||||
serialInput = "";
|
||||
if (input == '\n') {
|
||||
parseReceivedLine(buffer);
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
// Just in case the buffer gets to big, start from scratch
|
||||
if (serialInput.length() > RECV_LINE_SIZE + 10) {
|
||||
serialInput = "";
|
||||
if (idx > 42) {
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
Serial.write(in);
|
||||
}
|
||||
|
||||
ArduinoOTA.handle();
|
||||
|
|
|
@ -6,24 +6,22 @@
|
|||
#define IDX_uSv 5
|
||||
#define IDX_MODE 6
|
||||
|
||||
#define RECV_LINE_SIZE 37
|
||||
|
||||
#define PIN_UART_RX 0 // 4
|
||||
#define PIN_UART_RX D1 // 4
|
||||
#define PIN_UART_TX 13 // UNUSED
|
||||
|
||||
#define UPDATE_INTERVAL_SECONDS 300L
|
||||
|
||||
#define BAUD_GEIGERCOUNTER 9600
|
||||
|
||||
const char* HOSTNAME = "ESP-GeigerCounter";
|
||||
const char* WIFI_SSID = "----";
|
||||
const char* WIFI_PASSWORD = "----";
|
||||
const char* WIFI_HOSTNAME = "ESP-GeigerCounter";
|
||||
const char* WIFI_SSID = "xxxxxxxxxx";
|
||||
const char* WIFI_PASSWORD = "xxxxxxxxxxx";
|
||||
|
||||
const char* OTA_PASSWORD = "foobar";
|
||||
|
||||
#define MQTT_TOPIC_CPM "sensor/radiation/cpm"
|
||||
#define MQTT_TOPIC_USV "sensor/radiation/uSv"
|
||||
#define MQTT_TOPIC_LAST_WILL "state/sensor/geigercounter"
|
||||
#define MQTT_HOST "mqtt.xxxxxxxxx.org"
|
||||
|
||||
const char* mqttHost = "mqtt.core.bckspc.de";
|
||||
const char* delimiter = ", ";
|
||||
#define MQTT_TOPIC_CPM "sensor/geigercounter/cpm"
|
||||
#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)
|
Loading…
Reference in a new issue