#include #include #include #include #include #include #include // for Display #include // For Display #include // For temperature Sensor #include // For Temperature Sensor #define PH_SOFT_VERSION "SYS: SERIALESP" #define ONE_WIRE_BUS 2 // Temperature sensors data in Pin 2 #define MAX_VAL_LIST 6 // Maximun number of values to display (see below) /* 1. Temp. sensor 1, oC/oF 2. Temp. sensor 2, oC/oF 3. pH 4. pH sensor value 5. File Size 6. WIFI Status 7. calibration Offset 6.86pH 8. catibration analog Value 8.86 9. calibration Offset 9.18pH 10. colaibration analog Value 9.18 */ #define esp8266_reset_pin 5 // Connect this pin to CH_PD on the esp8266, not reset. (let reset be unconnected) #define MY_SSID "TYPE YOUR WIFI SSID HERE" #define MY_PWRD "TYPE YOUR WIFI PASSWORD HERE" #define MY_SERVER_IP "TYPE YOUR FTP IP ADDRESS HERE" #define MY_SERVER_PORT "LIKELY 21 HERE" #define MY-SERVER_PWRD "TYPE YOUR FTP PASSWORD HERE" #define MAX_ESPRESP_BUF MSG_BUFFER_MAX char ESPResp[MSG_BUFFER_MAX]; // to rear responses from ESP8266 String ESPRespS; String fileString; String TXTSend; // Text to send to FTP connection int PASV_port[6]; bool PASV_port_Y = false; Timer SensorTimer; // Create timer for sensors Timer FileSendTimer; // Create timer for FTP long int Sensing_Freq = 60000 * 3; // Time for sensing in msec int timer_event_S = 0; long int Transfer_Freq = Sensing_Freq * 10 ; // FTP every 30 min int timer_event_T = 0; unsigned long MaxnumLines = 21; // max number of lines shortlog.txt DS3231 clock; RTCDateTime dt; LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // for temp sensor OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); float mVal = -0.03126; float bVal = 23.46855; float Celcius[2] = {0, 0}; float Fahrenheit[2] = {0, 0}; float pHValue[5]; float pHVoltage = 2.5; float calOffset686 = 0.0; float calOffsetRaw686 = 512.00; float calOffset918 = 0.0; float calOffsetRaw918 = 512.00; float pHsensorValue = 512.00; // For Button int ButtonPin = 3; // Botton wired to pin 3 int ButtonStatus = LOW; int ValueIndex = 3; unsigned long fileSize; String WIFIStatus = "NA"; SerialESP8266wifi wifi(Serial1, Serial1, esp8266_reset_pin, Serial);// adding Serial enabled local echo and wifi debug void setup() { // put your setup code here, to run once: clock.begin(); // Set sketch compiling time // clock.setDateTime(__DATE__, __TIME__); Serial.begin(9600); //serial start and pin configconfig Serial.println(PH_SOFT_VERSION); Serial.println("Initialize DS3231"); // for Display // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.clear(); lcd.println(PH_SOFT_VERSION); delay(2000); lcd.println("Initialize DS3231"); delay(1000); /* Arduino Mega 2560 - Serial1 TX pin 18 RX pin 19 */ Serial1.begin(115200);//Serial for ESP8266 wifi Serial.println("ESP8266 serial begin"); lcd.clear(); lcd.println("ESP8266 begin"); delay(1000); // to show in lcd wifi.begin(); wifi.setTransportToTCP();// this is also default wifi.endSendWithNewline(true); // Will end all transmissions with a newline and carrage return ie println.. default is true Serial.println("Connecting to WIFI"); lcd.clear(); lcd.println("Connecting WIFI"); wifi.connectToAP(MY_SSID, MY_PWRD); Serial.println(); waitAnswer(); sensors.begin(); // Starts temp sensor Serial.println("Starting Sensors"); // For Sensor timer_event_S = SensorTimer.every(Sensing_Freq, ReadSensors); // schedule sensor Serial.print("Sensors to run every "); Serial.print(Sensing_Freq / 1000.0 / 60.0); Serial.println(" min"); lcd.clear(); lcd.println("Sensors scan "); lcd.print(Sensing_Freq / 1000.0 / 60.0); lcd.println(" min"); delay(1000); // to show in lcd // for file transfer timer_event_T = FileSendTimer.every(Transfer_Freq, FTPCall); // schedule FTP send file Serial.print("send Log every "); Serial.print(Transfer_Freq / 1000.0 / 60.0); Serial.println(" min"); // For Button pinMode(ButtonPin, INPUT); // // SD card read/write // The circuit: // SD card attached to SPI bus as follows: // MOSI - pin 50 // MISO - pin 51 // CLK - pin 52 // CS - pin 53 // Serial.print("Initializing SD card... "); if (!SD.begin(53)) { Serial.println("initialization failed!"); lcd.clear(); lcd.println("SD failed!"); } else { Serial.println("initialization done."); lcd.clear(); lcd.println("SD ready"); } delay(1000); // to show in lcd } void loop() { // put your main code here, to run repeatedly: SensorTimer.update(); // run timer for Sensors FileSendTimer.update(); // run timer for FTP transfer dt = clock.getDateTime(); // lcd.clear(); switch (ValueIndex) { case 1: // Get Temperature sensor 1 lcd.setCursor(0, 0); lcd.print("Temp1: "); lcd.print(Celcius[0]); lcd.print("oC"); lcd.setCursor(0, 1); lcd.print(Fahrenheit[0]); lcd.print("oF"); break; case 2: // Get Temperature sensor 2 lcd.setCursor(0, 0); lcd.print("Temp2: "); lcd.print(Celcius[1]); lcd.print("oC"); lcd.setCursor(0, 1); lcd.print(Fahrenheit[1]); lcd.print("oF"); break; case 3: // pH lcd.setCursor(0, 0); lcd.print("pH: "); lcd.print(pHValue[0]); lcd.print("; "); lcd.print(pHValue[1]); lcd.setCursor(0, 1); lcd.print(pHValue[2]); lcd.print("; "); lcd.print(pHValue[3]); lcd.print("; "); lcd.print(pHValue[4]); lcd.print(""); break; case 4: // pHsensorValue lcd.setCursor(0, 0); lcd.print("pH sensor"); lcd.setCursor(0, 1); lcd.print(pHsensorValue); lcd.print(""); break; case 5: // File Size lcd.setCursor(0, 0); lcd.print("File Size"); lcd.setCursor(0, 1); lcd.print(fileSize); lcd.print(""); break; case 6: // WIFI Status lcd.setCursor(0, 0); lcd.print("WIFI Status"); lcd.setCursor(0, 1); lcd.print(WIFIStatus); lcd.print(""); break; case 7: // Offset 6.86 lcd.setCursor(0, 0); lcd.print("Offset 6.86"); lcd.setCursor(0, 1); lcd.print(calOffset686); lcd.print(""); break; case 8: // Offset Analog Value 6.86 lcd.setCursor(0, 0); lcd.print("Off Raw 6.86"); lcd.setCursor(0, 1); lcd.print(calOffsetRaw686); lcd.print(""); break; case 9: // Offset 9.18 lcd.setCursor(0, 0); lcd.print("Offset 9.18"); lcd.setCursor(0, 1); lcd.print(calOffset918); lcd.print(""); break; case 10: // Offset Analog Value 9.18 lcd.setCursor(0, 0); lcd.print("Off Raw 9.18"); lcd.setCursor(0, 1); lcd.print(calOffsetRaw918); lcd.print(""); break; } ButtonStatus = digitalRead(ButtonPin); if (ButtonStatus == HIGH) { Serial.print("Button Pushed. "); ValueIndex++; delay(2000); lcd.clear(); if (ValueIndex > MAX_VAL_LIST) { ValueIndex = 1; } Serial.print("Selecting Value: "); Serial.println(ValueIndex); } // } void ReadSensors() { // Temperature reading function dt = clock.getDateTime(); sensors.requestTemperatures(); Celcius[0] = sensors.getTempCByIndex(0); Fahrenheit[0] = sensors.toFahrenheit(Celcius[0]); Celcius[1] = sensors.getTempCByIndex(1); Fahrenheit[1] = sensors.toFahrenheit(Celcius[1]); // read the input for pH on analog pin 0: pHsensorValue = 0; calOffsetRaw686 = 0; calOffsetRaw918 = 0; for (int i = 1; i <= 10 ; i++) { pHsensorValue = pHsensorValue + analogRead(A0); calOffsetRaw686 = calOffsetRaw686 + analogRead(A1); calOffsetRaw918 = calOffsetRaw918 + analogRead(A2); delay(500); } // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): calOffsetRaw686 = calOffsetRaw686 / 10.00; calOffset686 = calOffsetRaw686 * 2.00 * 0.50 / 1023.00 - 0.50; calOffsetRaw918 = calOffsetRaw918 / 10.00; calOffset918 = calOffsetRaw918 * 2.00 * 0.50 / 1023.00 - 0.50; pHsensorValue = pHsensorValue / 10.00; pHVoltage = pHsensorValue * (5.00 / 1023.00); pHValue[4] = pHValue[3]; pHValue[3] = pHValue[2]; pHValue[2] = pHValue[1]; pHValue[1] = pHValue[0]; pHValue[0] = mVal * pHsensorValue + bVal; // Check WIFI status WIFIStatus = "NA"; Serial1.println("AT+CWJAP?"); waitAnswer(5000, false); if (ESPRespS.indexOf(MY_SSID) > 0) { WIFIStatus = "OK"; } // Show in LCD lcd.clear(); Serial.print(dt.year); Serial.print("-"); Serial.print(dt.month); Serial.print("-"); Serial.print(dt.day); Serial.print(" "); Serial.print(dt.hour); Serial.print(":"); Serial.print(dt.minute); Serial.print(":"); Serial.print(dt.second); Serial.print(". "); Serial.print(Celcius[0]); Serial.print("C/"); Serial.print(Fahrenheit[0]); Serial.print("F; "); Serial.print(Celcius[1]); Serial.print("C/"); Serial.print(Fahrenheit[1]); Serial.print("F; pH="); Serial.print(pHValue[0]); Serial.print("; SV="); Serial.print(pHsensorValue); Serial.print("; OS1="); Serial.print(calOffset686); Serial.print("; OSR1="); Serial.print(calOffsetRaw918); Serial.print("; OS2="); Serial.print(calOffset918); Serial.print("; OSR2="); Serial.print(calOffsetRaw918); Serial.print("; FS="); Serial.print(fileSize); Serial.print("; WIFI "); Serial.print(WIFIStatus); Serial.print(" ;LCD Val:"); Serial.println(ValueIndex); // // Write in file File myFile = SD.open("Datalog.txt", FILE_WRITE); if (myFile) { myFile.print(dt.year); myFile.print("-"); myFile.print(dt.month); myFile.print("-"); myFile.print(dt.day); myFile.print(" "); myFile.print(dt.hour); myFile.print(":"); myFile.print(dt.minute); myFile.print(":"); myFile.print(dt.second); myFile.print(";"); myFile.print(Celcius[0]); myFile.print(";"); myFile.print(Celcius[1]); myFile.print(";"); myFile.print(pHsensorValue); myFile.print(";"); myFile.println(pHValue[0]); fileSize = myFile.size(); myFile.close(); } } void FTPCall() { // Temperature reading function; unsigned long numChar = 0; char oneChar; dt = clock.getDateTime(); Serial.print(dt.year); Serial.print("-"); Serial.print(dt.month); Serial.print("-"); Serial.print(dt.day); Serial.print(" "); Serial.print(dt.hour); Serial.print(":"); Serial.print(dt.minute); Serial.print(":"); Serial.print(dt.second); Serial.print(". "); Serial.println("Sending File..."); File myFile = SD.open("Datalog.txt"); SD.remove("SHORTLOG.TXT"); File myShortF = SD.open("SHORTLOG.TXT", FILE_WRITE); if (myFile) { Serial.print("Reading Datalog.txt..."); unsigned long numLines = 0; myFile.seek(myFile.size() - 1); while (numLines < MaxnumLines ) { if (myFile.peek() == '\n') { numLines++; } if (myFile.position() > 0) { myFile.seek(myFile.position() - 1); numChar++; } else { myFile.seek(0); break; } } Serial.print(numLines); Serial.println(" lines"); int i = 0; if (myShortF) { Serial.print("Writing SHORTLOG.TXT... "); char cRd; fileString = ""; while (numChar > 0) { cRd = myFile.peek(); myShortF.write(cRd); fileString.concat(cRd); myFile.seek(myFile.position() + 1); numChar = numChar - 1; i++; } Serial.print(myShortF.size()) ; Serial.println(" bytes"); myShortF.close(); } myFile.close(); } // FTP if (!wifi.isConnectedToAP()) { wifi.connectToAP(MY_SSID, MY_PWRD); Serial.println(); waitAnswer(); } if (!wifi.isConnectedToServer()) { Serial1.println("AT+CIPMUX=1"); waitAnswer(); wifi.connectToServer(MY_SERVER_IP, MY_SERVER_PORT); waitAnswer(); Serial.println(">USER YOUR_FTP_USERNAME HERE"); wifi.send(SERVER, "USER YOUR_FTP-USERNAME_HERE", true); waitAnswer(); Serial.println(">PASS ........."); wifi.send(SERVER, "PASS YOUR_FTP_PASSWORD_HERE", true); waitAnswer(); } Serial.println(">TYPE A"); wifi.send(SERVER, "TYPE A", true); waitAnswer(); Serial.println("SHORTLOG.TXT is ..."); Serial.println(fileString); PASV_port_Y = false; Serial.println(">PASV"); wifi.send(SERVER, "PASV", true); waitAnswer(); findPASV_port(); if (!PASV_port_Y) { Serial.println("NO VALID PASV MODE"); wifi.disconnectFromServer(); waitAnswer(); Serial.println(); return; } unsigned int dPort; dPort = PASV_port[4] * 256 + PASV_port[5]; Serial1.print("AT+CIPSTART=3,\"TCP\",\""); Serial1.print(MY_SERVER_IP); Serial1.print("\","); Serial1.println(dPort); waitAnswer(); if (ESPRespS.indexOf("ERR") > 0) { Serial.println("Data port Not Connected"); wifi.disconnectFromServer(); waitAnswer(); return; } Serial.println(">STOR SHORTLOG.TXT"); wifi.send(SERVER, "STOR SHORTLOG.TXT", true); waitAnswer(); Serial.println(fileString.length()); // wifi.send('3',fileString,true); Serial1.print("AT+CIPSEND=3,"); Serial1.println(fileString.length() + 1); waitAnswer(); Serial1.println(fileString.c_str()); waitAnswer(); Serial.println(">QUIT"); wifi.send(SERVER, "QUIT", true); waitAnswer(); wifi.disconnectFromServer(); waitAnswer(); Serial1.println("AT+CIPCLOSE=5"); waitAnswer(); // end FTP stuff } void waitAnswer() { waitAnswer(5000 , true); } void waitAnswer(int MaxWaitTM , bool showR) { int c_tm = 0; ESPRespS = ""; while (c_tm <= MaxWaitTM) { if (!Serial1.available()) delay(5); else { while (Serial1.available()) { ESPRespS.concat((char)Serial1.read()); } c_tm = 0; } c_tm = c_tm + 5; } if (showR) { Serial.println(ESPRespS); } } bool findPASV_port() { int pos1, pos2; String sPort; int i = 0; if (PASV_port_Y) return true; pos1 = ESPRespS.indexOf("("); pos2 = ESPRespS.indexOf(")"); if ( pos1 == -1 || pos2 == -1) { PASV_port_Y = false; return false; } PASV_port_Y = true; sPort = ESPRespS.substring(pos1 + 1, pos2); Serial.println(sPort); String xPort; int j = 0; pos1 = sPort.indexOf(","); while (pos1 != -1) { j++; pos1 = sPort.indexOf(",", pos1 + 1); } Serial.print(sPort); Serial.print(" has "); Serial.print(j); Serial.println(" ,s"); PASV_port[5] = 0; for (i = 0; i < j; i++) { pos1 = sPort.indexOf(","); xPort = sPort.substring(0, pos1); PASV_port[i] = xPort.toInt(); Serial.println(PASV_port[i]); sPort.remove(0, pos1 + 1); Serial.println(sPort); } PASV_port[i] = sPort.toInt(); return true; }