Mqtt and while loop

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.

Thanks,
Travis

Welcome to the forum

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()

It wouldn't let me post the sketch,said new users can only post two links and wouldnt let me upload it either

#include <string.h>
#include <ArduinoMqttClient.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ph_grav.h>

int messageSize = mqttClient.parseMessage();

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());
 
}

}

You need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can…

  • 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

you could try readBytes()

` if(mqttClient.available()) {
        byte data[100]={0};
       mqttClient.readBytes(data, 100)
       Serial.println((char *) data);
      // then parse the data array

}`

or go with String like this

if ( mqttClient.available() ) {
  String buff = "";
      while (mqttClient.available()) {
         char c = mqttClient.read();
         buff += c
     } // end while
        
     if ( buff.length() > 0 ) {
        Serial.println(buff);
     }
 
 
}

Thank you!!!! That worked!!

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
      
    }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.