Here the part 2 of the reply I post previously,
Okay so the above one is just a demo of a multiplexer data logging 2 serial devices using one serial port at the same time.
Right now I want to data log even more so I add another serial device and my code is like this
//This softserial library Automatically sets TX as pin 9 and RX as pin 8.
#include <AltSoftSerial.h> //Include the software serial library
AltSoftSerial altSerial; //Name the software serial library altSerial (this cannot be omitted)
int s0 = 7; //Arduino pin 7 to control pin S0
int s1 = 6; //Arduino pin 6 to control pin S1
char computerdata[20]; //A 20 byte character array to hold incoming data from a pc/mac/other
char sensordata[30]; //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0; //We need to know how many characters bytes have been received
byte sensor_bytes_received=0; //We need to know how many characters bytes have been received
boolean datacomplete = false;
char *channel; //Char pointer used in string parsing
char *cmd; //Char pointer used in string parsing
void setup() {
pinMode(s1, OUTPUT); //Set the digital pin as output.
pinMode(s0, OUTPUT); //Set the digital pin as output.
Serial.begin(38400); //Set the hardware serial port to 38400
altSerial.begin(38400); //Set the soft serial port to 38400
channel = "2";
open_channel();
altSerial.print("f\r");
}
void serialEvent(){ //This interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received
computer_bytes_received=Serial.readBytesUntil(13,computerdata,20); //We read the data sent from the serial monitor(pc/mac/other) until we see a <CR>. We also count how many characters have been received
computerdata[computer_bytes_received]=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
}
void loop(){
if(computer_bytes_received!=0){ //If computer_bytes_received does not equal zero
channel=strtok(computerdata, ":"); //Let's parse the string at each colon
cmd=strtok(NULL, ":"); //Let's parse the string at each colon
open_channel(); //Call the function "open_channel" to open the correct data path
altSerial.print(cmd); //Send the command from the computer to the Atlas Scientific device using the softserial port
altSerial.print("\r"); //After we send the command we send a carriage return <CR>
computer_bytes_received=0; //Reset the var computer_bytes_received to equal 0
datacomplete= true;
}
if(datacomplete)
{
if(altSerial.available() > 0){ //If data has been transmitted from an Atlas Scientific device
sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received]=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
Serial.println(sensordata); //let’s transmit the data received from the Atlas Scientific device to the serial monitor
Serial.print("Channel:'");
Serial.print(channel);
Serial.println("'");
char *FirstValue = strtok (sensordata ,",");
Serial.println(FirstValue);
char *SecondValue =strtok(NULL,",");
Serial.println(SecondValue);
char *ThirdValue = strtok(NULL,",");
Serial.println(ThirdValue);
char *FourthValue = strtok(NULL, ",");
Serial.println(FourthValue);
}
datacomplete = false;
delay(1500);
}
channel = "0";
open_channel();
delay(500);
altSerial.print("r\r");
if(altSerial.available()>0)
{
sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received]=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
char *TotalVolume = strtok(sensordata ,",");
Serial.print("Make Up Total Volume:'");
Serial.print(TotalVolume);
Serial.println("'");
char *LiterPerHour = strtok(NULL, ",");
Serial.print("Make up Liter per hour :'");
Serial.print(LiterPerHour);
Serial.println("'");
char *LiterPerMinute = strtok(NULL,",");
Serial.print("Make up Liter per min :'");
Serial.print(LiterPerMinute);
Serial.println("'");
}
delay(2000);
channel = "1";
open_channel();
altSerial.print("r\r");
if(altSerial.available()>0)
{
sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received]=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
char *TotalVolume2 = strtok(sensordata ,",");
Serial.print("Blow Off Total Volume:'");
Serial.print(TotalVolume2);
Serial.println("'");
char *LiterPerHour2 = strtok(NULL, ",");
Serial.print("Blow off Liter per hour :'");
Serial.print(LiterPerHour2);
Serial.println("'");
char *LiterPerMinute2 = strtok(NULL,",");
Serial.print("Blow off Liter per min :'");
Serial.print(LiterPerMinute2);
Serial.println("'");
delay(1000);
}
delay(2000);
channel = "2";
open_channel();
altSerial.print("R\r");
if(altSerial.available()>0)
{
sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received]=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
char *phValue=strtok(sensordata,",");
float ph = atof(phValue);
Serial.print("Ph value :'");
Serial.print(ph);
Serial.println("'");
if(ph>=7.5){
Serial.println("high\r");
} //This is the proof that it has been converted into a float.
if(ph<7.5){
Serial.println("low\r");
} //This is the proof that it has been converted into a float.
delay(1000);
}
}
void open_channel(){ //This function controls what UART port is opened.
switch (*channel) { //Looking to see what channel to open
case '0': //If channel==0 then we open channel 0
digitalWrite(s0, LOW); //S0 and S1 control what channel opens
digitalWrite(s1, LOW); //S0 and S1 control what channel opens
break; //Exit switch case
case '1':
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
break;
case '2':
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
break;
case '3':
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
break;
}
}
and here's what I see on the serial monitor
