-1

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

igusso
  • 1
  • 1
  • 1
  • 1
    I don't know or use those libraries, but it looks to me like your ESP waits to receive something then sends something and your UNO never tries to send or receive anything. Could you edit your question and clarify who is supposed to be sending and when/where in the code and likewise who is supposed to be receiving what/where/when please? Thank you. – Mark Setchell Jan 12 '21 at 17:28

1 Answers1

-1

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.

igusso
  • 1
  • 1
  • 1
  • what? you use SoftwareSerial on hardware Serial pins? then use the hardware Serial – Juraj Jan 13 '21 at 06:33
  • the right answer is in the comment. you wait for available() but there is nothing because you send nothing from the other side. there is something only if random bytes from re-connection occur – Juraj Jan 13 '21 at 06:35