0
\$\begingroup\$

I'm working on communicating between the two NodeMCU basing on the serial communication protocol. I've used SoftwareSerial and ArduinoJson library for my program according to some sources on the Internet. The problem is at the stability of the reading value from Serial. As far as what I saw on the COM monitor, it was either lack of data or appeared some odd letter. I want the two Node to be able to send and receive data from each other but the instability has stopped me in the first direction. Anyone's help will be appreciated. I'm trying to apply UART because of its simplicity and speed but the suggestion of another solution with other protocols would be great. The code of the transmitting node and the receiving node was attached below:

The sending program

#include <ArduinoJson.h>
SoftwareSerial s(D5,D6);
byte C1;
void setup() {

  pinMode(D0,OUTPUT);
s.begin(115200);
}
 

void loop() {
 

  //Sending code
   StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["Temp"] = 100;
  
if(s.available()>0)
{
 root.printTo(s);
}

The receiving program

#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
#include <ArduinoJson.h>
 
void setup() {
  // Initialize Serial port
  Serial.begin(115200);
  pinMode(D0,OUTPUT);
  s.begin(115200);
  while (!Serial) continue;
 
}
 
void loop() {
 
 //Receiving code
StaticJsonBuffer<1000> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(s);
  
  if (root == JsonObject::invalid())
    return;
 
  root.prettyPrintTo(Serial);
  Serial.print("Data 1 ");
  Serial.println("");
  int data1=root["Temp"];
  if(data1==100)
  digitalWrite(D0,HIGH);
  else
  digitalWrite(D0,LOW);
  delay(100);
}

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ I think trying to use a software-based asynchronous serial communications link at 115kbaud is an inherently bad idea. Either reduce the baud rate to something like 9600 baud or use a hardware-based communication. \$\endgroup\$ Commented Nov 15, 2020 at 16:41
  • \$\begingroup\$ I'll think of it. I plan to go with the I2C protocol as the recommendation from the answer below. Do you think it's a good alternative solution? \$\endgroup\$
    – Thanh Nhon
    Commented Nov 15, 2020 at 16:49
  • \$\begingroup\$ You haven't given enough information to make a judgement about "good" approaches. If you are sending temperature data isn't 2400 baud fast enough? \$\endgroup\$ Commented Nov 15, 2020 at 16:56
  • \$\begingroup\$ Your code doesn't make any sense so you do not currently have any specific answerable question here. Doing serial communication between MCUs is routine, but requires fairly sophisticated programming skills to do correctly; if at all possible, you'll find it far easier to keep everything in one MCU. \$\endgroup\$ Commented Nov 15, 2020 at 18:41
  • \$\begingroup\$ @Chris Stratton Could you point out the problem with my code? Why doesn’t it make sense? I thought it is inadequate of some factor to be stable rather than completely wrong, isn’t it? I intend to send collected analog and digital data to send to each other. Could you give me a hint to adjust or replace with new code? Thank you. \$\endgroup\$
    – Thanh Nhon
    Commented Nov 16, 2020 at 5:41

1 Answer 1

1
\$\begingroup\$

You should provide a string or char array into the

  JsonObject& root = jsonBuffer.parseObject(s);

The s variable is an object here for SoftwareSerial library.

You firstly need to read the serial like

while(s.available()){

String r = s.readString();
JsonObject& root = jsonBuffer.parseObject(r);
}

Here r will be a raw json array and json object will parse it. And it will be good if you upload your output to understand and see what is really going on

Update:

Using UART with 115200 baudrate without delay and with jumper cables may cause the miss of the characters.

The other issue is, maybe this is happenning because of monitoring the Serial port at the same time. Maybe you can debug it with just looking the digital pins which you are controlling to understand your code is working fine or not.You can

Or you can try I2c communication which is synchronous and more reliable and faster instead of UART.

Actually, SoftwareSerial library is a bit slower than the actual serial port.

\$\endgroup\$
1
  • \$\begingroup\$ I just followed your guidance of looking at the led status but it varied as similarly as the change of reading data on the monitor. Also, I consider switching to I2C for now. \$\endgroup\$
    – Thanh Nhon
    Commented Nov 15, 2020 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.