ESP32 saving string array globally

Thread Starter

zazas321

Joined Nov 29, 2015
777
Hello. I am having a lot of troubles saving a string globally. I am using MQTT communications and my server ends a status message to remote devices. I need to read that status message and assign a current_status variable to that string.

I use raspberry PI as a server and I use python to publish message as following:
mqttc.publish("device1/status", "ACTIVE").
And on my serial monitor I can see that I receive that message.
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);

  }
  if (strcmp(topic, status_topic) == 0){ 
    if (strncmp(message, "GOOD_JOB", 8) == 0){
      toggle_led_strip(blue,5);           
    }
    else if (strncmp(message, "ACTIVE", 6) == 0){
      strcpy (current_status,message);// RETURNS GURU MEDITATION ERROR
      Serial.println(current_status);
    }
  }
  
  if (strcmp(topic,number_to_pick_topic) == 0){ //if number_to_pick received, means the complecataion has been scanned and initiate the pick_to_light
    clear_LED_strip(6);
    number_to_pick = atoi(message);
    //Serial.print("number to pick = ");
    //Serial.println(number_to_pick);
    OLED_display(item_inside.quantity,number_to_pick);
    LED_strip_ON(green,number_to_pick);
    }   
}
I am checking whether the string received is "ACTIVE" and then use
Code:
strcpy (current_status,message);// RETURNS GURU MEDITATION ERROR
to copy the string to my variable current_status

I have set the current_status as global variable and the initial state is "none"
Code:
char* current_status = "none"
However, my code immidaitely goes into an error state:
1596787460978.png

What is the problem with the way im saving string?
 

Thread Starter

zazas321

Joined Nov 29, 2015
777
UPDATE

Instead of using strcpy function, I have tried to assign the variable directly:
Code:
    else if (strncmp(message, "ACTIVE", 6) == 0){
     current_status = "ACTIVE";
      Serial.println(current_status);
    }


And that no longer generates an error, however I am having another problem now: I am using this current_status variable in my other function, where I read sensor value. If the current_status is "ACTIVE" , I want to do something with the data. But it never gets into the if statement:

my reading sensor function:
[code]
void Read_sensor(){
  sensor=digitalRead(SENSOR);//ALWAYS TAKE SENSOR READING EVERY 50MS
  //IF ITEM ACTIVATED, SET GREEN LIGHT AND WAIT FOR PERSON TO TOGGLE THE DEVICE
  
    if(sensor==LOW && sensor_prev_state==HIGH){ // IF SENSOR IS TRIGGERED ON
      //Serial.println("sensor triggered\n"); // output serial monitor
      sensor_prev_state=LOW; // set the previous state to LOW
      Serial.print("sensor triggered\n"); // output serial monitor
      if (number_to_pick==0){ // IF NUMBER TO PICK ITEMS IS 0, SET RED LIGHT TO SHOW OPERATOR WRONG BOX
        //digitalWrite(LED_RED,1);     //if the sensor active flag is 0 (item not activated), set the RED light on to indicate the wrong selection  
        //LED_strip_ON(black,6);
        tone(BUZZER, frequency);
        //LED_strip_ON(black,6);
        LED_strip_ON(red,6);
        
      }   
    }
      
    
    else if(sensor==HIGH && sensor_prev_state==LOW){ //sensor deactivated
      if(number_to_pick > 0 && current_status == "ACTIVE"){ // if we still need to take items from this box after
        Serial.println("sensor deactivated=");
      //if(number_to_pick > 0 && strcmp(current_status,"ACTIVE")==0){ // if we still need to take items from this box after
        char buffer2 [33];
        number_to_pick--;
        item_inside.quantity--;
        itoa (number_to_pick,buffer2,10);
        client.publish(number_to_pick_topic, buffer2); // publish a message that the sensor has been triggered    
        create_JSON_object();  // publish item_inside topic to update quantity
        //OLED_display(item_inside.quantity,number_to_pick);
        Serial.print("number_to_pick=");
        Serial.println(number_to_pick);
        sensor_prev_state = HIGH;
        delay(200);
        //CHECKIF NUMBER TO PICK GOT DECREMENTED TO 0 HERE 
        if(number_to_pick==0){ // if we finished collecting items from this box, blink yellow LED to notify operator 
          client.publish(status_topic,"DONE");
          client.publish(number_to_pick_topic,"0");// respond to topic that 0 items left
        }
      }
        else if(number_to_pick == 0 && current_status != "ACTIVE"){
          digitalWrite(LED_RED,0);
          noTone(BUZZER);
          LED_strip_ON(black,6);       
          sensor_prev_state = HIGH;
        }
        else{
          Serial.print("current_status=");
          Serial.println(current_status);
          delay(500);
        }
    }
}
The important part from this function is:
Code:
//if(number_to_pick > 0 && strcmp(current_status,"ACTIVE")==0){ // if we still need to take items from this box after
        char buffer2 [33];
        number_to_pick--;
        item_inside.quantity--;
        itoa (number_to_pick,buffer2,10);
        client.publish(number_to_pick_topic, buffer2); // publish a message that the sensor has been triggered    
        create_JSON_object();  // publish item_inside topic to update quantity
        //OLED_display(item_inside.quantity,number_to_pick);
        Serial.print("number_to_pick=");
        Serial.println(number_to_pick);
        sensor_prev_state = HIGH;
        delay(200);
        //CHECKIF NUMBER TO PICK GOT DECREMENTED TO 0 HERE 
        if(number_to_pick==0){ // if we finished collecting items from this box, blink yellow LED to notify operator 
          client.publish(status_topic,"DONE");
          client.publish(number_to_pick_topic,"0");// respond to topic that 0 items left
        }
      }
        else if(number_to_pick == 0 && current_status != "ACTIVE"){
          digitalWrite(LED_RED,0);
          noTone(BUZZER);
          LED_strip_ON(black,6);       
          sensor_prev_state = HIGH;
        }
        else{
          Serial.print("current_status=");
          Serial.println(current_status);
          delay(500);
        }
I am checking if the number_to_pick is higher than 1, which is 100% correct ( I have used this part before and it works, now just trying to add status)

Code:
if(number_to_pick > 0 && strcmp(current_status,"ACTIVE")==0){ // if we still need to take items from this box after
But it never goes into if statement. If it does not go into any if statements, i print what is the current_status
Code:
        else{
          Serial.print("current_status=");
          Serial.println(current_status);
          delay(500);
        }
But I am getting weird characters all the time, see serial monitor:


1596788613768.png

Why would it not be "ACTIVE" ??
 
Top