manipulating String data on serial monitor into floats MQTT payload as one..

Hi..

The scenario is as so, I have one MKR1000 board that is measuring temp and humidity packing it up from Float to String and sending it to a second MKR1000 board were I want to be able to manipulate it.

I have the following output on the serial monitor of the receiving board.

the 2 numbers are sensor values. presently as String data type.

I would like them as separate float variables presently they are received at the same time via the following code.

I had thought about splitting the "payload" string into 2 different character arrays then into 2 floats but I can't figure the code.

void messageReceived(String &topic, String &payload) {

Serial.println(payload);

}

the whole code as below, the message is received from another MKR1000 board that is measuring the values and sending them over MQTT.

26/10 began commenting
*/

/////// Libaries \\\\\\\
#include <SPI.h>
#include <WiFi101.h>
#include <MQTT.h>


/////// setup WiFi details\\\\\\\

const char ssid[] = "";
const char pass[] = "";


///////Variables to take values from received variables\\\\\\\

unsigned long lastMillis = 0;
String payload;


WiFiClient net;
MQTTClient client;


void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nconnecting...");
  while (!client.connect("arduino", "try", "try")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");

  client.subscribe("TempTopic");
   client.subscribe("HumidityTopic");
  // client.unsubscribe("/hello");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  client.begin("broker.shiftr.io", net);


  connect();
}

void loop() {
  
  client.loop();
  client.onMessage(messageReceived);

  if(!client.connected()) {
    connect();
  }


 delay(1000);



}
void messageReceived(String &topic, String &payload) {

Serial.println(payload);

}

}

and one other thing to clarify..

do MQTT messages have to be sent as String data type?

Apologies if the code is sloppy I have not figured a standard as yet.

Ta.

hadwll:
do MQTT messages have to be sent as String data type?

you do#include <MQTT.h>

which MQTT library is that?
if this is that one, the doc describes those API:

// Publishes a message to the broker with an optional payload:

bool publish(const String &topic);
bool publish(const char topic[]);
bool publish(const String &topic, const String &payload);
bool publish(const String &topic, const String &payload, bool retained, int qos);
bool publish(const char topic[], const String &payload);
bool publish(const char topic[], const String &payload, bool retained, int qos);
bool publish(const char topic[], const char payload[]);
bool publish(const char topic[], const char payload[], bool retained, int qos);
bool publish(const char topic[], const char payload[], int length);
bool publish(const char topic[], const char payload[], int length, bool retained, int qos);

// Subscribe to a topic:
bool subscribe(const String &topic);
bool subscribe(const String &topic, int qos); 
bool subscribe(const char topic[]);
bool subscribe(const char topic[], int qos);

Thanks, it is the same library. I will study this and see if I can work out how to utilize it!

probably this one

bool publish(const char topic[], const char payload[], int length);

could be used to publish a binary message

Hi,

So I had a look this morning could you help me understand what is going on?

I understand that I need to use the piece of code that you have lined up above. The only place I can find code similar to that is in the header file.

I see them all listed there. I can't figure out how to run a specific line.

however, I do see this advanced callback... which is the one I need.

typedef void (*MQTTClientCallbackSimple)(String &topic, String &payload);

typedef void (*MQTTClientCallbackAdvanced)(MQTTClient *client, char topic[], char bytes[], int length);

how do I call the advance callback opposed to the simple..

in my code, I have this function

void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);

and this

client.onMessage(messageReceived);

which relate to here in the header file I guess?

(I tried changing the ".onMessage" to".onMessageAdvanced" to no avail.

 void onMessage(MQTTClientCallbackSimple cb);

  void onMessageAdvanced(MQTTClientCallbackAdvanced cb);

I'm struggling to put it all together though.

Thanks. :slight_smile:

solution...

call- client.onMessageAdvanced(messageReceived);

function void messageReceived(MQTTClient *client, char topic[], char payload[], int payload_length) {

and now I am going to try and split the payload with a for loop...

void messageReceived(MQTTClient *client, char topic[], char payload[], int payload_length) {
  for (byte i = 0; i < payload_length; i++) {
      Serial.print((char)payload[i]);

  }

and then convert the char array to float...

hopefully :smiley: