I am trying to read two sensors (ORP and PH) that need serial to work. Since the Uno only has one hardware serial I need to use software serial.
I have created two software serial connections and am trying to read them. From everything I can tell it is only using the first one I start. The second one is not able to be used. I read the software serial page here and it states that two cannot be used at the same time. I am putting delays in but that is not working.
Here is the code. Any thoughts?
#include <OneWire.h>
#include <SoftwareSerial.h>
char SensorString[15];
OneWire ds(9); // thermometer on digital pin 9
//ORP sensor settings
#define ORPrxpin 2
#define ORPtxpin 3
SoftwareSerial ORPserial(ORPrxpin, ORPtxpin);
//PH sensor settings
#define PHrxpin 5
#define PHtxpin 6
SoftwareSerial PHserial(PHrxpin, PHtxpin);
boolean Sensor_StringComplete = false;
/***********************************************************************
** ftoa Function **
***********************************************************************/
char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
/***********************************************************************
** setup Function **
***********************************************************************/
void setup(){
Serial.begin(9600);
PHserial.begin(38400);
//setup PH sensor
PHserial.write("e\r");//turn off auto read
delay(50);
PHserial.write("e\r");//turn off auto read
delay(1000);
ORPserial.begin(38400);
ORPserial.write("e\r");
delay(50);
ORPserial.write("e\r");
delay(50);
}
/***********************************************************************
** getTemp Function **
***********************************************************************/
void *getTemp(char * tempType, char * sensorString) {
byte data[12];
byte addr[8];
char *tempStr;
float temperatureSum;
if ( !ds.search(addr))
{
//no more sensors on chain, reset search
ds.reset_search();
sensorString == "-1000";
}
if ( OneWire::crc8( addr, 7) != addr[7])
{
Serial.println(F("wrong CRC"));
sensorString == "-1000";
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
sensorString == "-1000";
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(750);
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++)
{ // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
if (strcmp(tempType,"C") == 0 ) {
temperatureSum = (tempRead / 16.0);// celcius
}
else if (strcmp(tempType,"F") ==0 ) {
temperatureSum = (tempRead / 16.0)*(9.0/5.0) + 32.0;//fahrenheit
}
else {
temperatureSum = 999.99;
}
ftoa(sensorString,temperatureSum,2);
}
/***********************************************************************
** getAtlanticSensorData Function **
***********************************************************************/
void getAtlasSensorData(char tranType[3], char tranAction[6],char * sensorString){
/* PH Tran Types
ALL tran types must be followed by <CR>
ALL responses will be followed by <CR>
Calibration must be in continuous mode
L1 - Enable Debug LEDs (No Response)
L0 - Disable LEDs (No Response)
R - Return a single reading (up to 8 chars e.g. -1024.00)
C - Enter continuous mode (read every 378 ms, returns same as R)
TT.TT - Take temperature dependent reading (tt.tt is degrees celcius)
E - End continuous mode
S - Calibrate at PH Seven (must be in continuous mode)
F - Calibrate at PH Four (must be in continuous mode)
T - Calibrate at PH Ten (must be in continuous mode)s
I - Returns version (type,version,date e.g. O,V4.4,2/13<CR>)
# - Set Device ID
Z0 - Set auto baud rate
Z# - Set specific baud rate (# is single digit representation or baud rate)
*/
/* ORP Tran Types
ALL tran types must be followed by <CR>
ALL responses will be followed by <CR>
Calibration must be in continuous mode
L1 - Enable Debug LEDs (No Response)
L0 - Disable LEDs (No Response)
R - Return a single reading (up to 8 chars e.g. -1024.00)
C - Enter continuous mode (read every 320 ms, returns same as R)
E - End continuous mode
+ - Calibration add 1 mV to the original ORP reading
- - Calibration subtract 1 mV from the original ORP reading
X - Return to factory settings
I - Returns version (type,version,date e.g. O,V4.4,2/13<CR>)
Z0 - Set auto baud rate
Z# - Set specific baud rate (# is single digit representation or baud rate)
*/
// char temp[5] = "";
// char tempType[2] = "C";
byte cind = 0;
// memset(SensorString,0,UDP_TX_PACKET_MAX_SIZE);
//Serial.println(tranAction);
if (strcmp(tranType,"PH") == 0){
// getTemp(tempType,temp); //get temp in celcius for PH read
// Serial.print("Sending...");
// Serial.print(tranAction);
// Serial.println('-');
// PHserial.print(tranAction);
PHserial.print('R');
PHserial.print('\r');
while(PHserial.available()){
Serial.println("PH serial is available");
char inchar = (char)PHserial.read();
Serial.print("inchar=");
Serial.print(inchar);
if (inchar == '\r') {
Sensor_StringComplete = true;
} else {
sensorString[cind++] = inchar;
}
}
} else {
ORPserial.print(tranAction);
ORPserial.print('\r');
while(ORPserial.available()){
char inchar = (char)ORPserial.read();
if (inchar == '\r') {
Sensor_StringComplete = true;
} else {
sensorString[cind++] = inchar;
}
}
}
if (Sensor_StringComplete){
Sensor_StringComplete = false;
}
}
/***********************************************************************
** loop Function **
***********************************************************************/
void loop(){
char s_tempC[8]="";
char s_tempF[8]="";
char s_ORP[8]="";
char s_PH[8]="";
// char writeStr[36];
getTemp("F",s_tempF);
getTemp("C",s_tempC);
getAtlasSensorData("PH",s_tempC,s_PH);
delay(1000);
getAtlasSensorData("ORP","R",s_ORP);
delay(1000);
Serial.print("Temp C=");
Serial.println(s_tempC);
Serial.print("Temp F=");
Serial.println(s_tempF);
Serial.print("ORP=");
Serial.println(s_ORP);
Serial.print("PH=");
Serial.println(s_PH);
delay(1000);
}