I am able to read the pH, E.C. and DO values separately in separate serial monitors one at a time. Is it possible to take these measurements on one serial monitor without having to merge the codes in one file?
data sheets are as under:
The codes for reading the measurements are below:
Dissolved Oxygen:
#include <Wire.h> //enable I2C.
#define address 97 //default I2C ID number for EZO D.O. Circuit.
char computerdata[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other.
byte received_from_computer = 0; //we need to know how many characters have been received.
byte code = 0; //used to hold the I2C response code.
char DO_data[20]; //we make a 20 byte character array to hold incoming data from the D.O. circuit.
byte in_char = 0; //used as a 1 byte buffer to store inbound bytes from the D.O. Circuit.
int time_ = 600; //used to change the delay needed depending on the command sent to the EZO Class D.O. Circuit.
float DO_float; //float var used to hold the float value of the DO.
char *DO; //char pointer used in string parsing.
char *sat; //char pointer used in string parsing.
float do_float; //float var used to hold the float value of the dissolved oxygen.
float sat_float; //float var used to hold the float value of the saturation percentage.
void setup() //hardware initialization.
{
Serial.begin(9600); //enable serial port.
Wire.begin(); //enable I2C port.
}
void loop() { //the main loop.
byte i = 0; //counter used for DO_data array.
if (Serial.available() > 0) { //if data is holding in the serial buffer
received_from_computer = 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[received_from_computer] = 0; //stop the buffer from transmitting leftovers or garbage.
computerdata[0] = tolower(computerdata[0]); //we make sure the first char in the string is lower case.
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 600; //if a command has been sent to calibrate or take a reading we wait 600ms so that the circuit has time to take the reading.
else time_ = 300; //if not 300ms will do
Wire.beginTransmission(address); //call the circuit by its ID number.
Wire.write(computerdata); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
if (strcmp(computerdata, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
delay(time_); //wait the correct amount of time for the circuit to complete its instruction.
Wire.requestFrom(address, 20, 1); //call the circuit and request 20 bytes (this may be more than we need)
code = Wire.read(); //the first byte is the response code, we read this separately.
switch (code) { //switch case based on what the response code is.
case 1: //decimal 1.
Serial.println("Success"); //means the command was successful.
break; //exits the switch case.
case 2: //decimal 2.
Serial.println("Failed"); //means the command has failed.
break; //exits the switch case.
case 254: //decimal 254.
Serial.println("Pending"); //means the command has not yet been finished calculating.
break; //exits the switch case.
case 255: //decimal 255.
Serial.println("No Data"); //means there is no further data to send.
break; //exits the switch case.
}
while (Wire.available()) { //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
DO_data[i] = in_char; //load this byte into our array.
i += 1; //incur the counter for the array element.
if (in_char == 0) { //if we see that we have been sent a null command.
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
}
}
if (isDigit(DO_data[0])) {
string_pars(); //If the first char is a number we know it is a DO reading, lets parse the DO reading
}
else { //if it’s not a number
Serial.println(DO_data); //print the data.
for (i = 0; i < 20; i++) { //step through each char
DO_data[i] = 0; //set each one to 0 this clears the memory
}
}
}
}
}
void string_pars() { //this function will break up the CSV string into its 2 individual parts, DO and %sat.
byte flag = 0; //this is used to indicate is a “,” was found in the string array
byte i = 0; //counter used for DO_data array.
for (i = 0; i < 20; i++) { //Step through each char
if (DO_data[i] == ',') { //do we see a ','
flag = 1; //if so we set the var flag to 1 by doing this we can identify if the string being sent from the DO circuit is a CSV string containing tow values
}
}
if (flag != 1) { //if we see the there WAS NOT a ‘,’ in the string array
Serial.print("DO:"); //print the identifier
Serial.println(DO_data); //print the reading
}
if (flag == 1) { //if we see the there was a ‘,’ in the string array
DO = strtok(DO_data, ","); //let's pars the string at each comma
sat = strtok(NULL, ","); //let's pars the string at each comma
Serial.print("DO:"); //print the identifier
Serial.println(DO); //print the reading
Serial.print("Sat:"); //print the identifier
Serial.println(sat); //print the reading
flag = 0; //reset the flag
}
/* //uncomment this section if you want to take the ASCII values and convert them into a floating point number.
DO_float=atof(DO);
sat_float=atof(sat);
*/
}
Thank you very much Delta_G. I am trying to combine the codes for all these sensors but getting compile error. I am still in the process of learning. Can you please share any code that combines various sensors to read all at the same time for myself to learn doing it?
I would be very grateful to you as im in the middle of my project and stuck at this stage.
Thanks Global Moderator. Actually i did try to include all the sensor codes but it was exceeding the number of words allowed. i am trying post the codes for E.C. and pH again as Dissolved oxygen code was posted in my first post.
//**Code for measuring electrical conductivity**
#include <Wire.h> //enable I2C.
#define address 100 //default I2C ID number for EZO EC Circuit.
char computerdata[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other.
byte received_from_computer = 0; //we need to know how many characters have been received.
bool serial_event = false; //a flag to signal when data has been received from the pc/mac/other.
byte code = 0; //used to hold the I2C response code.
char ec_data[48]; //we make a 48 byte character array to hold incoming data from the EC circuit.
byte in_char = 0; //used as a 1 byte buffer to store inbound bytes from the EC Circuit.
byte i = 0; //counter used for ec_data array.
int delay_time = 600; //used to change the delay needed depending on the command sent to the EZO Class EC Circuit.
char *ec; //char pointer used in string parsing.
char *tds; //char pointer used in string parsing.
char *sal; //char pointer used in string parsing.
char *sg; //char pointer used in string parsing.
float ec_float; //float var used to hold the float value of the conductivity.
float tds_float; //float var used to hold the float value of the TDS.
float sal_float; //float var used to hold the float value of the salinity.
float sg_float; //float var used to hold the float value of the specific gravity.
void setup() //hardware initialization.
{
Serial.begin(9600); //enable serial port.
Wire.begin(); //enable I2C port.
}
void serialEvent() { //this interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received.
received_from_computer = 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[received_from_computer] = 0; //stop the buffer from transmitting leftovers or garbage.
serial_event = true; //set the serial event flag.
}
void loop() { //the main loop.
if (serial_event == true) { //if a command was sent to the EC circuit.
computerdata[0] = tolower(computerdata[0]); //we make sure the first char in the string is lower case
if (computerdata[0] == 'c' || computerdata[0] == 'r')delay_time = 600; //if a command has been sent to calibrate or take a reading we wait 600ms so that the circuit has enough time to take the reading.
else delay_time = 300; //if any other command has been sent we wait only 300ms.
Wire.beginTransmission(address); //call the circuit by its ID number.
Wire.write(computerdata); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
if (strcmp(computerdata, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the EC circuit.
delay(delay_time); //wait the correct amount of time for the circuit to complete its instruction.
Wire.requestFrom(address, 48, 1); //call the circuit and request 48 bytes (this is more than we need)
code = Wire.read(); //the first byte is the response code, we read this separately.
while (Wire.available()) { //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
ec_data[i] = in_char; //load this byte into our array.
i += 1; //incur the counter for the array element.
if (in_char == 0) { //if we see that we have been sent a null command.
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
}
}
switch (code) { //switch case based on what the response code is.
case 1: //decimal 1.
Serial.println("Success"); //means the command was successful.
break; //exits the switch case.
case 2: //decimal 2.
Serial.println("Failed"); //means the command has failed.
break; //exits the switch case.
case 254: //decimal 254.
Serial.println("Pending"); //means the command has not yet been finished calculating.
break; //exits the switch case.
case 255: //decimal 255.
Serial.println("No Data"); //means there is no further data to send.
break; //exits the switch case.
}
Serial.println(ec_data); //print the data.
}
serial_event = false; //reset the serial event flag.
//if(computerdata[0]=='r') string_pars(); //uncomment this function if you would like to break up the comma separated string into its individual parts.
}
}
void string_pars() { //this function will break up the CSV string into its 4 individual parts. EC|TDS|SAL|SG.
//this is done using the C command “strtok”.
ec = strtok(ec_data, ","); //let's pars the string at each comma.
tds = strtok(NULL, ","); //let's pars the string at each comma.
sal = strtok(NULL, ","); //let's pars the string at each comma.
sg = strtok(NULL, ","); //let's pars the string at each comma.
Serial.print("EC:"); //we now print each value we parsed separately.
Serial.println(ec); //this is the EC value.
Serial.print("TDS:"); //we now print each value we parsed separately.
Serial.println(tds); //this is the TDS value.
Serial.print("SAL:"); //we now print each value we parsed separately.
Serial.println(sal); //this is the salinity value.
Serial.print("SG:"); //we now print each value we parsed separately.
Serial.println(sg); //this is the specific gravity.
//uncomment this section if you want to take the values and convert them into floating point number.
/*
ec_float=atof(ec);
tds_float=atof(tds);
sal_float=atof(sal);
sg_float=atof(sg);
*/
}
I'm not sure what the difficulty is. You have three peripherals on the 12c bus, on addresses 97, 99, and 100. Your arduino listens to the serial. When it receives a message, it parses that message and kicks off a process that talks to the relevant peripheral.
As for your syntax errors, you have way too many closing braces. Use ctrl/cmd-T to auto format.
This:
Wire.beginTransmission(address_DO); //call the circuit by its ID number.
Wire.beginTransmission(address_EC);
Wire.beginTransmission(address_pH);
Wire.write(computerdata); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
really isn't going to work. An i2c bus talks to one thing at a time.
If I was going to write a sketch for you in Gigs and Collaborations, I'd ask "what, exactly, do you want to send to the arduino on the serial, and what, exactly, do you require the arduino to send back"? The PDFs you linked to are fairly clear on what needs to be sent to the peripherals.
i want to send all the measurements from these sensors on the serial monitor which i then want to send to labview serial interface. can you please suggest the way forward?
There is no block in labview that would connect to Ameba RTL8195 board im using. I have already displayed individual sensor's measurement on labview serial communication (continuous serial read and write program) after running the sensor's program on Ameba and sending it to serial monitor. the reason of using labview is the display and interface features it offers.
sal_murd:
i want to send all the measurements from these sensors on the serial monitor which i then want to send to labview serial interface. can you please suggest the way forward?
I'm not sure what the 'labview serial interface' might be. Do you mean there's some sort of piece of equipment that connects to a different pin on the arduino?
As for the readings, you open i2c to each in turn and ask them to take a reading. You then poll each of them with maybe a delay between retries until you no longer get the 'still processing' status. The result from each might be an error or might be the data you need.
PaulMurrayCbr:
As for the readings, you open i2c to each in turn and ask them to take a reading. You then poll each of them with maybe a delay between retries until you no longer get the 'still processing' status. The result from each might be an error or might be the data you need.
I am not able to open i2c to each sensor as it generates error. my code for taking readings from 2 sensors is attached as i could not include code here due to maximum allowed character length.
The error I receive is :
Arduino: 1.8.1 (Windows 7), Board: "Ameba RTL8195A"
C:\Users\eng.EEPROJ-DP.000\Desktop\ph_ec_do\ph_ec_do.ino: In function 'void setup()':
ph_ec_do:147: error: redefinition of 'void setup()'
void setup() //hardware initialization.
^
ph_ec_do:40: error: 'void setup()' previously defined here
void setup() //hardware initialization.
^
C:\Users\eng.EEPROJ-DP.000\Desktop\ph_ec_do\ph_ec_do.ino: In function 'void loop()':
ph_ec_do:155: error: redefinition of 'void loop()'
void loop() { //the main loop.
^
ph_ec_do:49: error: 'void loop()' previously defined here
void loop() { //the main loop.
^
exit status 1
redefinition of 'void setup()'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
C:\Users\eng.EEPROJ-DP.000\Desktop\ph_ec_do\ph_ec_do.ino: In function 'void loop()':
ph_ec_do:109: error: 'pH_data' was not declared in this scope
pH_data = in_char_pH;
^* ph_ec_do:117: error: 'pH_data' was not declared in this scope pH_data*=in_char_pH;
_^_ ph_ec_do:119: error: 'string_pars' was not declared in this scope
string_pars(); //If the first char is a number we know it is a DO reading, lets parse the DO reading* * ^* ph_ec_do:133: error: a function-definition is not allowed here before '{' token void string_pars() { //this function will break up the CSV string into its 2 individual parts, DO and %sat. * ^* ph_ec_do:163: error: expected '}' at end of input } ^ exit status 1 'pH_data' was not declared in this scope This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences. can you please help me correct the mistake? ph_ec_do.ino (9.12 KB)
please help in reading the sensors (dissolved oxygen and pH). My code is attached as i could not included here due to maximum allowed length of the message.
According to the datasheets provided, sending character R results in reading and gives measurement.however, im only able to read dissolved oxygen sensor and not pH(i can observe the green LED blink on the sensor). The response on serial monitor is as below:
No Data from pH
#include <Wire.h>
#define address 97
#define address_pH 99
char computerdata[20];
byte received_from_computer = 0;
byte code = 0;
char DO_data[20];
byte in_char = 0;
int time_ = 900;
float DO_float;
char *DO;
char *sat;
float do_float;
float sat_float;
char computerdata_pH[20];
byte received_from_computer_pH = 0;
byte code_pH = 0;
char pH_data[20];
byte in_char_pH = 0;
byte i = 0;
int time_pH = 900;
float ph_float;
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop() {
byte i = 0;
if (Serial.available() > 0) {
received_from_computer = Serial.readBytesUntil(13, computerdata, 20);
computerdata[received_from_computer] = 0;
computerdata[0] = tolower(computerdata[0]);
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 900;
else time_ = 300;
Wire.beginTransmission(address);
Wire.write(computerdata);
if (strcmp(computerdata, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
delay(time_);
Wire.requestFrom(address, 20, 1);
code = Wire.read();
switch (code) {
case 1:
Serial.println("Success");
break;
case 2:
Serial.println("Failed");
break;
case 254:
Serial.println("Pending");
break;
case 255:
Serial.println("No Data from do");
break;
}
}
while (Wire.available()) {
in_char = Wire.read();
DO_data[i] = in_char;
i += 1;
if (in_char == 0 || in_char_pH == 0) {
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
Serial.println(DO_data);
delay(100);
Wire.beginTransmission(address_pH); //call the circuit by its ID number.
Wire.write(computerdata_pH); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
if (isDigit(DO_data[0])) {
string_pars(); //If the first char is a number we know it is a DO reading, lets parse the DO reading
}
else { //if it’s not a number
Serial.println(DO_data); //print the data.
for (i = 0; i < 20; i++) { //step through each char
DO_data[i] = 0; //set each one to 0 this clears the memory
}
}
if (strcmp(computerdata_pH, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
}
}
}
}
if (Serial.available() > 0) {
received_from_computer = Serial.readBytesUntil(13, computerdata, 20);
computerdata[0] = tolower(computerdata[0]);
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 900;
else time_ = 300;
Wire.beginTransmission(address);
Wire.write(computerdata);
delay(time_pH);
Wire.requestFrom(address_pH, 20, 1);
code_pH = Wire.read();
switch (code_pH) {
case 1:
Serial.println("Success");
break;
case 2:
Serial.println("Failed");
break;
case 254:
Serial.println("Pending");
break;
case 255:
Serial.println("No Data from pH");
break;
}
while (Wire.available()) {
in_char_pH = Wire.read();
pH_data[i] = in_char_pH;
i += 1;
if (in_char_pH == 0) {
i = 0;
Wire.endTransmission();
break;
}
}
Serial.println(pH_data);
}
}
void string_pars() { //this function will break up the CSV string into its 2 individual parts, DO and %sat.
byte flag = 0; //this is used to indicate is a “,” was found in the string array
byte i = 0; //counter used for DO_data array.
for (i = 0; i < 20; i++) { //Step through each char
if (DO_data[i] == ',') { //do we see a ','
flag = 1; //if so we set the var flag to 1 by doing this we can identify if the string being sent from the DO circuit is a CSV string containing tow values
}
}
if (flag != 1) { //if we see the there WAS NOT a ‘,’ in the string array
Serial.print("DO:"); //print the identifier
Serial.println(DO_data); //print the reading
}
if (flag == 1) { //if we see the there was a ‘,’ in the string array
DO = strtok(DO_data, ","); //let's pars the string at each comma
sat = strtok(NULL, ","); //let's pars the string at each comma
Serial.print("DO:"); //print the identifier
Serial.println(DO); //print the reading
Serial.print("Sat:"); //print the identifier
Serial.println(sat); //print the reading
flag = 0; //reset the flag
}
/* //uncomment this section if you want to take the ASCII values and convert them into a floating point number.
DO_float=atof(DO);
sat_float=atof(sat);
*/
}
Programming hint: to avoid per-line comments, make your code read like a story.
Example, your function "string_pars"[sic].
You have a variable called "flag".
Is it any particular flag?
Yes it is - it indicates the presence or absence of a comma.
So why not make it a Boolean, and call it 'commaFound" ?
Then a simple if/else will suffice.
I have modified my code and can read both sensors but only after sending a character R in the serial monitor. is there any way to include sending R in the code in itself so as get the readings without user intervention? please help , my code is under:
#include <Wire.h>
#define address 97
#define address_pH 99
char computerdata[20];
byte received_from_computer = 0;
byte code = 0;
char DO_data[20];
byte in_char = 0;
int time_ = 900;
float DO_float;
char *DO;
char *sat;
float do_float;
float sat_float;
char computerdata_pH[20];
byte received_from_computer_pH = 0;
byte code_pH = 0;
char pH_data[20];
byte in_char_pH = 0;
byte i = 0;
int time_pH = 900;
float ph_float;
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop() {
byte i = 0;
if (Serial.available() > 0) {
received_from_computer = Serial.readBytesUntil(13, computerdata, 20);
computerdata[received_from_computer] = 0;
computerdata[0] = tolower(computerdata[0]);
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 900;
else time_ = 300;
Wire.beginTransmission(address);
Wire.write(computerdata);
if (strcmp(computerdata, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
delay(time_);
Wire.requestFrom(address, 20, 1);
code = Wire.read();
switch (code) {
case 1:
Serial.println("Success");
break;
case 2:
Serial.println("Failed");
break;
case 254:
Serial.println("Pending");
break;
case 255:
Serial.println("No Data from do");
break;
}
}
while (Wire.available()) {
in_char = Wire.read();
DO_data[i] = in_char;
i += 1;
if (in_char == 0 || in_char_pH == 0) {
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
Serial.println(DO_data);
delay(100);
Wire.beginTransmission(address_pH); //call the circuit by its ID number.
Wire.write(computerdata_pH); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
if (isDigit(DO_data[0])) {
string_pars(); //If the first char is a number we know it is a DO reading, lets parse the DO reading
}
else { //if it’s not a number
Serial.println(DO_data); //print the data.
for (i = 0; i < 20; i++) { //step through each char
DO_data[i] = 0; //set each one to 0 this clears the memory
}
}
if (strcmp(computerdata_pH, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
}
}
}
}
if (Serial.available() > 0) {
received_from_computer = Serial.readBytesUntil(13, computerdata, 20);
computerdata[0] = tolower(computerdata[0]);
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 900;
else time_ = 300;
Wire.beginTransmission(address_pH);
Wire.write(computerdata);
delay(time_pH);
Wire.requestFrom(address_pH, 20, 1);
code_pH = Wire.read();
switch (code_pH) {
case 1:
Serial.println("Success");
break;
case 2:
Serial.println("Failed");
break;
case 254:
Serial.println("Pending");
break;
case 255:
Serial.println("No Data from pH");
break;
}
while (Wire.available()) {
in_char_pH = Wire.read();
pH_data[i] = in_char_pH;
i += 1;
if (in_char_pH == 0) {
i = 0;
Wire.endTransmission();
break;
}
}
Serial.println(pH_data);
}
}
void string_pars() { //this function will break up the CSV string into its 2 individual parts, DO and %sat.
byte flag = 0; //this is used to indicate is a “,” was found in the string array
byte i = 0; //counter used for DO_data array.
for (i = 0; i < 20; i++) { //Step through each char
if (DO_data[i] == ',') { //do we see a ','
flag = 1; //if so we set the var flag to 1 by doing this we can identify if the string being sent from the DO circuit is a CSV string containing tow values
}
}
if (flag != 1) { //if we see the there WAS NOT a ‘,’ in the string array
Serial.print("DO:"); //print the identifier
Serial.println(DO_data); //print the reading
}
if (flag == 1) { //if we see the there was a ‘,’ in the string array
DO = strtok(DO_data, ","); //let's pars the string at each comma
sat = strtok(NULL, ","); //let's pars the string at each comma
Serial.print("DO:"); //print the identifier
Serial.println(DO); //print the reading
Serial.print("Sat:"); //print the identifier
Serial.println(sat); //print the reading
flag = 0; //reset the flag
}
/* //uncomment this section if you want to take the ASCII values and convert them into a floating point number.
DO_float=atof(DO);
sat_float=atof(sat);
*/
}
Please suggest the method for Sending character R followed by carriage return in the program itself rather than for user input in the serial monitor to read from these sensors.
#include <Wire.h>
#define DEV_ID 0x61 //DO
#define DEV_ID2 0x63 //pH
#define DEV_ID3 0x64 //EC
char computerdata[20];
byte received_from_computer = 0;
byte code = 0;
char DO_data[20];
byte in_char = 0;
int time_ = 600;
float DO_float;
char *DO;
char *sat;
float do_float;
float sat_float;
char computerdata_EC[20];
byte received_from_computer_EC = 0;
bool serial_event = false;
byte code_EC = 0;
char ec_data[48];
byte in_char_EC = 0;
byte i = 0;
int delay_time = 600;
char *ec;
char *tds;
char *sal;
char *sg;
float ec_float;
float tds_float;
float sal_float;
float sg_float;
char pH_data[20];
int time_pH = 900;
void setup()
{
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(DEV_ID);
Wire.write(0x52);
Wire.endTransmission();
Wire.begin();
Wire.beginTransmission(DEV_ID2);
Wire.write(0x52);
Wire.endTransmission();
Wire.beginTransmission(DEV_ID3);
Wire.write(0x52);
Wire.endTransmission();
}
void loop()
{
byte i = 0;
if (Serial.available() > 0) {
received_from_computer = Serial.readBytesUntil(13, computerdata, 20);
computerdata[received_from_computer] = 0;
computerdata[0] = tolower(computerdata[0]);
if (computerdata[0] == 'c' || computerdata[0] == 'r')time_ = 600;
else time_ = 300;
Wire.beginTransmission(DEV_ID); //call the circuit by its ID number.
Wire.write(computerdata); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
if (strcmp(computerdata, "sleep") != 0) { //if the command that has been sent is NOT the sleep command, wait the correct amount of time and request data.
//if it is the sleep command, we do nothing. Issuing a sleep command and then requesting data will wake the D.O. circuit.
delay(time_);
Wire.requestFrom(DEV_ID, 20, 1);
code = Wire.read();
switch (code) {
case 1:
Serial.println("Success");
break;
case 2:
Serial.println("Failed");
break;
case 254:
Serial.println("Pending");
break;
case 255: //decimal 255.
Serial.println("No Data"); //means there is no further data to send.
break; //exits the switch case.
}
while (Wire.available()) { //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
DO_data[i] = in_char; //load this byte into our array.
i += 1; //incur the counter for the array element.
if (in_char == 0) { //if we see that we have been sent a null command.
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
}
}
if (isDigit(DO_data[0])) {
string_pars();
}
else {
Serial.println(DO_data);
for (i = 0; i < 20; i++) {
DO_data[i] = 0;
}
}
}
Wire.beginTransmission(DEV_ID2);
Wire.write(computerdata);
Wire.endTransmission();
if (strcmp(computerdata, "sleep") != 0) {
code = Wire.read(); //the first byte is the response code, we read this separately.
delay(time_pH); //wait the correct amount of time for the circuit to complete its instruction.
Wire.requestFrom(DEV_ID2, 20, 1); //call the circuit and request 20 bytes (this may be more than we need)
code = Wire.read(); //the first byte is the response code, we read this separately.
switch (code) { //switch case based on what the response code is.
case 1: //decimal 1.
Serial.println("Success"); //means the command was successful.
break; //exits the switch case.
case 2: //decimal 2.
Serial.println("Failed"); //means the command has failed.
break; //exits the switch case.
case 254: //decimal 254.
Serial.println("Pending"); //means the command has not yet been finished calculating.
break; //exits the switch case.
case 255: //decimal 255.
Serial.println("No Data"); //means there is no further data to send.
break; //exits the switch case.
}
while (Wire.available()) { //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
pH_data[i] = in_char; //load this byte into our array.
i += 1; //incur the counter for the array element.
if (in_char == 0) { //if we see that we have been sent a null command.
i = 0; //reset the counter i to 0.
Wire.endTransmission(); //end the I2C data transmission.
break; //exit the while loop.
}
}
Serial.println(pH_data); //print the data.
}
Wire.beginTransmission(DEV_ID3);
Wire.write(computerdata);
Wire.endTransmission();
if (strcmp(computerdata, "sleep") != 0) {
delay(delay_time);
Wire.requestFrom(DEV_ID3, 48, 1);
code = Wire.read();
while (Wire.available()) {
in_char = Wire.read();
ec_data[i] = in_char;
i += 1;
if (in_char == 0) {
i = 0;
Wire.endTransmission();
break;
}
}
code = Wire.read();
switch (code) { //switch case based on what the response code is.
case 1: //decimal 1.
Serial.println("Success"); //means the command was successful.
break; //exits the switch case.
case 2: //decimal 2.
Serial.println("Failed"); //means the command has failed.
break; //exits the switch case.
case 254:
Serial.println("Pending");
break;
case 255:
Serial.println("No Data");
break;
}
Serial.println(ec_data);
}
serial_event = false;
}
}
void string_pars() {
byte flag = 0;
byte i = 0;
for (i = 0; i < 20; i++) {
if (DO_data[i] == ',') {
flag = 1;
}
}
if (flag != 1) { //if we see the there WAS NOT a ‘,’ in the string array
Serial.print("DO:"); //print the identifier
Serial.println(DO_data); //print the reading
}
if (flag == 1) { //if we see the there was a ‘,’ in the string array
DO = strtok(DO_data, ","); //let's pars the string at each comma
sat = strtok(NULL, ","); //let's pars the string at each comma
Serial.print("DO:"); //print the identifier
Serial.println(DO); //print the reading
Serial.print("Sat:"); //print the identifier
Serial.println(sat); //print the reading
flag = 0; //reset the flag
}
}