void setupAP(void) { configMode = true; // in config mode Serial.print("Configuring access point..."); /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAPConfig(apIP, apIP, netMsk); // WiFi.softAP(softAP_ssid, softAP_password); // WiFi.softAP(softAP_ssid.c_str(), softAP_password.c_str()); WiFi.softAP(softAP_ssid.c_str()); // WiFi.softAP(softAP_ssid); delay(1000); // Without delay I've seen the IP address blank // delay(100); // Without delay I've seen the IP address blank //// Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); /* Setup the DNS server redirecting all the domains to the apIP */ // dnsServer.setErrorReplyCode(DNSReplyCode::NoError); // dnsServer.start(DNS_PORT, "*", apIP); } /** Is this an IP? */ boolean isIp(String str) { for (int i = 0; i < str.length(); i++) { int c = str.charAt(i); if (c != '.' && (c < '0' || c > '9')) { return false; } } return true; } void cleareeprom() { for (int i = 0; i < 256; ++i) { EEPROM.write(i, clearchar[0]); } // String firsteeprom = "+"; // firsteeprom = "9"; // EEPROM.write(0, firsteeprom[0] ); EEPROM.commit(); Serial.print ("EEPROM.read(0) = "); Serial.println (EEPROM.read(0)); Serial.print ("char(EEPROM.read(0)) = "); Serial.println (char(EEPROM.read(0))); Serial.println ("Cleared Eeprom"); } /** IP to String? */ String toStringIp(IPAddress ip) { String res = ""; for (int i = 0; i < 3; i++) { res += String((ip >> (8 * i)) & 0xFF) + "."; } res += String(((ip >> 8 * 3)) & 0xFF); return res; } void senddatatoapi() { if ( !getPage() ) { PlnSerial("GET request failed"); sentapi = 0 ; } else { PlnSerial("GET request succeded"); sentapi = 1 ; } } bool getPage() { WiFiClient client; // Attempt to make a connection to the remote server if ( !client.connect(http_site, http_port) ) { // if (!client.connect(Accessip, Accesport)) { return false; } if ( tempreading = "C" ) { float temptodisp = valuedev1; } else { // to change to celcius or farhaneigh float temptodisp = (valuedev1*1.8)+32; } if (precision == 1) { temptodisp = round(temptodisp * 10); temptodisp = temptodisp/10 ; PSerial("temptodisp after making it 1 decimal = ");PlnSerial (String (temptodisp)); temptodisp = (int) round(temptodisp) ; PSerial("temptodisp after making it integer and rounded = ");PlnSerial (String (temptodisp)); } else { temptodisp = (round(temptodisp * precision) ) ; // / precision ; temptodisp = (temptodisp / precision) ; // / precision ; } matrix.print(temptodisp, DEC); matrix.writeDisplay(); if (preverse == 1 ) { if (Pcurrent == 1) { Pcurrent = 0 ; PlnSerial("in if preverse and pcurrent = 1 "); } else { Pcurrent = 1 ; PlnSerial("in if preverse and pcurrent = 0 "); } } PSerial("Pcurrent = "); PlnSerial(String(Pcurrent)); if (dhttemp == 1) { valuedev1 = temp_f ; valuedev2 = humidity; } String strvaluedev1 = "" ; if ( valuedev1 == -127 || valuedev1 == 185 || valuedev1 == 0) { strvaluedev1 = "n/a" ; } else { strvaluedev1 = String(valuedev1) ; } String strvaluedev2 = "" ; if ( valuedev2 == -127 || valuedev2 == 185 || valuedev1 == 0) { strvaluedev2 = "n/a" ; } else { strvaluedev2 = String(valuedev2) ; } if (devicetodo.substring(12,13) == "a" ) { strvaluedev1 = temp_act; strvaluedev2 = hum_act; pressureKPA = press_act; } String toprint = "" ; // Make an HTTP GET request // String toprint = "GET http://boatautomation.com/jojo/sendtoAPI-1.php?macid='"+String(MAC_char)+"'&temp1='"+String(valuedev1)+"'&temp2='"+String(valuedev2)+"'&temp3='"+String(valuedev3)+"'&freq='"+String(freq)+"'&rebootfreq='"+String(rebootfreq)+"'&ip='"+ipadd+"' HTTP/1.1" ; if ( buttonupdt == 1) { // if ( datetosave == " " ) { PlnSerial("in buttonupdt = 1"); toprint = wheretosend + "?macid='"+String(MAC_char)+"'&datesaved='"+String(datetosave)+"'&power='"+"'&baro='"+String(pressureKPA,1)+String(pcurrent)+"'&pushbutton='"+String(curr)+"'&button='"+String(current)+"'&pump='"+String(Pcurrent)+"'&pump2='"+String(Pcurrent2)+"'&pump3='"+String(Pcurrent3)+"'&signal='"+String(rssi)+"'&temp1='"+String(strvaluedev1)+"'&temp2='"+String(strvaluedev2)+"'&temp3='"+String(valuedev3)+"'&freq='"+String(freq)+"'&rebootfreq='"+String(rebootfreq)+"'&clientcode='"+String(clientcode)+"'&ip='"+ipadd+"' HTTP/1.1" ; //"'&pump='"+String(Pcurrent1)+"'&pump2='"+String(Pcurrent2)+"'&pump3='"+String(Pcurrent3)+ if ((devicetodo.substring(4,5) == "s" ) || (devicetodo.substring(8,9) == "b" )) { buttonupdt = 0 ; } } else { // toprint = wheretosend + "?macid='"+String(MAC_char)+"'&pump='"+String(Pcurrent)+"'&signal='"+String(rssi)+"'&temp1='"+String(strvaluedev1)+"'&temp2='"+String(strvaluedev2)+"'&temp3='"+String(valuedev3)+"'&freq='"+String(freq)+"'&rebootfreq='"+String(rebootfreq)+"'&clientcode='"+String(clientcode)+"'&ip='"+ipadd+"' HTTP/1.1" ; toprint = wheretosend + "?macid='"+String(MAC_char)+"'&datesaved='"+String(datetosave)+"'&power='"+String(pcurrent)+"'&baro='"+String(pressureKPA,1)+"'&pushbutton='"+5+"'&button='"+5+"'&pump='"+String(Pcurrent)+"'&pump2='"+String(Pcurrent2)+"'&pump3='"+String(Pcurrent3)+"'&signal='"+String(rssi)+"'&temp1='"+String(strvaluedev1)+"'&temp2='"+String(strvaluedev2)+"'&temp3='"+String(valuedev3)+"'&freq='"+String(freq)+"'&rebootfreq='"+String(rebootfreq)+"'&clientcode='"+String(clientcode)+"'&ip='"+ipadd+"' HTTP/1.1" ; } PSerial("toprint = "); PlnSerial(String(toprint)); client.println(toprint); //// client.println("GET /test.html HTTP/1.1"); client.print("Host: "); client.println(wheretosend + "?macid='"+String(MAC_char)+"'&signal='"+String(rssi)+"'&pump='"+String(Pcurrent)+"'&temp1='"+String(strvaluedev1)+"'&temp2='"+String(valuedev2)+"'&temp3='"+String(valuedev3)+"'&freq='"+String(freq)+"'&rebootfreq='"+String(rebootfreq)+"'&clientcode='"+String(clientcode)+"'&ip='"+ipadd+"'"); if (preverse == 1 ) { if (Pcurrent == 1) { Pcurrent = 0 ; PlnSerial("in if preverse and pcurrent = 1 "); } else { Pcurrent = 1 ; PlnSerial("in if preverse and pcurrent = 0 "); } } // delay(2000); while(client.available()){ String line = client.readStringUntil('\r'); PSerial("line = :"); PlnSerial(String(line)); } client.println("Connection: close"); client.println(); return true; } void getpressure(){ pressureKPA = mpl115a2.getPressure(); // Serial.print("Pressure (kPa): "); Serial.print(pressureKPA, 4); Serial.println(" kPa"); PSerial("Pressure (kPa): ");PSerial (String (pressureKPA, 4)) ; PlnSerial (" kPa") ; pressureKPA = 10*pressureKPA; // Serial.print("Pressure (mpar): "); Serial.print(pressureKPA, 4); Serial.println(" mpar"); PSerial("Pressure (mpar): ");PSerial (String (pressureKPA, 4)) ; PlnSerial (" mpar") ; Serial.print("Pressure (mpar): ");Serial.print (String (pressureKPA, 4)) ; Serial.println (" mpar") ; if (precision == 1) { pressureKPA = round(pressureKPA * 10); pressureKPA = pressureKPA/10 ; PSerial("pressureKPA after making it 1 decimal = ");PlnSerial (String (pressureKPA)); pressureKPA = (int) round(pressureKPA) ; PSerial("pressureKPA after making it integer and rounded = ");PlnSerial (String (pressureKPA)); } else { pressureKPA = round(pressureKPA * precision) ; pressureKPA = pressureKPA / precision ; PSerial("pressureKPA after * precision = ");PlnSerial (String (pressureKPA)); } // temperatureC = mpl115a2.getTemperature(); // Serial.print("Temp (*C): "); Serial.print(temperatureC, 1); Serial.println(" *C"); } void gettemp() { if (dhttemp == 1) { humidity = dht.readHumidity(); // Read humidity (percent) temp_f = dht.readTemperature(false); // Read temperature as Celsius if (isnan(humidity) || isnan(temp_f)) { PlnSerial("Failed to read from DHT sensor!"); temp_f = -127.0; humidity = -127.0; temptemp == -127.0 ; temptemp2 == -127.0 ; return; } else { temptemp = temp_f ; temptemp = temptemp + adjtemp ; temptemp2 = humidity ; temptemp2 = temptemp2 + adjtemp2 ; if (precision == 1) { temptemp = round(temptemp * 10); temptemp = temptemp/10 ; PSerial("temptemp after making it 1 decimal = ");PlnSerial (String (temptemp)); temptemp = (int) round(temptemp) ; PSerial("temptemp after making it integer and rounded = ");PlnSerial (String (temptemp)); temptemp2 = round(temptemp2 * 10); temptemp2 = temptemp2/10 ; PSerial("temptemp2 after making it 1 decimal = ");PlnSerial (String (temptemp2)); temptemp2 = (int) round(temptemp2) ; PSerial("temptemp2 after making it integer and rounded = ");PlnSerial (String (temptemp2)); } else { temptemp = (round(temptemp * precision) ) ; // / precision ; temptemp = (temptemp / precision) ; // / precision ; temptemp2 = (round(temptemp2 * precision) ) ; // / precision ; temptemp2 = (temptemp2 / precision) ; // / precision ; } } } else if (dhttemp == 2) { printTemp31855(temp.readThermocouple(CELSIUS)); temptemp = tempmax31855 ; if ( temptemp == 0 ) { temptemp == -127.0 ; temptemp2 == -127.0 ; return; } temptemp = temptemp + adjtemp ; PSerial("tempmax31855 temptemp = ");PlnSerial (String (temptemp)); printTemp31855(temp.readJunction(CELSIUS)); temptemp2 = tempmax31855 ; temptemp2 = temptemp2 + adjtemp2 ; PSerial("tempmax31855 temptemp2 = ");PlnSerial (String (temptemp2)); if (precision == 1) { temptemp = round(temptemp * 10); temptemp = temptemp/10 ; PSerial("temptemp after making it 1 decimal = ");PlnSerial (String (temptemp)); temptemp = (int) round(temptemp) ; PSerial("temptemp after making it integer and rounded = ");PlnSerial (String (temptemp)); temptemp2 = round(temptemp2 * 10); temptemp2 = temptemp2/10 ; PSerial("temptemp2 after making it 1 decimal = ");PlnSerial (String (temptemp2)); temptemp2 = (int) round(temptemp2) ; PSerial("temptemp2 after making it integer and rounded = ");PlnSerial (String (temptemp2)); } else { temptemp = (round(temptemp * precision) ) ; // / precision ; temptemp = (temptemp / precision) ; // / precision ; temptemp2 = (round(temptemp2 * precision) ) ; // / precision ; temptemp2 = (temptemp2 / precision) ; // / precision ; } } else if ( dhttemp == 0){ tempv = ""; // added Feb 15 //// float temptemp; //// String temperature; String temperature = ""; sensors.requestTemperatures(); // Send the command to get temperatures // temptemp = sensors.getTempCByIndex(0); // removed Feb 15 // print the device information printData(insideThermometer); temptemp = tempv.toFloat(); PSerial("temptemp before anything = ");PlnSerial (String (temptemp)); // if ((abs(temptemp - oldtemptest) > 20 * tempdiff && oldtemptest < 200 && wrongtempcount < wtcmax) || temptemp == 185 ){ // if ((wrongtempcount < wtcmax) && oldtemptest < 200 )|| temptemp == 185 || temptemp == 0 ){ if ( temptemp == 0 || temptemp == 185 ) { temptemp = -127 ; wrongtempcount += 1 ; PSerial("wrongtempcount = ");PlnSerial (String (wrongtempcount)); } else { wrongtempcount = 0 ; temptemp = temptemp + adjtemp ; PSerial ("temptemp = "); PlnSerial(String(temptemp)); if (precision == 1) { temptemp = round(temptemp * 10); temptemp = temptemp/10 ; //PSerial("temptemp after making it 1 decimal = ");PlnSerial (String (temptemp)); temptemp = (int) round(temptemp) ; // PSerial("temptemp after making it integer and rounded = ");PlnSerial (String (temptemp)); } else { temptemp = (round(temptemp * precision) ) ; // / precision ; temptemp = (temptemp / precision) ; // / precision ; } // PSerial("temptemp: "); // PlnSerial(String(temptemp)); PSerial("temptemp after rounding: "); PlnSerial(String(temptemp)); } if ( numtemp > 1 ) { printData(outsideThermometer); temptemp2 = tempv.toFloat(); // temperature = String(temptemp); // temperature = String(temptemp + adjtemp); // Serial.print("Temperature in C: "); // Serial.println(temperature); ///////////////////////////////////////////////////////////// a faire come temp1 // if (abs(temptemp2 - oldtemptest2) > 4 * tempdiff ) { // if ((abs(temptemp2 - oldtemptest2) > 20 * tempdiff && oldtemptest2 < 200 && wrongtempcount < wtcmax) || temptemp2 == 185 ){ if ( temptemp2 == 0 || temptemp2 == 185 ) { temptemp2 = -127 ; wrongtempcount += 1 ; PSerial("wrongtempcount = ");PlnSerial (String (wrongtempcount)); } else { wrongtempcount = 0 ; temptemp2 = temptemp2 + adjtemp2 ; PSerial ("tempeading = "); PlnSerial(String(tempreading)); // temptemp = temptemp + adjtemp ; // if ( tempreading == "F" ) { // temptemp = (temptemp * 2) + 30 ; // //F = (C x 2) + 30 // } if (precision == 1) { temptemp2 = round(temptemp2 * 10); temptemp2 = temptemp2/10 ; PSerial("temptemp2 after making it 1 decimal = ");PlnSerial (String (temptemp2)); temptemp2 = (int) round(temptemp2) ; PSerial("temptemp2 after making it integer and rounded = ");PlnSerial (String (temptemp2)); } else { temptemp2 = (round(temptemp2 * precision) ) ; // / precision ; temptemp2 = (temptemp2 / precision) ; // / precision ; } PSerial("temptemp2: "); PlnSerial(String(temptemp2)); /* PSerial("temptemp after rounding: "); PlnSerial(String(temptemp)); Serial.print("precision: "); PlnSerial(String(precision)); */ PSerial("temptemp2 after rounding: "); PlnSerial(String(temptemp2)); } } } // added may 4th-2016 /* if (precision == 1) { temptemp = round(temptemp * 10); temptemp = temptemp/10 ; PSerial("temptemp after making it 1 decimal = ");PlnSerial (String (temptemp)); temptemp = (int) round(temptemp) ; PSerial("temptemp after making it integer and rounded = ");PlnSerial (String (temptemp)); temptemp2 = round(temptemp2 * 10); temptemp2 = temptemp2/10 ; PSerial("temptemp2 after making it 1 decimal = ");PlnSerial (String (temptemp2)); temptemp2 = (int) round(temptemp2) ; PSerial("temptemp2 after making it integer and rounded = ");PlnSerial (String (temptemp2)); } else { temptemp = (round(temptemp * precision) ) ; // / precision ; temptemp = (temptemp / precision) ; // / precision ; temptemp2 = (round(temptemp2 * precision) ) ; // / precision ; temptemp2 = (temptemp2 / precision) ; // / precision ; } */ } // function to print a device address void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { // zero pad the address if necessary if (deviceAddress[i] < 16) PSerial("0"); PlnSerial(String(deviceAddress[i], HEX)); } } // function to print the temperature for a device void printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); PSerial("Temp C: "); PSerial(String(tempC)); // Serial.print(" Temp F: "); // Serial.print(DallasTemperature::toFahrenheit(tempC)); PlnSerial(" "); tempv=String(tempC); } // function to print a device's resolution void printResolution(DeviceAddress deviceAddress) { PSerial("Resolution: "); PSerial(String(sensors.getResolution(deviceAddress))); PlnSerial(" "); } // Print the temperature, or the type or fault for Max31855 void printTemp31855(double temperature) { float oldtemp31855 = valuedev1 ; switch ((int) temperature) { case FAULT_OPEN: PSerial("FAULT_OPEN"); tempmax31855 = oldtemp31855; break; case FAULT_SHORT_GND: PSerial("FAULT_SHORT_GND"); tempmax31855 = oldtemp31855; break; case FAULT_SHORT_VCC: PSerial("FAULT_SHORT_VCC"); tempmax31855 = oldtemp31855; break; case NO_MAX31855: PSerial("NO_MAX31855"); break; default: tempmax31855 = temperature ; PSerial("Max31855 = "); PlnSerial(String(tempmax31855)); break; } PSerial(" "); } // main function to print information about a device void printData(DeviceAddress deviceAddress) { PSerial("Device Address: "); printAddress(deviceAddress); PSerial(" "); printTemperature(deviceAddress); PlnSerial(" "); } void getvolt(){ /* float testvolt = 0 ; int i = 0 ; for ( i ; i < 10; ++i){ // testvolt = testvolt + analogRead(A0) ; testvolt = testvolt + (analogRead(A0) / adjvolt) ; // PSerial( i ); PSerial("testvolt = ");PlnSerial (String (testvolt)); PSerial( i ); PSerial("analog = ");PlnSerial (String ((analogRead(A0) / adjvolt))); } voltvolt = testvolt / i ; */ // PSerial("analog = ");PlnSerial (String (analogRead(A0)); voltvolt = (analogRead(A0) / adjvolt) ; voltvolt = (round(voltvolt * vprecision) ) ; // / precision ; voltvolt = (voltvolt / vprecision) ; // / precision ; //// voltvolt = analogRead(A0); // PSerial("volts = "); // PlnSerial(String(voltvolt)); // PSerial("output: "); // PlnSerial(String(voltvolt)); // PlnSerial(String(analogRead(A0)); // voltvolt = voltvolt / adjvolt ; // voltvolt = (round(voltvolt * vprecision) ) ; // / precision ; // voltvolt = (voltvolt / vprecision) ; // / precision ; /* PSerial("voltvolt after rounding: "); PlnSerial(String(voltvolt)); PSerial("vprecision: "); PlnSerial(String(vprecision)); PSerial("voltvolt after rounding: "); PlnSerial(String(voltvolt)); */ } void dowhatis() { thermostat = 0 ; aircond = 0 ; heater = 0 ; if (whatis == "B") { // devicetype = "temp-volt-baro"; // devicetodo = "t,v, , , , ,a, ,"; devicetype = "temp-volt"; devicetodo = "t,v, , , , , , ,"; } else if (whatis == "C") { devicetype = "temp-volt-switch"; devicetodo = "t,v,s, , , , , , "; } else if (whatis == "D") { devicetype = "temp-volt-switch-pump"; devicetodo = "t,v,s,p, , , , , "; } else if (whatis == "d") { devicetype = "temp-volt-pump"; devicetodo = "t,v, ,p, , , , , "; } else if (whatis == "E") { devicetype = "temp-switch"; devicetodo = "t, ,s, , , , , , "; } else if (whatis == "F") { devicetype = "temp-switch-heater"; devicetodo = "t, ,s, , ,h, , , "; thermostat = 1 ; heater = 1 ; } else if (whatis == "G") { devicetype = "temp-switch-a/c"; devicetodo = "t, ,s, , ,a, , , "; thermostat = 1 ; aircond = 1 ; } else if (whatis == "H") { devicetype = "temp-switch-heater & A/C"; devicetodo = "t, ,s, , ,b, , , "; thermostat = 1 ; aircond = 1 ; heater = 1 ; } else if (whatis == "P") { devicetype = "pump"; devicetodo = " , , ,p, , , , , "; tempfreq = 2 ; } else if (whatis == "Q") { devicetype = "pump"; devicetodo = " , , ,p, , , , , "; preverse = 1; tempfreq = 2 ; } else if (whatis == "p") { // power failure devicetype = "power"; devicetodo = " , , , , , , , ,p"; tempfreq = 2 ; } else if (whatis == "o") { // power failure + temp devicetype = "power & temp"; devicetodo = "t, , , , , , , ,p"; dhttemp = 0; // ds18b20 sensor } else if (whatis == "R") { // device means reading 0 - 1023 on adc devicetype = "dev"; devicetodo = " , , ,d, , , , , "; } else if (whatis == "S") { devicetype = "switch"; devicetodo = " , ,s, , , , , , "; tempfreq = 2 ; } else if ( whatis == "T") { devicetype = "temp"; devicetodo = "t, , , , , , , , "; dhttemp = 0; // ds18b20 sensor } else if (whatis == "U") { devicetype = "push button"; devicetodo = " , , , ,b, , , "; tempfreq = 2 ; } else if (whatis == "V") { devicetype = "volt"; devicetodo = " ,v, , , , , , , "; } else if (whatis == "W") { devicetype = "temp, volt, switch, pump & push button"; devicetodo = "t,v,s,p,b, , , "; } else if (whatis == "X") { devicetype = "temp & push button"; devicetodo = "t, , , ,b, , , "; } else if ( whatis == "Y") { devicetype = "temp"; devicetodo = "t, , , , , , , , "; dhttemp = 1; // dht200 sensor } else if ( whatis == "Z") { devicetype = "temp"; devicetodo = "t, , , , , , , , "; dhttemp = 2; // 500 to 800 degrees heat } if (devicetodo.substring(4,5) == "s" ) { // buttonupdt = 1 ; // relay pin // pinMode(BUTTON, INPUT_PULLUP); pinMode(BUTTON, OUTPUT); // grab the current state of the button current = digitalRead(BUTTON); currentold = 0 ; PSerial (""); PSerial ("Button = "); PlnSerial(String(current)); } if (devicetodo.substring(6,7) == "p" ) { // relay pin // pinMode(PUMP1, INPUT_PULLUP); pinMode(PUMP1, INPUT); pinMode(PUMP2, INPUT); pinMode(PUMP3, INPUT); digitalWrite(PUMP1, LOW); digitalWrite(PUMP2, LOW); digitalWrite(PUMP3, LOW); // pinMode(BUTTON, OUTPUT); // grab the current state of the button Pcurrent = digitalRead(PUMP1); Pcurrentold = Pcurrent ; Pcurrent2 = digitalRead(PUMP2); Pcurrentold2 = Pcurrent2 ; Pcurrent3 = digitalRead(PUMP3); Pcurrentold3 = Pcurrent3 ; PSerial (""); PSerial ("Pump1 = "); PlnSerial(String(Pcurrent)); PSerial ("Pump2 = "); PlnSerial(String(Pcurrent2)); PSerial ("Pump3 = "); PlnSerial(String(Pcurrent3)); if (devicetodo.substring(4,5) == " " ) { pinMode(BUTTON, INPUT); digitalWrite(BUTTON, LOW); // grab the current state of the button current = digitalRead(BUTTON); currentold = 0 ; PSerial ("Pump4 = "); PlnSerial(String(current)); } if (devicetodo.substring(8,9) == " " ) { // buttonupdt = 1 ; pinMode(BUTT, INPUT); // grab the current state of the button curr = digitalRead(BUTT); currold = 0 ; PSerial (""); PSerial ("Pump5 = "); PlnSerial(String(curr)); } } if (devicetodo.substring(8,9) == "b" ) { // relay pin // pinMode(BUTTON, INPUT_PULLUP); pinMode(BUTT, OUTPUT); // grab the current state of the button curr = digitalRead(BUTT); currold = 0 ; PSerial (""); PSerial ("Push Button = "); PlnSerial(String(curr)); } if (devicetodo.substring(8,9) == "h" ) { thermostat = 1 ; heater = 1 ; PSerial (""); PSerial ("Heater is = "); PlnSerial(String(heater)); } if (devicetodo.substring(8,9) == "a" ) { thermostat = 1 ; aircond = 1 ; PSerial (""); PSerial ("A/C is = "); PlnSerial(String(aircond)); } } void PSerial(String value) { if (details != 0 ) { Serial.print(value); } } void PlnSerial(String value) { if (details != 0 ) { Serial.println(value); } } void pins(int pinp) { String pin = pinnum.substring(pinp,pinp+1) ; int pinpr = 0 ; if ( pin == "C" ) { pinpr = 12 ; } else if ( pin == "E" ) { pinpr = 13 ; } else if ( pin == "F" ) { pinpr = 14 ; } else if ( pin == "G" ) { pinpr = 15 ; } else if ( pin == "H" ) { pinpr = 16 ; } else { pinpr = pin.toInt() ; } return ; } void ispoweron() { PSerial("powercut = "); PlnSerial(String(powercut)); PSerial("sentapi = "); PlnSerial(String(sentapi)); if ( powercut == 1) { pcurrent = 0 ; // datetosave = dateTime ; senddatatoapi(); PlnSerial("Sending api"); if ( sentapi == 1) { powercut = 0 ; datetosave = " " ; PSerial("datetosave = "); PlnSerial(datetosave); String firsteeprom = "9"; EEPROM.write(0, firsteeprom[0] ); EEPROM.commit(); PlnSerial("clearing eeprom"); // for (int i = 140; i < 141; ++i) for (int i = datetosave_from; i < datetosave_to; ++i) { EEPROM.write(i, clearchar[0]) ; } /* PlnSerial("writing eeprom datetosave:"); // for (int i = 140; i < 141; ++i) for (int i = datetosave_from; i < datetosave_to; ++i) { EEPROM.write(i, datetosave[i-datetosave_from]); PSerial("Wrote: "); PlnSerial(datetosave[i-datetosave_from]); } */ EEPROM.commit(); // s += "

Saved

"; // resolution = strreqanswer.toInt() ; // y = freq ; // sensors.setResolution(0,resolution); PSerial("datetosave = "); PlnSerial(datetosave); } } else { pcurrent = digitalRead(POWER); // PSerial (""); // PSerial ("PUMP1 = "); PlnSerial(String(current)) ( } if (pcurrent == 1) { // IO0 is 1 /* PSerial("connecting to "); PlnSerial(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 13; if (!client.connect(host, httpPort)) { PlnSerial("connection failed"); return; } PlnSerial(">Sending Header<}"); // This will send the request to the server /////////////////////////////////////////////////// missing line here delay(100); // Read all the lines of the reply from server and print them to Serial // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT // char buffer[12]; // String dateTime = ""; PlnSerial("> Listening...<"); while(client.available()) { String line = client.readStringUntil('\r'); if (line.indexOf("Date") != -1) { PSerial("=====>"); } else { // PSerial(line); // date starts at pos 7 TimeDate = line.substring(7); PlnSerial(TimeDate); // time starts at pos 14 TimeDate = line.substring(7, 15); datetosave = TimeDate ; TimeDate.toCharArray(buffer, 10); PlnSerial("UTC Date/Time:"); PlnSerial(buffer); TimeDate = line.substring(16, 24); TimeDate.toCharArray(buffer, 10); PlnSerial(buffer); // $date = date('Y-m-d\TH:i:s\-\0\0\:\0\0'); PSerial("datetosave1 = "); PlnSerial(datetosave); // datetosave += myChar + TimeDate + myChar + "-" + myChar + "0" + myChar + "0" + myChar + ":" + myChar + "0" + myChar + "0" ; datetosave += "T" + TimeDate + "-00:00" ; PSerial("datetosave = "); PlnSerial(datetosave); } } PlnSerial(""); PlnSerial("closing connection"); client.println("Connection: close"); client.println(); PlnSerial("Close connection}"); PlnSerial(">WiFi Connected<}"); */ datetosave = dateTime ; if (pcurrentold == 0 ){ datetosave = dateTime ; senddatatoapi(); PlnSerial("Sending api"); } } else if (pcurrentold == 1){ // save the last time read in eeprom // else if ( req.startsWith("/resolution=") ) { // s += "

