#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <DS3231.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#define RX 10
#define TX 11
#define ONE_WIRE_BUS 5
const int chipSelect = 53;
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false; //have we received all the data from the PC
boolean sensor_string_complete = false; //have we received all the data from the Atlas Scientific product
float pH;
String inputstringdo = "";
String sensorstringdo = "";
boolean input_string_completedo = false;
boolean sensor_string_completedo = false;
float DO;
String AP ="MySpectrumWifif8-2G"; // CHANGE ME
String PASS ="longspark249"; // CHANGE ME
String API = "A7XJSXSGZT9PG7YP"; // CHANGE ME
String APIII = "3LKWE0CQXARPIYKN"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
String fieldb = "field2";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
File dataFile;
SoftwareSerial esp8266(RX, TX);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DS3231 rtc(SDA, SCL);
#define I2C_ADDR 0x27 // Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define ONE_WIRE_BUS 5
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
void setup()
{
Serial.begin(9600);
esp8266.begin(9600);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP="" + AP + "","" + PASS + """, 30, "OK");
Serial3.begin(9600); //set baud rate for software serial port_3 to 9600
Serial2.begin(9600);
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30);
sensors.begin();
lcd.begin (20, 4); // our LCD is a 20x4, change for your LCD if needed
// LCD Backlight ON
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home on LCD
rtc.begin();
//#### The following lines can be uncommented to set the date and time for the first time###
/*
rtc.setDOW(TUESDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(21, 30, 30); // Set the time to 12:00:00 (24hr format)
rtc.setDate(7, 5, 2019); // Set the date to January 1st, 2014
*/
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}//if
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
//dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
dataFile.println(" ");
dataFile.println(" ");
dataFile.print(" ");
dataFile.print("Date");
dataFile.print(" ");
dataFile.print("Time");
dataFile.print(" ");
dataFile.print("pH");
dataFile.print(" ");
dataFile.print("DO");
dataFile.print(" ");
dataFile.print("Water Temp");
dataFile.close();
}//if
}//setup
void serialEvent()
{
//if the hardware serial port_0 receives a char
inputstring = Serial.readStringUntil(13); //read the string until we see a
input_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}//serialEvent
void serialEvent4()
{ //if the hardware serial port_0 receives a char
inputstringdo = Serial.readStringUntil(13); //read the string until we see a
input_string_completedo = true; //set the flag used to tell if we have received a completed string from the PC
}
void serialEvent3()
{ //if the hardware serial port_3 receives a char
sensorstring = Serial3.readStringUntil(13); //read the string until we see a
sensor_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}
void serialEvent2()
{ //if the hardware serial port_3 receives a char
sensorstringdo = Serial2.readStringUntil(13); //read the string until we see a
sensor_string_completedo = true; //set the flag used to tell if we have received a completed string from the PC
}
void loop()
{
delay(1000000);
Serial.print(rtc.getDOWStr());
Serial.print(" ");
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
Serial.println(rtc.getTimeStr());
delay(1000);
if (input_string_complete == true)
{ //if a string from the PC has been received in its entirety
Serial3.print(inputstring); //send that string to the Atlas Scientific product
Serial3.print('\r'); //add a to the end of the string
inputstring = ""; //clear the string
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
}
if (sensor_string_complete == true)
{ //if a string from the Atlas Scientific product has been received in its entirety
Serial.println(sensorstring); //send that string to the PC's serial monitor
//uncomment this section to see how to convert the pH reading from a string to a float
if (isdigit(sensorstring[0]))
{ //if the first character in the string is a digit
pH = sensorstring.toFloat(); //convert the string to a floating point number so it can be evaluated by
if (pH >= 7.0)
{ //if the pH is greater than or equal to 7.0
Serial.println("high"); //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
}//if
if (pH <= 6.99)
{ //if the pH is less than or equal to 6.99
Serial.println("low"); //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
}//if
}//if
}//if