I am trying to establish a communication between Arduino Uno ( Master) and ESP32 (Slave) using RS485 protocol (using MAX485 module). I have successfully done this between two Arduino Unos. SoftwareSerial library is what i am using between two Arduino Unos, now trying with ESP32 and Arduino Uno (using MAX485 module) . For ESP32 i have used SoftwareSerial (https://github.com/akshaybaweja/SoftwareSerial) library.
After reading the library and making connection i am not be able to establish a communication between them. Sharing my code below.
Receiver
#include <SoftwareSerial.h>
SoftwareSerial softwareSerial(9, 10);
String str;
void setup()
{
Serial.begin(9600);
softwareSerial.begin(9600);
}
void loop()
{
while (softwareSerial.available() > 0) {
char inChar = softwareSerial.read();
if (inChar == '$') {
str.trim();
Serial.println("received command" + str);
memset(&str, 0, sizeof(str));
Serial.flush();
softwareSerial.flush();
}
else {
str = str + inChar;
}
}
}
Sender
#include <SoftwareSerial.h>
SoftwareSerial swSer1(22, 23, false, 256);
String str;
void setup() {
Serial.begin(9600);
swSer1.begin(9600);
}
void loop() {
String sendCmd = "MSG1$";
Serial.println("Sending ");
Serial.println(sendCmd );
char copy[50];
sendCmd.toCharArray(copy, 50);
swSer1.write(sendCmd);
delay(1000);
}
Here my question is where i am going wrong in this project. Please guide me.
Note : DE AND RE PINS are connected to high for sender device and to low for receiver device as given in the RS485 documentation.