Page 1 of 1
Forum

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!




Operate two softwar...
 
Share:
Notifications
Clear all

Operate two software serials on esp8266

2 Posts
2 Users
1 Likes
468 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

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!");
}

}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2641
 

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)


   
harrypeter reacted
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: