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:
When I receive the data on my ESP8266, I receive that as a string :
I use an example code to receive the mqtt data:
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:
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:
RESULT:
Code:
msg.payload = {};
msg.payload.item = msg.item;
msg.payload.serial = msg.serial;
msg.payload.quantity = msg.quantity;
return msg;
When I receive the data on my ESP8266, I receive that as a string :
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);
Code:
struct item_inside_struct {
char item[30];
char serial[30];
int quantity;
};
item_inside_struct item_inside;
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, " ,");
}
}