1

I'm new to embedded systems. I'm trying to send data/values to a NodeMCU and I found I could using the ArduinoJSON lib. I'm trying the same thing that the tutorial gives, but I don't know why it doesn't work for me. Can someone tell me why? And how to fix it?

This is the Arduino Nano script:

#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);
  }
}

And this is the NodeMCU script:

#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5); //RX,TX
#include <ArduinoJson.h>

void setup() {
  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.println(data1);
  Serial.print("Data 2 ");
  int data2=root["data2"];
  Serial.print(data2);
  Serial.println("");
  Serial.println("---------------------xxxxx---------------------");
}

When I check serial, it doesn't show the data that's sent by the Nano, but this thing shows up on the serial monitor.

⸮$r$rlp⸮n⸮⸮8⸮⸮⸮

By the way sorry for my bad English.

dda
  • 6,030
  • 2
  • 25
  • 34
  • link the tutorial, provide info on your wiring. did you try to simply send a single character from one to the other device befor you added all that json stuff? – Piglet Jan 27 '20 at 14:01
  • You are following an old tutorial which is based on ArduinoJson v5.x API, you need to use ArduinoJson v6.x API. Read the document on [Migrating from version 5 to 6](https://arduinojson.org/v6/doc/upgrade/) or read the latest v6 example codes from [github](https://github.com/bblanchon/ArduinoJson). – hcheung Jan 27 '20 at 16:04
  • did you wire RX to TX? I would nest the parsing into `if (s.available()) {` – Juraj Jan 27 '20 at 16:40

0 Answers0