First - thank you in advance for any input here - has me scratching my head!
I am reading data in via Serial from a COZIR temp/humidity/co2 sensor.
The serial Format of the data is “H ##### T ##### Z ##### z ##### \r\n”
where H is the humidity, T is temperature, Z is filtered CO2, and z is instantaneous CO2 value.
as you can see in the serial output at the end of my post, sometimes that data gets read into the string in the incorrect order(I think?)
Most of the time its correct , but intermittently … its in the wrong order.
and sometimes there is extra data as well(second output has an extra zero after the ‘z’ value - z 007820 which causes CO2 to read 7,820 ppm instead of the correct 782 ppm)
Does anyone see anything in my code that could cause this?
// - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
//char messageFromPC[numChars] = {0};
float humidity = 0;
float temp1 = 0;
float co2avg = 0;
float co2 = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Data String Expected from sensor is H ##### T ##### Z ##### z ##### \r\n");
// Particle.variable("humidity", humidity);
// Particle.variable("temperature", temp1);
// Particle.variable("co2avg", co2avg);
// Particle.variable("co2", co2);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
calculations();
showParsedData();
sendToCloud();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = 'H';
char endMarker = '\r';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
Serial.println("String ");
Serial.println(tempChars);
Serial.println("-----------------");
strtokIndx = strtok(tempChars,"TZz"); // get the first part - the string
humidity = atof(strtokIndx); // convert to int
strtokIndx = strtok(NULL, "TZz"); // this continues where the previous call left off
temp1 = atof(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, "TZz");
co2avg = atof(strtokIndx); // convert this part to an int
strtokIndx = strtok(NULL, "TZz");
co2 = atof(strtokIndx); // convert this part to an int
}
//============
void calculations() {
temp1 = (((temp1 - 1000) / 10) * 1.8) + 32;
humidity = humidity / 10;
}
void showParsedData() {
Serial.print("humidity: ");
Serial.println(humidity);
Serial.print("temperature: ");
Serial.println(temp1);
Serial.print("CO2 Avg ");
Serial.println(co2avg);
Serial.print("CO2 ");
Serial.println(co2);
}
void sendToCloud(){
Particle.publish("UbidotsHumidity", String(humidity), PRIVATE);
delay(2000);
Particle.publish("UbidotsTemp", String(temp1), PRIVATE);
delay(2000);
Particle.publish("UbidotsCo2", String(co2avg), PRIVATE);
delay(2000);
delay(90000); //wait 15 minutes
}
Here are the Serial Results:
Data String Expected from sensor is H ##### T ##### Z ##### z ##### \r\n
String 00628 T 01212 Z 00758 z 00726
-----------------
humidity: 62.80
temperature: 70.16
CO2 Avg 758.00
CO2 726.00
String 00628 T 01212 Z 00761 z 007820
-----------------
humidity: 62.80
temperature: 70.16
CO2 Avg 761.00
CO2 7820.00
String 00614 T 012 H 00589 T 01220 Z
-----------------
humidity: 61.40
temperature: -145.84
CO2 Avg 1220.00
CO2 0.00
String 00571 T 01207 Z 00688 z 00659
-----------------
humidity: 57.10
temperature: 69.26
CO2 Avg 688.00
CO2 659.00