Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

json - Issue with communication between ESP8266 and Arduino UNO

I am having an odd issue. I am providing my codes but I believe the problem is in the wiring which I will explain in a second. ESP8266 CODE:

#include <SoftwareSerial.h>
SoftwareSerial s(12,14);
#include <ArduinoJson.h>
 
void setup() {
s.begin(9600);
Serial.begin(9600);
}
 
void loop() {
 StaticJsonBuffer<1000> jsonBuffer;
 JsonObject& root = jsonBuffer.createObject();
  root["data1"] = 100;
  root["data2"] = 200;
 Serial.println(s.available());
if(s.available()>0)
{
 root.printTo(s);
}
delay(1000);
}

ARDUINO UNO CODE:

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial s(5,6);
 


void setup() {
  // Initialize Serial port
  Serial.begin(9600);
  s.begin(9600);
  //while (!Serial) continue;
  Serial.println("START");
 
}
 
void loop() {
 StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(s);
  if (root == JsonObject::invalid()){
    Serial.println("JSON invalid");
    Serial.println(s.available());
    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);

  delay(1000);
 
}

So the serial monitor of Arduino Uno (after I plug in both arduino and esp8266) says that s.available() = 0. Therefore, it is not recieving JSON. The weird thing is, when i take out the cables from the pins in Arduino, put them in TX and RX pins and then put them back to pins 5 and 6 everything works. When I unplug the arduino and/or ESP and plug them back in the problem repeats- s.availalbe() = 0 (until I do that weird manouver with taking out the cables and putting them back in). I believe there is something I do not understand wiring-wise. I tried connecting them (esp and uno) to the common GND but it still does not work. Does anyone have any ideas what I might be missing here? Thanks

PS, thats the setup: wiring pic


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, I have just managed to fix the problem. I had to change the pins to 0 and 1 like that:

SoftwareSerial s(0,1);

However, this pins are used to communicate with your computer as well, resulting in the arduino ide not being able to upload the code to our arduino while this pins are occupied. Simple solution was to just upload the code and connect NodeMcu ESP8266 afterwards. Hope this helps somone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...