[SOLVED] How to save payload having length more than 1 in a variable


#1

I am subscribed to few variables and certain actions are performed on the basis of the response I get from the ubidots using callback function
for example: If a get a payload '5’ from the source /v1.6/devices/IoT_Project/Fire/lv then the pin 4 is made high for a sec.

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  String strTopic = String((char*)topic);
  if (strTopic == "/v1.6/devices/IoT_Project/Fire/lv") 
  {
    if((char)payload[0] == '5')
    {
    digitalWrite(4, HIGH);
    delay(1000);
    digitalWrite(4, LOW); 
    
  }  } }

Now, I want to get the value of payload and save it in a variable and I don’t know how to do it if the length of payload is ‘2’ or ‘3’.

I hope someone can help me here.

Thanks


#2

Hello, If you subscribe to a topic finished in ‘lv’ you get only the value of the variable, so you should loop over the char array size to obtain the value.

Regards