Code format

This commit is contained in:
schinken 2016-02-20 22:21:44 +01:00
parent 4823f0a36b
commit e06c587b19

View file

@ -17,10 +17,12 @@ int lastCPM = 0, currentCPM = 0;
float lastuSv = 0, currentuSv = 0; 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);
delay(10); delay(10);
Serial.print("Connecting to "); Serial.print("Connecting to ");
@ -43,91 +45,91 @@ void setup() {
void 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) { char tmp[8];
String(currentuSv).toCharArray(tmp, 8);
Serial.print("Sending uSv");
Serial.println(tmp);
mqttClient.publish(MQTT_TOPIC_USV, tmp, true);
}
lastCPM = currentCPM; if (currentCPM != lastCPM) {
lastuSv = currentuSv; 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() { void connectMqtt() {
bool newConnection = false; bool newConnection = false;
while (!mqttClient.connected()) { while (!mqttClient.connected()) {
mqttClient.setClient(wifiClient); mqttClient.setClient(wifiClient);
mqttClient.setServer(mqttHost, 1883); mqttClient.setServer(mqttHost, 1883);
mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected"); mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
delay(1000); delay(1000);
newConnection = true; newConnection = true;
} }
if(newConnection) { if (newConnection) {
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true); mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
} }
} }
void parseReceivedLine(char* input) { void parseReceivedLine(char* input) {
char segment = 0;
char *token;
float uSv = 0; char segment = 0;
float cpm = 0; char *token;
token = strtok(input, delimiter); float uSv = 0;
float cpm = 0;
while (token != NULL) { token = strtok(input, delimiter);
switch(segment) { while (token != NULL) {
// This is just for validation switch (segment) {
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: // This is just for validation
Serial.printf("Current uSv/hr: %s\n", token); case IDX_CPS_KEY: if (strcmp(token, "CPS") != 0) return; break;
uSv = String(token).toFloat(); case IDX_CPM_KEY: if (strcmp(token, "CPM") != 0) return; break;
break; case IDX_uSv_KEY: if (strcmp(token, "uSv/hr") != 0) return; break;
}
if(segment > 7) { case IDX_CPM:
// Invalid! There should be no more than 7 segments Serial.printf("Current CPM: %s\n", token);
return; cpm = String(token).toInt();
} break;
token = strtok(NULL, delimiter); case IDX_uSv:
segment++; Serial.printf("Current uSv/hr: %s\n", token);
uSv = String(token).toFloat();
break;
} }
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() {
connectMqtt(); connectMqtt();
timer.run(); timer.run();
mqttClient.loop(); mqttClient.loop();
@ -145,10 +147,10 @@ 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 = "";
} }
Serial.write(in); Serial.write(in);
} }
} }