NODEMCU AND ADAFRUIT.IO ERROR

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
Warmann
 
Posts: 2
Joined: Mon Aug 23, 2021 5:49 am

NODEMCU AND ADAFRUIT.IO ERROR

Post by Warmann »

//Include Lib for Arduino to Nodemcu
#include <SoftwareSerial.h>

#include <ArduinoJson.h>

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID "********"
#define WLAN_PASS "***********"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "********"
#define AIO_KEY "*************************"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

Adafruit_MQTT_Publish volt1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/line_1_voltage");
Adafruit_MQTT_Publish volt2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/line_2_voltage");
Adafruit_MQTT_Publish current1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/line_1_current");
Adafruit_MQTT_Publish current2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/line_2_current");
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");

Adafruit_MQTT_Subscribe onoffbutton1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay1");
Adafruit_MQTT_Subscribe onoffbutton2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay2");



void MQTT_connect();

//D6 = Rx & D5 = Tx
SoftwareSerial nodemcu(D6, D5);
int relay_1 = D1;
int relay_2 = D2;


void setup() {
// Initialize Serial port
Serial.begin(9600);
nodemcu.begin(9600);
while (!Serial) continue;
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
digitalWrite(relay_2,LOW);
digitalWrite(relay_1,LOW);

Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();

Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());

mqtt.subscribe(&onoffbutton1);
mqtt.subscribe(&onoffbutton2);


}

void loop() {
MQTT_connect();

DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, nodemcu);
if (error) {
Serial.println("Invalid Json Object");
doc.clear();
return;
}

Serial.println("JSON Object Recieved");
Serial.print("Recieved Humidity: ");
float hum = doc["humidity"];
// humidity.publish(hum);
Serial.println(hum);
Serial.print("Recieved Temperature: ");
float temp = doc["temperature"];
temperature.publish(temp);
float voltage1 = doc["voltage1"];
volt1.publish(voltage1);
Serial.println(voltage1);
float voltage2 = doc["voltage2"];
//volt2.publish(voltage2);
float curr1 = doc["current1"];
current1.publish(curr1);
float curr2 = doc["current2"];
//current2.publish(curr2);

Serial.println(temp);
Serial.println("-----------------------------------------");

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton1) {

Serial.print(F("Got: "));
Serial.println((char *)onoffbutton1.lastread);
if(!strcmp((char *)onoffbutton1.lastread, "ON")){
digitalWrite(relay_1,HIGH);
}
else{
digitalWrite(relay_1,LOW);
}
}
if (subscription == &onoffbutton2) {

Serial.print(F("Got: "));
Serial.println((char *)onoffbutton2.lastread);
if(!strcmp((char *)onoffbutton2.lastread, "ON")){
digitalWrite(relay_2,HIGH);
}
else
{

digitalWrite(relay_2,LOW);
}
}
}



}

void MQTT_connect() {
int8_t ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Serial.print("Connecting to MQTT... ");

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}


I got this code to monitor current, temperature and voltage with the nodeMCU and Arduino but i'm getting the invalid json object error and can't understand why. Please help me out. Thanks

User avatar
brubell
Learn User Page
 
Posts: 2017
Joined: Fri Jul 17, 2015 10:33 pm

Re: NODEMCU AND ADAFRUIT.IO ERROR

Post by brubell »

I got this code to monitor current, temperature and voltage with the nodeMCU and Arduino but i'm getting the invalid json object error and can't understand why. Please help me out. Thanks
Could you please paste the exact text of the error here?

User avatar
Warmann
 
Posts: 2
Joined: Mon Aug 23, 2021 5:49 am

Re: NODEMCU AND ADAFRUIT.IO ERROR

Post by Warmann »

Invalid Json object

That is what i see in the serial monitor. I haven't used the ArduinoJson library before so I don't understand why that happens

User avatar
brubell
Learn User Page
 
Posts: 2017
Joined: Fri Jul 17, 2015 10:33 pm

Re: NODEMCU AND ADAFRUIT.IO ERROR

Post by brubell »

Seems like you're having an issue parsing the object returned from Adafruit IO,

Take a look at: https://arduinojson.org/v5/faq/why-parsing-fails and try to parse less out of the JSON object returned by adafruit io. Try it line-by-line until it fully works.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”