My circuit connection is like: (I am following this tutorial)
Code uploaded to Arduino:
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial s(5,6);
void setup() {
s.begin(9600);
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["data1"] = 100;
root["data2"] = 200;
if(s.available()>0){
root.printTo(s);
}
}
Code uploaded to NodeMCU:
#include <SoftwareSerial.h>
int D5 = 14, D6 = 12;
SoftwareSerial s(D6,D5);
#include <ArduinoJson.h>
void setup() {
// Initialize Serial port
Serial.begin(9600);
s.begin(9600);
while (!Serial) continue;
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid())
return;
Serial.println("JSON received and parsed");
root.prettyPrintTo(Serial);
Serial.print("Data 1 ");
Serial.println("");
int data1=root["data1"];
Serial.print(data1);
Serial.print(" Data 2 ");
int data2=root["data2"];
Serial.print(data2);
Serial.println("");
Serial.println("---------------------xxxxx--------------------");
}
Arduino sends JSON data and NodeMCU parses it as expected.
--------Till this everything is fine. ---------
But my requirement is vice versa. When I create JSON object in nodeMcu and try to parse in arduino, JSON not received.
This time I wrote this code in nodeMcu:
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
int D5 = 14, D6 = 12;
SoftwareSerial s(D6,D5);
void setup() {
Serial.begin(9600);
s.begin(9600);
while (!Serial) continue;
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["data1"] = 100;
root["data2"] = 200;
if(s.available()>0){
root.printTo(s);
}
}
And code in Arduino:
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial s(5,6);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid())
return;
Serial.println("JSON received and parsed");
Serial.print("Data 1 : ");
int data1=root["data1"];
Serial.println(data1);
Serial.print("Data 2 : ");
int data2=root["data2"];
Serial.println(data2);
Serial.println("---------------------xxxxx--------------------");
}
This code doesn't work. I don't receive anything in serial monitor of arduino. The circuit connection remains the same as before.
Anymore information should I provide ? What modification do I need?