forked from github/esp8266-geigercounter
Code format
This commit is contained in:
parent
4823f0a36b
commit
e06c587b19
1 changed files with 57 additions and 55 deletions
|
@ -18,6 +18,8 @@ float lastuSv = 0, currentuSv = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
||||||
|
|
||||||
|
@ -44,24 +46,24 @@ void setup() {
|
||||||
void updateRadiationValues() {
|
void updateRadiationValues() {
|
||||||
|
|
||||||
|
|
||||||
char tmp[8];
|
char tmp[8];
|
||||||
|
|
||||||
if(currentCPM != lastCPM) {
|
if (currentCPM != lastCPM) {
|
||||||
String(currentCPM).toCharArray(tmp, 8);
|
String(currentCPM).toCharArray(tmp, 8);
|
||||||
Serial.print("Sending CPM");
|
Serial.print("Sending CPM");
|
||||||
Serial.println(tmp);
|
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);
|
String(currentuSv).toCharArray(tmp, 8);
|
||||||
Serial.print("Sending uSv");
|
Serial.print("Sending uSv");
|
||||||
Serial.println(tmp);
|
Serial.println(tmp);
|
||||||
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
|
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
lastCPM = currentCPM;
|
lastCPM = currentCPM;
|
||||||
lastuSv = currentuSv;
|
lastuSv = currentuSv;
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectMqtt() {
|
void connectMqtt() {
|
||||||
|
@ -77,7 +79,7 @@ void connectMqtt() {
|
||||||
newConnection = true;
|
newConnection = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(newConnection) {
|
if (newConnection) {
|
||||||
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
|
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,45 +87,45 @@ 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, delimiter);
|
||||||
|
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
|
|
||||||
switch(segment) {
|
switch (segment) {
|
||||||
|
|
||||||
// This is just for validation
|
// This is just for validation
|
||||||
case IDX_CPS_KEY: if (strcmp(token, "CPS") != 0) return; break;
|
case IDX_CPS_KEY: if (strcmp(token, "CPS") != 0) return; break;
|
||||||
case IDX_CPM_KEY: if (strcmp(token, "CPM") != 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_uSv_KEY: if (strcmp(token, "uSv/hr") != 0) return; break;
|
||||||
|
|
||||||
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 = String(token).toInt();
|
||||||
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 = String(token).toFloat();
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if(segment > 7) {
|
|
||||||
// Invalid! There should be no more than 7 segments
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
token = strtok(NULL, delimiter);
|
|
||||||
segment++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentuSv = uSv;
|
if (segment > 7) {
|
||||||
currentCPM = cpm;
|
// Invalid! There should be no more than 7 segments
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
token = strtok(NULL, delimiter);
|
||||||
|
segment++;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentuSv = uSv;
|
||||||
|
currentCPM = cpm;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -145,7 +147,7 @@ void loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (serialInput.length() > RECV_LINE_SIZE + 10) {
|
||||||
serialInput = "";
|
serialInput = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue