Thank you again sir! I have implemented that - I am having one problem though. I believe that is something to do with the way I have initialized my structure or my strings item_inside.item and item_inside.serial:
typedef struct {
char *item;
char *serial;
int quantity;
}item_inside_struct;
item_inside_struct item_inside;
As previously mentioned, I am able to fill the structure here without any problems:
if (strcmp(topic,available_items_topic) == 0){ //if number_to_pick received, means the complecataion has been scanned and initiate the pick_to_light
available_items = atoi(message);
//Serial.print("available_items = ");
//Serial.println(available_items);
OLED_display(available_items);
}
if (strcmp(topic,ota_topic) == 0){
start_OTA();
}
if (strcmp(topic,item_inside_topic) == 0){ //if number_to_pick received, means the complecataion has been scanned and initiate the pick_to_light
cJSON* jsonObj = cJSON_Parse((char*)(payload));
// Get pointer to specific field in JSON object
cJSON* item = cJSON_GetObjectItem(jsonObj, "item");
cJSON* serial = cJSON_GetObjectItem(jsonObj, "serial");
cJSON* quantity = cJSON_GetObjectItem(jsonObj, "quantity");
if (item != NULL)
{
//Get integer value - other fields allow access to strings, floats etc.
item_inside.item = item->valuestring;
item_inside.serial = serial->valuestring;
item_inside.quantity = quantity->valueint;
Serial.println(item_inside.item);
Serial.println(item_inside.serial);
Serial.println(item_inside.quantity);
}
When the quantity updates, I am using this function to send the json to the mqtt:
void create_JSON_object()
{
Serial.println("CREATING JSON OBJECT");
Serial.println("item_inside.item=");
Serial.println(item_inside.item);
Serial.println("item_inside.serial=");
Serial.println(item_inside.serial);
Serial.println("item_inside.quantity=");
Serial.println(item_inside.quantity);
cJSON* sendObj = cJSON_CreateObject();
cJSON_AddStringToObject(sendObj, "item", item_inside.item);
cJSON_AddStringToObject(sendObj, "serial", item_inside.serial);
cJSON_AddNumberToObject(sendObj, "quantity", item_inside.quantity);
// Convert to a string
char *msg = cJSON_Print(sendObj);
// Send the info
client.publish(item_inside_topic, (byte*)(msg), strlen(msg));
// Clean up
free(msg);
cJSON_Delete(sendObj);
}
Serial monitor output:
CREATING JSON OBJECT
14:07:43.202 -> item_inside.item=
14:07:43.202 -> ⸮⸮?⸮"
14:07:43.202 -> item_inside.serial=
14:07:43.202 -> ⸮⸮⸮?⸮⸮}⸮-⸮⸮}⸮⸮⸮⸮⸮⸮⸮⸮
14:07:43.202 -> item_inside.quantity=
14:07:43.202 -> 49
As you can see, the quantity is correct, because i decremented by 1. However, the item_inside.item and item_inside.serial data seems corrupted??
How could that happen? I have initialized my structure in global scope and I have checked if i properly assigned the values of my structure here:
item_inside.item = item->valuestring;
item_inside.serial = serial->valuestring;
item_inside.quantity = quantity->valueint;
Serial.println(item_inside.item);
Serial.println(item_inside.serial);
Serial.println(item_inside.quantity);
and it seemed right cause in serial monitor I could see correct values.
I have also noticed slight difference:
When I initially send data from my raspberry PI to my ESP8266 device, it comes like that:
14:09:45.303 -> Message arrived [device1/item_inside] {"item":"item1","serial":"1A2B","quantity":50}
Since my ESP8266 is subscribed to the same topic, i am able to receive the information that the ESP8266 itself sends and it looks like that:
14:09:49.375 -> Message arrived [device1/item_inside] {
14:09:49.375 -> "item": "⸮\u0001",
14:09:49.375 -> "serial": "",
14:09:49.375 -> "quantity": 49
Despite the fact that the item and serial data is corrupted, you can notice that it is slightly different format than the original json sent by raspberry PI. Raspberry PI sends a whole message in one line whereas here, every object is in a new line. I am not sure whether that can cause any problems?