Saving the resolution value

"; // s += "\r\n\r\n"; // strreqanswer = req.substring(req.lastIndexOf('=')+1); PSerial("in else datetosave = "); datetosave = dateTime ; PlnSerial("datetosave = " + datetosave); String firsteeprom = "9"; EEPROM.write(0, firsteeprom[0] ); EEPROM.commit(); PlnSerial("clearing eeprom"); // for (int i = 140; i < 141; ++i) for (int i = datetosave_from; i < datetosave_to; ++i) { EEPROM.write(i, clearchar[0]) ; } PlnSerial("writing eeprom datetosave:"); // for (int i = 140; i < 141; ++i) for (int i = datetosave_from; i < datetosave_to; ++i) { EEPROM.write(i, datetosave[i-datetosave_from]); Serial.print("Wrote: "); Serial.println(datetosave[i-datetosave_from]); } EEPROM.commit(); // s += "

Saved

"; // resolution = strreqanswer.toInt() ; // y = freq ; // sensors.setResolution(0,resolution); Serial.println("datetosave = " + datetosave); powercut = 1 ; Serial.println("Powering off in 10s "); delay(5000); Serial.println("Off"); digitalWrite(POWER2, LOW); delay(10000); } if ( digitalRead(POWER2) == 0 && pcurrent == 1 ) { Serial.println("restarting"); digitalWrite(POWER2, HIGH); } if ( sentapi == 1) { } pcurrentold = pcurrent ; } void counttime() { if (TimeDates < 59) { TimeDates += 1 ; } else if ( TimeDates == 59 ) { TimeDates = 0 ; if (TimeDatem < 59 ) { TimeDatem += 1 ; } else if ( TimeDatem == 59 ) { TimeDatem = 0 ; // checktime() ; //Serial.println("*********************************************checked time"); if ( TimeDateh < 23 ) { TimeDateh += 1 ; if (TimeDateh == 23 ) { //TimeDateh = 0 ; if (TimeDateh == 23 && TimeDatem == 59 && TimeDates == 59 ) { checktime() ; PlnSerial("*********************************************checked time"); // to continue with days or restart the datetime } } } } } if ((TimeDates == 59 ) && (TimeDatem == 10 || TimeDatem == 20 || TimeDatem == 30 || TimeDatem == 40 || TimeDatem == 50 || TimeDatem == 0 )){ checktime() ; PlnSerial("*********************************************checked time"); } if (TimeDateh < 10) { TimeDatehs = "0"+String(TimeDateh); } else { TimeDatehs = String(TimeDateh); } if (TimeDatem < 10) { TimeDatems = "0"+String(TimeDatem); } else { TimeDatems = String(TimeDatem); } if (TimeDates < 10) { TimeDatess = "0"+String(TimeDates); } else { TimeDatess = String(TimeDates); } dateTime = DateDate + "T" + TimeDatehs + ":" + TimeDatems + ":" + TimeDatess + "-00:00" ; PlnSerial ("Date = " + dateTime ); } void checktime() { dateTime = "" ; while ((dateTime == "") || dateTime.startsWith("d")) { PSerial("connecting to "); PlnSerial(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 13; /// if (!client.connect(host, httpPort)) { /// PlnSerial("connection failed"); /// return; /// } int ttt = 0 ; //while (!client.connect(host, httpPort) && ttt < 20 ) { while ( ttt < 20 ) { if (!client.connect(host, httpPort)) { PlnSerial("connection failed"); } else { PlnSerial("connection succeeded"); ttt = 20 ; } ttt++ ; } PlnSerial(">Sending Header<}"); // This will send the request to the server client.print("HEAD / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n\r\n"); delay(100); // Read all the lines of the reply from server and print them to Serial // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT // char buffer[12]; PlnSerial("> Listening...<"); while(client.available()) { String line = client.readStringUntil('\r'); if (line.indexOf("Date") != -1) { PSerial("=====>"); } else { /// TimeDate = line.substring(7); DateDate = line.substring(7); PlnSerial(DateDate); // time starts at pos 14 /// TimeDate = line.substring(7, 15); DateDate = line.substring(7, 15); /// datetosave = TimeDate ; /// TimeDate.toCharArray(buffer, 10); DateDate.toCharArray(buffer, 10); PlnSerial("UTC Date/Time:"); PlnSerial(buffer); TimeDate = line.substring(16, 24); TimeDateh = line.substring(16, 18).toInt(); PlnSerial("Hours:" + String(TimeDateh)); TimeDatem = line.substring(19, 21).toInt(); PlnSerial("Minutes:" + String(TimeDatem)); TimeDates = line.substring(22, 24).toInt(); PlnSerial("Seconds:" + String(TimeDates)); TimeDate.toCharArray(buffer, 10); PlnSerial(buffer); // $date = date('Y-m-d\TH:i:s\-\0\0\:\0\0'); // PSerial("datetosave1 = "); PlnSerial(datetosave); // datetosave += myChar + TimeDate + myChar + "-" + myChar + "0" + myChar + "0" + myChar + ":" + myChar + "0" + myChar + "0" ; dateTime = DateDate + "T" + TimeDate + "-00:00" ; dateTime = DateDate + "T" + String(TimeDateh) + ":" + String(TimeDatem) + ":" + String(TimeDates) + "-00:00" ; PSerial("dateTime = "); PlnSerial(dateTime); } } PlnSerial(""); PlnSerial("closing connection"); client.println("Connection: close"); client.println(); PlnSerial("Close connection}"); PlnSerial(">WiFi Connected<}"); } } void get280() { // double temp_act = 0.0, press_act = 0.0,hum_act=0.0; // signed long int temp_cal; // unsigned long int press_cal,hum_cal; readData(); temp_cal = calibration_T(temp_raw); press_cal = calibration_P(pres_raw); hum_cal = calibration_H(hum_raw); temp_act = (double)temp_cal / 100.0; press_act = (double)press_cal / 100.0; hum_act = (double)hum_cal / 1024.0; Serial.print("TEMP : "); Serial.print(temp_act); Serial.print(" DegC PRESS : "); Serial.print(press_act); Serial.print(" hPa HUM : "); Serial.print(hum_act); Serial.println(" %"); temp_act = (round(temp_act * precision) ) ; // / precision ; temp_act = (temp_act / precision) ; // / precision ; hum_act = (round(hum_act * precision) ) ; // / precision ; hum_act = (hum_act / precision) ; // / precision ; press_act = press_act/100 ; press_act = (round(press_act * vprecision) ) ; // / precision ; press_act = (press_act / vprecision) ; // / precision ; Serial.print("TEMP : "); Serial.print(temp_act); Serial.print(" DegC PRESS : "); Serial.print(press_act); Serial.print(" hPa HUM : "); Serial.print(hum_act); Serial.println(" %"); delay(1000); } void readTrim() { uint8_t data[32],i=0; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0x88); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,24); while(Wire.available()){ data[i] = Wire.read(); i++; } Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xA1); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,1); data[i] = Wire.read(); i++; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xE1); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,7); while(Wire.available()){ data[i] = Wire.read(); i++; } dig_T1 = (data[1] << 8) | data[0]; dig_T2 = (data[3] << 8) | data[2]; dig_T3 = (data[5] << 8) | data[4]; dig_P1 = (data[7] << 8) | data[6]; dig_P2 = (data[9] << 8) | data[8]; dig_P3 = (data[11]<< 8) | data[10]; dig_P4 = (data[13]<< 8) | data[12]; dig_P5 = (data[15]<< 8) | data[14]; dig_P6 = (data[17]<< 8) | data[16]; dig_P7 = (data[19]<< 8) | data[18]; dig_P8 = (data[21]<< 8) | data[20]; dig_P9 = (data[23]<< 8) | data[22]; dig_H1 = data[24]; dig_H2 = (data[26]<< 8) | data[25]; dig_H3 = data[27]; dig_H4 = (data[28]<< 4) | (0x0F & data[29]); dig_H5 = (data[30] << 4) | ((data[29] >> 4) & 0x0F); dig_H6 = data[31]; } void writeReg(uint8_t reg_address, uint8_t data) { Wire.beginTransmission(BME280_ADDRESS); Wire.write(reg_address); Wire.write(data); Wire.endTransmission(); } void readData() { int i = 0; uint32_t data[8]; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xF7); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,8); while(Wire.available()){ data[i] = Wire.read(); i++; } pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); hum_raw = (data[6] << 8) | data[7]; } signed long int calibration_T(signed long int adc_T) { signed long int var1, var2, T; var1 = ((((adc_T >> 3) - ((signed long int)dig_T1<<1))) * ((signed long int)dig_T2)) >> 11; var2 = (((((adc_T >> 4) - ((signed long int)dig_T1)) * ((adc_T>>4) - ((signed long int)dig_T1))) >> 12) * ((signed long int)dig_T3)) >> 14; t_fine = var1 + var2; T = (t_fine * 5 + 128) >> 8; return T; } unsigned long int calibration_P(signed long int adc_P) { signed long int var1, var2; unsigned long int P; var1 = (((signed long int)t_fine)>>1) - (signed long int)64000; var2 = (((var1>>2) * (var1>>2)) >> 11) * ((signed long int)dig_P6); var2 = var2 + ((var1*((signed long int)dig_P5))<<1); var2 = (var2>>2)+(((signed long int)dig_P4)<<16); var1 = (((dig_P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long int)dig_P2) * var1)>>1))>>18; var1 = ((((32768+var1))*((signed long int)dig_P1))>>15); if (var1 == 0) { return 0; } P = (((unsigned long int)(((signed long int)1048576)-adc_P)-(var2>>12)))*3125; if(P<0x80000000) { P = (P << 1) / ((unsigned long int) var1); } else { P = (P / (unsigned long int)var1) * 2; } var1 = (((signed long int)dig_P9) * ((signed long int)(((P>>3) * (P>>3))>>13)))>>12; var2 = (((signed long int)(P>>2)) * ((signed long int)dig_P8))>>13; P = (unsigned long int)((signed long int)P + ((var1 + var2 + dig_P7) >> 4)); return P; } unsigned long int calibration_H(signed long int adc_H) { signed long int v_x1; v_x1 = (t_fine - ((signed long int)76800)); v_x1 = (((((adc_H << 14) -(((signed long int)dig_H4) << 20) - (((signed long int)dig_H5) * v_x1)) + ((signed long int)16384)) >> 15) * (((((((v_x1 * ((signed long int)dig_H6)) >> 10) * (((v_x1 * ((signed long int)dig_H3)) >> 11) + ((signed long int) 32768))) >> 10) + (( signed long int)2097152)) * ((signed long int) dig_H2) + 8192) >> 14)); v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * ((signed long int)dig_H1)) >> 4)); v_x1 = (v_x1 < 0 ? 0 : v_x1); v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); return (unsigned long int)(v_x1 >> 12); } String urldecode(const char *src){ //fix encoding String decoded = ""; char a, b; while (*src) { if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) { if (a >= 'a') a -= 'a'-'A'; if (a >= 'A') a -= ('A' - 10); else a -= '0'; if (b >= 'a') b -= 'a'-'A'; if (b >= 'A') b -= ('A' - 10); else b -= '0'; decoded += char(16*a+b); src+=3; } else if (*src == '+') { decoded += ' '; *src++; } else { decoded += *src; *src++; } } decoded += '\0'; return decoded; }