Hello everyone, I'm new in the world of electronics, but for a university project I have to work with Arduino and a CO2 sensor.
The following code allows me to send a command to the sensor, and to receive back the CO2 concentration.
void loop() {
int i=0;
char measure[13];
Serial1.write("send\r");
delay(100);
while (Serial1.available()>0) {
measure[i]=Serial1.read();
i++;
}
Serial.println(measure);
delay(2000);
}
The response of the sensor is:
send
731.8
My question is: how can I extract the number "731.8" from the array, and transform it into a float?
Thank's to anyone who can help me!
The first thing to do is to add a '\0' immediately after the characters received to turn the array into a C style string (NOTE - not a String, capital S), then use the aftof() function to convert it to a float as in this example
void setup()
{
Serial.begin(115200);
char measure[13];
measure[0] = '7';
measure[1] = '3';
measure[2] = '1';
measure[3] = '.';
measure[4] = '8';
measure[4] = '\0'; //terminate the string
float aFloat = atof(measure);
Serial.println(aFloat);
Serial.println(aFloat * 10); //prove it is a real float
}
void loop()
{
}
@leobeltra is being requested to post the UART Protocol of his CO2 sensor.
The sensor sends data items after receiving "Send\r" command from the Host -- this event can be seen as a preamble and then Host should read only 5 bytes.
I tried your codes, this one of GolamMostafa functions a little bit: it returns "0.0" to each measure.
So I still don't get the result that i would, but at least it's able to select only the numeric part of the array!
You need to think about the speed of character reception on Serial1. Post a complete code, and I'll say more. What we presently have doesn't even set the baud rate for Serial1, nor "begin" the stream, so I know you've not given us the whole story. What you perceive as a solution is therefore opaque to us, denying you the benefit of this forums extensive understanding of both Serial, and the broader context of Arduino.
is there any point in processing an incomplete string? (and where is "i" reset to zero for the next string)?
in the code i posted above (copied below), readByteUntil() doesn't return until the termination char (\n) is received or there is a timeout and only after the entire string is received is it processed (the simple print after calling atof()).
@gcjr You are also guarding against overrunning the buffer endpoint, which is ignored in the OP's post. That's important, because if the sender either omits the line end, or the serial transmission is garbled by noise, the receiver has no way of knowing. Assuming perfect transmission in serial matters is always a recipe for failure.
void setup() {
Serial.begin(19200); // serial monitor
Serial1.begin(19200); // serial co2 sensor
while(!Serial);
while(!Serial1);
}
void loop() {
int i=0;
char received[13]={0};
Serial1.write("send\r"); // ask to sensor for a co2 measure
delay(100);
while (Serial1.available()>0) { // read the response by the sensor
received[i]=Serial1.read();
i++;
}
char* initialpart = strtok(received, " "); // discard the echo
char* measure = strtok(NULL, "\r"); // keep only the c02 concentration
Serial.println(measure);
float co2 = atof(measure);
Serial.println(co2, 1);
delay(2000);
}