i am creating a PH sensor that relays data back to Home Assistant using MQTT. i also have the sensor subscribe to a topic to listen for calibration commands (ill ask about that in another thread) of the PH probe. I am able to receive the MQTT data the sensor if subscribed to but where im stuck is parsing that response into a variable that i can use. The response comes in an array array[c,a,l,,,7] and yes there is a comma in the array... i can step through the array with a while loop
while (mqttClient.available()) {
Serial.println((char)mqttClient.read());
}
and print the results to the serial monitor... but i want to capture the complete message into a variable char calMessageand subsequently use it in an IF statement something like if(calMessage == "CAL,7"){ calibrate to 7 here;
}
i know its probably something simple that im missing but for the life of me i cant seem to find the answer... and yes its an Atlas Scientific PH probe,i will be hacking their Arduino sketch to read and calibrate the probe from within Home Assistant.
Please post your full sketch so that what you are doing can be seen in context. For instance, which MQTT library are you using ?
If you have the response in an array of chars then put a '\0' in the next array position and you will have a C style string that you can parse using a variety of functions such as strcmp() and strtok()
if (messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.println((char)mqttClient.read());
}
Use all core Discourse functions; all new user restrictions are removed
Send PMs
Upload images and attachments
Now you have ignored the advice on posting code using code tags and to make things worse you have not posted the complete sketch.
Please help us to help you by following the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
if ( mqttClient.available() ) {
String buff = "";
while (mqttClient.available()) {
char c = mqttClient.read();
buff += c
} // end while
if ( buff.length() > 0 ) {
Serial.println(buff);
}
}
if(mqttClient.available()) {
byte data[100]={0};
mqttClient.readBytes(data, 100);
Serial.println((char *) data);
// then parse the data array
parse_cmd((char *)data); //here is my addition to bring it all together
}