How to store Parsed json data recieved via MQTT into char[] in ESP

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
long last_time = 0;
char apName[] = "SmartDEVICE.";
char data[100];
// MQTT client
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient); 
char *mqttServer = "broker.hivemq.com";
int mqttPort = 1883;
const char *SSID = "SSID";
const char *PWD = "PASSWORD";
char previous_state[100];
void connectToWiFi() {
  Serial.print("Connectiog to ");
 
  WiFi.begin(SSID, PWD);
  Serial.println(SSID);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.print("Connected.");  
} 


void setupMQTT() {
  mqttClient.setServer(mqttServer, mqttPort);
  // set the callback function
  mqttClient.setCallback(callback);
}
void setup() {
  Serial.begin(115200);
connectToWiFi();
setupMQTT();
  }
void reconnect() {
  Serial.println("Connecting to MQTT Broker...");
  while (!mqttClient.connected()) {
      Serial.println("Reconnecting to MQTT Broker..");
      String clientId = "ESP32Client-";
      clientId += String(random(0xffff), HEX);
      
      if (mqttClient.connect(clientId.c_str())) {
        Serial.println("Connected.");
        // subscribe to topic
        mqttClient.subscribe("swa/commands");
      }
      }
}
void loop() {

  if (!mqttClient.connected())
    reconnect();
   mqttClient.loop();
   StaticJsonBuffer<200> JSONbuffer;
  JsonObject& JSONencoder = JSONbuffer.createObject();
 
  JSONencoder["Device_Name"] = apName;
  JSONencoder["Wifi_Status"]= "Wifi_OK";
  JSONencoder["Switch_Status"]= previous_state;
 
  char JSONmessageBuffer[200];
  JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  Serial.println("Sending message to MQTT topic..");
  Serial.println(JSONmessageBuffer);
 
  mqttClient.publish("esp/test", JSONmessageBuffer) ;
  if (mqttClient.publish("esp/test", JSONmessageBuffer) == true) {
    Serial.println("Success sending message");
  } else {
    Serial.println("Error sending message");
  }
  
  Serial.println("-------------");
  delay(5000);
  
}
void callback(char* topic, byte* payload, unsigned int length) {
  StaticJsonBuffer<200> JSONBuffer;
  char JSONinData[150];
  Serial.print("Callback From- ");
  Serial.println(topic);
    Serial.print("data:");  
   
  Serial.print("payload: ");
  for(int i =0; i<length; i++){
  Serial.print((char)payload[i]);
   JSONinData[i] = (char)payload[i];
 }
 Serial.println();
 JsonObject& parsed = JSONBuffer.parseObject(JSONinData);  
if (!parsed.success()) {   //Check for errors in parsing
 
    Serial.println("Parsing failed");
    delay(2000);
    return;

  }

 const char * val= parsed["Switch_Status"];
// DO SOMETHING WITH THE DATA THAT CAME IN!
 Serial.println("Parsed Data:");
 Serial.println(val); 
previous_state[]=val;

// change the return type of the function

}

I found this error in mybox:
C:\Users\Dell\Documents\Arduino\mqtt_json2\mqtt_json2.ino: In function 'void callback(char*, byte*, unsigned int)':mqtt_json2:108:16: error: expected primary-expression before ']' token
previous_state[]=val;
** ^**
exit status 1
expected primary-expression before ']' token

Explaining by example:
I published this Json String via MQTT to my ESP32 device:
{"Device_Name":"SmartDevice","Wifi_Status":"Wifi_OK","Switch_Status":"ON"}
then according to code through this line

/*JsonObject& parsed = JSONBuffer.parseObject(JSONinData);

if (!parsed.success()) { //Check for errors in parsing
Serial.println("Parsing failed");
delay(2000);
return;
}
const char * val= parsed["Switch_Status"];
// DO SOMETHING WITH THE DATA THAT CAME IN!
Serial.println("Parsed Data:");
Serial.println(val);
previous_state[]=val;

__ */__

I got my parsed data from Switched_Status from JSON file i.e "ON/OFF" but I want to store/rewrite this "ON/OFF" in this line
"JSONencoder["Switch_Status"]= previous_state;" which is not happening. Anyone kindly guide me this to resolve the problem.

MQTT_JSON_TEST.ino (2.86 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.