I'm trying to read three sensors using softserial. If I try to listen to more than one port It doesn't read any of the sensors. If I only try to read one it works fine.
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it.
SoftwareSerial myserial1(2, 3); //define how the soft serial port is going to work.
SoftwareSerial myserial2(4, 5); //define how the soft serial port is going to work.
SoftwareSerial myserial3(6, 7); //define how the soft serial port is going to work.
char tds_data[20]; //we make a 20 byte character array to hold incoming data from the tds.
char o2_data[20]; //we make a 20 byte character array to hold incoming data from the o2.
char ph_data[20]; //we make a 20 byte character array to hold incoming data from the pH.
byte received_from_sensor=0; //we need to know how many characters have been received.
float o2=0; //used to hold a floating point number that is the o2.
float ph=0; //used to hold a floating point number that is the pH.
float tds=0; //used to hold a floating point number that is the tds.
byte string_received=0; //used to identify when we have received a string from the pH circuit.
int Data1,Data2,Data3,Data4,Data5,Data6,Data7;
unsigned long startMillis = 0;
String parseIt = 0;
int curPos = 3;
boolean isComma = false;
String tmpWholeStr = 0;
String tmpStr = 0;
void setup(){
Serial.begin(9600); //enable the hardware serial port
myserial1.begin(38400); //enable the software serial port
myserial2.begin(38400); //enable the software serial port
myserial3.begin(38400); //enable the software serial port
}
void loop(){
Serial.println("Looping...");
myserial1.listen();
if(myserial1.available() > 0){ //if we see that the o2 Circuit has sent a character.
received_from_sensor=myserial1.readBytesUntil(13,o2_data,20); //we read the data sent from o2 Circuit until we see a <CR>. We also count how many character have been received.
o2_data[received_from_sensor]=0; //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer.
string_received=1; //a flag used when the Arduino is controlling the o2 Circuit to let us know that a complete string has been received.
}
myserial1.print("R\r"); //send it the command to take a single reading.
if(string_received==1){ //did we get data back from the o2 Circuit?
o2=atof(o2_data); //many people ask us "how do I convert a sting into a float?" This is how...
int o21 = (int)o2; //Read Altitude digits on the left of the decimal place
int o22 = (int)((o2 - o21) * 100.0); //Read Altitude digits on the right of the decimal place
Serial.print("O2 ");
Serial.print(o21);
Serial.print(".");
Serial.println(o22);
string_received=0;} //reset the string received flag.
// myserial2.listen();
if(myserial2.available() > 0){ //if we see that the pH Circuit has sent a character.
received_from_sensor=myserial2.readBytesUntil(13,ph_data,20); //we read the data sent from pH Circuit until we see a <CR>. We also count how many character have been received.
ph_data[received_from_sensor]=0; //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer.
string_received=1; //a flag used when the Arduino is controlling the pH Circuit to let us know that a complete string has been received.
}
myserial2.print("R\r"); //send it the command to take a single reading.
if(string_received==1){ //did we get data back from the ph Circuit?
ph=atof(ph_data); //many people ask us "how do I convert a sting into a float?" This is how...
int ph1 = (int)ph; //Read Altitude digits on the left of the decimal place
int ph2 = (int)((ph - ph1) * 100.0); //Read Altitude digits on the right of the decimal place
Serial.print("PH ");
Serial.print(ph1);
Serial.print(".");
Serial.println(ph2);
string_received=0;} //reset the string received flag.
// myserial3.listen();
if(myserial3.available() > 0){ //if we see that the tds Circuit has sent a character.
received_from_sensor=myserial3.readBytesUntil(13,tds_data,20); //we read the data sent from tds Circuit until we see a <CR>. We also count how many character have been received.
tds_data[received_from_sensor]=0; //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer.
string_received=1; //a flag used when the Arduino is controlling the tds Circuit to let us know that a complete string has been received.
}
myserial3.print("R\r"); //send it the command to take a single reading.
if(string_received==1){ //did we get data back from the tds Circuit?
tds=atof(tds_data); //many people ask us "how do I convert a sting into a float?" This is how...
Serial.print("TDS ");
Serial.println(tds_data);
{
parseIt = tds_data;
curPos = 0;
isComma = false;
String tmpWholeStr = 0;
tmpStr = 0;
//Sensor01
while(!isComma) {
tmpStr = String(parseIt.charAt(curPos));
if (tmpStr == String(',')) {
Data1 = tmpWholeStr.toInt();
curPos++;
isComma = true;
continue;
}
tmpWholeStr += tmpStr;
curPos++;
// break;
}
//Serial.println("Sensor03: " + tmpWholeStr);
isComma = false;
tmpWholeStr = 0;
tmpStr = 0;
//Sensor03 First Value
//Sensor03 Second Value
while(!isComma) {
tmpStr = String(parseIt.charAt(curPos));
if (tmpStr == String(',')) {
Data2 = tmpWholeStr.toInt();
curPos++;
isComma = true;
continue;
}
tmpWholeStr += tmpStr;
curPos++;
// break;
}
isComma = false;
tmpWholeStr = 0;
tmpStr = 0;
//Sensor03 First Value
//Sensor03 Second Value
while(!isComma) {
tmpStr = String(parseIt.charAt(curPos));
if (tmpStr == String(',')) {
Data3 = tmpWholeStr.toInt();
curPos++;
isComma = true;
continue;
}
tmpWholeStr += tmpStr;
curPos++;
break;
}
isComma = false;
tmpWholeStr = 0;
tmpStr = 0;
}
Serial.println("TDS: " + String(Data1 + String(',') + Data2 + String(',')+ Data3));
string_received=0;} //reset the string received flag.
delay(800); //we will take a reading ever 800ms. You can make this much longer or shorter if you like.
}