Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Operate two software serials on esp8266
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter
August 26, 2023 2:56 AM
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 the those sensors and can't change them now). Please help me(it's a bit urgent task), 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!");
}
}
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
August 30, 2023 3:21 AM
I'm on vacation at the moment, so I do not have much time to respond.
However ... you may want to look in using an ESP32 instead (if that is an option).
The cool part is that an ESP32 has 2 cores which can be used as such even with Arduino code.
Maybe (!) this way you can handle two software serial ports?
(may have to read up on it though)
(@aliarifat794)
New Member
Joined: 4 months ago
Posts: 2
October 7, 2024 9:29 AM
The ESP8266 has a hardware UART (UART0) that you can use for one sensor, and you can use SoftwareSerial
for the other sensor.