I have to gather data from two sensors which work using uart(rx,tx) communication. I have to integrate them with esp8266 but the problem is that I can't have 2 software serials using esp8266. I searched for libraries which can allow 2 software serials on esp8266 but none of them support a baud rate of 115200. One of my sensors baud rate is 115200 and I can't change it. I saw a code on the official softwareserial library documentation and read the example of "2 ports using software serial" and implemented on esp8266 but it's not working. Kindly guide me (I only have to use esp8266 and those sensors and can't change them now). Please help me, anyone having any solution on esp8266. Thanks
My code:
#include <SoftwareSerial.h>
#define sh_TX D8
#define sh_RX D7
const int bufferSize = 29;
float currenttemp = 0.0,prevtemp = 0.0;
char receivedData[bufferSize] ; char prevreceivedData[bufferSize];
char temperature_data[8] = {'\0'}; char prevtemperature_data[8] = {'\0'};
char humidity_data[6] = {'\0'}; char prevhumidity_data[6] = {'\0'};
const unsigned long interval = 2000; // Interval in milliseconds (2 seconds)
unsigned long previousMillis = 0;
int count = 1;
#define TX D6
#define RX D5
SoftwareSerial witSerial(RX, TX);
SoftwareSerial SHT20Serial(sh_RX, -1);
int data;
void setup() {
pinMode(RX, INPUT);
pinMode(TX, OUTPUT);
Serial.begin(115200);
witSerial.begin(9600);
SHT20Serial.begin(115200);
}
void loop() {
SHT20Serial.listen();
if (SHT20Serial.isListening()) {
Serial.println("portOne is listening!");
int bytesRead = SHT20Serial.readBytesUntil('\n\r', receivedData, bufferSize - 1); //\r\n\r
if (isnan(bytesRead)) {
Serial.println("Failed to read from SHT20 sensor!");
return;
}
receivedData[bytesRead] = '\0'; // Null-terminate the string clears the string
Serial.print("Received data: ");
Serial.println(receivedData); Serial.println();
int temperature_index = 7;
int i;
for (i = 0; i < 5; i++) { //prev: 7
temperature_data[i] = receivedData[temperature_index + i];
}
int humidity_index = 23;
for (i = 0; i < 5; i++) {
humidity_data[i] = receivedData[humidity_index + i];
}
Serial.println("Temperature data: ");
Serial.println(temperature_data);
Serial.println("Humidity data: ");
Serial.println(humidity_data);
} else {
Serial.println("portOne is not listening!");
}
if (witSerial.isListening()) {
Serial.println("portTwo is listening!");
while (witSerial.available()>0) {
Serial.write(witSerial.read());
}
} else {
Serial.println("portTwo is not listening!");
}
}
Apart from very trivial examples I can't remember hearing of anyone who has succeeded in getting two instances of SoftwareSerial working
The ESP8266 actually has 2 hardware UARTs but because the second one's Rx pin is used for something else it cannot be used to receive data, only transmit
I'm surprised you got software serial working at all on an ESP8266, the interrupts that drive the WiFi mess up the timing for software serial. Asking software serial to work at 115200 Baud is also unlikely to be successful. You need a different solution, either a controller with 2 hardware ports or another device to provide the serial ports. I'm not familiar with the ESP32, but I think it has 2 serial ports (someone will correct me if I'm wrong).
the ESP8266 will support two serial ports using EspSoftwareSerial,
e.g. characters entered on the keyboard are transmitted to swSer1 (pins 14 and 12) and swSer2 (pins 13 and 15)
I have connected pin 14 to pin 12 and pin 13 to pin15 to form a lookback test therefore characters transmitted are echoed back and displayed
// ESP8266 using library EspSoftwareSerial
// https://www.arduino.cc/reference/en/libraries/espsoftwareserial/
#include <SoftwareSerial.h>
// pins Rx GPIO14 (D5) and Tx GPIO 12 (D6)
SoftwareSerial swSer1(14, 12);
SoftwareSerial swSer2(13, 15);
void setup() {
Serial.begin(115200); //Initialize hardware serial with baudrate of 115200
swSer1.begin(57600); //Initialize software serial with baudrate of 115200
swSer2.begin(57600); //Initialize software serial with baudrate of 115200
Serial.println("\nESP8266 Software serial test started");
}
void loop() {
while (swSer1.available() > 0) { //wait for data at software serial
Serial.write("1>"); //Send data recived from software serial to hardware serial
Serial.println((char)swSer1.read()); //Send data recived from software serial to hardware serial
}
while (Serial.available() > 0) { //wait for data at hardware serial
char ch;
swSer1.write(ch=Serial.read()); //send data recived from hardware serial to software serial
swSer2.write(ch); //send data recived from hardware serial to software serial
}
while (swSer2.available() > 0) { //wait for data at software serial
Serial.write("2>"); //Send data recived from software serial to hardware serial
Serial.println((char)swSer2.read()); //Send data recived from software serial to hardware serial
}
}
if I enter 123abcd on the keyboard the serial monitor displays
ESP8266 Software serial test started
2>1
2>2
2>3
1>1
1>2
1>3
2>a
2>b
2>c
2>d
1>a
1>b
1>c
1>d
note the above code is running at 57600baud - 115200 baud does not work
if you require 115200 baud I think you are going to have to switch to an ESP32 which can support two hardware serial ports