Extracting information from a string MQTT (esp8266)

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hello. I am programming my ESP8266 devices. My raspberry PI is sending a MQTT data to ESP8266, the MQTT payload is an object type data which looks like this:
Code:
msg.payload = {};
msg.payload.item = msg.item;
msg.payload.serial = msg.serial;
msg.payload.quantity = msg.quantity;
return msg;

2020-07-29-080322_1920x1080_scrot.png

When I receive the data on my ESP8266, I receive that as a string :
1595998459459.png

I use an example code to receive the mqtt data:

Code:
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  char message[length + 1];
  strncpy (message, (char*)payload, length);
  message[length] = '\0';
  Serial.println(message);
so as you can see from the code above, it puts a msg.payload into a message. When I receive this message, I have a structure:
Code:
struct item_inside_struct {
  char item[30];
  char serial[30];
  int quantity;
};
item_inside_struct item_inside;
and I need to fill it as following:
item_inside.item = "item1"
item_inside.serial = "1A2B"
item_inside.quantity = 50


I have made an attempt to use strtok() function, but since my string contains various chars such as {, " , I dont know if I can use it efficiently to extract the data that I need:

Code:
  if (strcmp(topic,item_inside_topic) == 0){ //if number_to_pick received, means the complecataion has been scanned and initiate the pick_to_light
    char * pch;
    pch=strtok(message,",");
    while (pch != NULL)
    {
      printf ("%s\n",pch);
      pch = strtok (NULL, " ,");
    }
    }
RESULT:
1595998830347.png
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
I also cannot fully figure out how does this variable byte* payload works:

Code:
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  char message[length + 1];
  strncpy (message, (char*)payload, length);
  message[length] = '\0';
  Serial.println(message);
  Serial.print("PAYLOAD TEST=");
  Serial.println((char*)payload);
I just use serial.print and printing (char*)payload:
Serial monitor:

Code:
09:50:33.137 -> Attempting MQTT connection...connected
09:50:33.442 -> Message arrived [device1/item_inside] {"item":"item1","serial":"1A2B","quantity":50}
09:50:33.442 -> PAYLOAD TEST={"item":"item1","serial":"1A2B","quantity":50}
09:50:33.442 -> Message arrived [device1/available_items] 76
09:50:33.442 -> PAYLOAD TEST=76":"item1","serial":"1A2B","quantity":50}
As you can see, after the first mqtt message is received, the (char*)payload is = {"item":"item1","serial":"1A2B","quantity":50}

After the second message is received, I expect the (char*)payload to be 76 and instead, it prints :
76":"item1","serial":"1A2B","quantity":50} which is a weird combination of both messages. How does it not replace data from the message 1 ?
 
Top