Hello. Something really really weird started to happen to my ESP32 devices. I am receiving MQTT commands from my raspberry PI.
number_to_pick is global variable and set to 0
I have a retained message that the message receives everytime it powers up. During that message, number_to_pick is not being modified, but somehow its value changes???
See the code below, since the current_status initially is not set to "PILDYMAS" the last 2 lines of code will be executed where I parse the item_inside mqtt message.
As you can see from my parse function, It does not have any code to change the value of number_to_pick. From the Serial monitor you can see that at the beggining of the function numer_to_pick4 is still 0, however, at the end, numer_to_pick5 is set to 6648929. The only thing that happens between numer_to_pick4 and numer_to_pick5 is the Raspberry PI confirmation that the message has been received, there is nothing to do with the number_to_pick value in either of these functions.
Also, the value is always 6648929.
Could someone help me understand why would that happen???
number_to_pick is global variable and set to 0
I have a retained message that the message receives everytime it powers up. During that message, number_to_pick is not being modified, but somehow its value changes???
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
if(number_to_pick >0){
toggle_led_strip(yellow,1);
}
else if(strncmp(current_status, "PILDYMAS", 8)==0){
toggle_led_strip(purple,1);
parse_item_inside((char*)payload);
item_inside.updated_quantity = item_inside.quantity; // THIS IS UPDATED QUANTITY
strcpy(current_status,"IDLE");
item_inside.quantity = item_inside.previous_quantity+item_inside.updated_quantity;
create_JSON_object_pildymas("global/device_status/Quantity_update",false);
Quantity_update_flag=1;
return;
}
parse_item_inside((char*)payload);
item_inside.previous_quantity = item_inside.quantity;
}
}
Code:
void parse_item_inside(char* payload){
Serial.print("numer_to_pick4=");
Serial.println(number_to_pick);
cJSON* jsonObj = cJSON_Parse((char*)(payload));
// Get pointer to specific field in JSON object
cJSON* serial = cJSON_GetObjectItem(jsonObj, "Serial");
cJSON* quantity = cJSON_GetObjectItem(jsonObj, "Quantity");
cJSON* state = cJSON_GetObjectItem(jsonObj, "Status");
//Get integer value - other fields allow access to strings, floats etc.
strcpy(item_inside.serial,serial->valuestring);
item_inside.quantity = quantity->valueint;
strcpy(item_inside.state,state->valuestring);
if (strncmp(item_inside.state, "NEXT", 4) == 0){
Serial.println("Raspberry PI confirms that NEXT command was received");
confirmation_counter=0;
next_flag = 0;
}
if (strncmp(item_inside.state, "DONE", 4) == 0){
Serial.println("Raspberry PI confirms that DONE command was received");
confirmation_counter=0;
done_flag = 0;
}
if (strncmp(item_inside.state, "Quantity_update", 15) == 0){
Serial.println("Raspberry PI confirms that Quantity_update command was received");
confirmation_counter=0;
Quantity_update_flag = 0;
}
Serial.print("numer_to_pick5=");
Serial.println(number_to_pick);
// Delete JSON object (don't forget this)
cJSON_Delete(jsonObj);
}
Also, the value is always 6648929.
Could someone help me understand why would that happen???