#include #include #include // libraries for realtime clock #include #include #define BUFSIZ 100 #define READING_PIN 0 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //IPAddress ip(192,168,1, 12); byte localIP[] = { 192,168,1, 12 }; byte mask[] = { 255, 255, 255, 0 }; byte gw[] = { 192, 168, 1, 254 }; RTC_DS1307 RTC; boolean login = false; // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void readHtmlPage(char* fileName, String a, String b, EthernetClient client){ String row = ""; File myFile; myFile = SD.open(fileName,FILE_READ); if (myFile) { while (myFile.available()) { char c = (myFile.read()); // Elimino i ritorni a capo e i tab if ( byte(c)!=9){ if ( c!='\n' && c!='\r' ) { row.concat(c); } else{ if ( a != NULL ){ row.replace(a,b); } //Serial.println(row); client.println(row); row=""; } } } } else { // if the file didn't open, print an error: Serial.println("Errore aprendo login.htm"); } myFile.close(); } String getLocalIpAddress(){ String ip; int size = sizeof(localIP) / sizeof(byte); for (int i = 0; i < size; i++) { ip.concat(localIP[i]); if ( i= 100 Udp.read(pb, NTP_PACKET_SIZE); // New from IDE 1.0, #else Udp.readPacket(pb, NTP_PACKET_SIZE); #endif // NTP contains four timestamps with an integer part and a fraction part // we only use the integer part here unsigned long t1, t2, t3, t4; t1 = t2 = t3 = t4 = 0; for (int i=0; i< 4; i++) { t1 = t1 << 8 | pb[16+i]; t2 = t2 << 8 | pb[24+i]; t3 = t3 << 8 | pb[32+i]; t4 = t4 << 8 | pb[40+i]; } // part of the fractional part // could be 4 bytes but this is more precise than the 1307 RTC // which has a precision of ONE second // in fact one byte is sufficient for 1307 float f1,f2,f3,f4; f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0; f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0; f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0; f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0; // NOTE: // one could use the fractional part to set the RTC more precise // 1) at the right (calculated) moment to the NEXT second! // t4++; // delay(1000 - f4*1000); // RTC.adjust(DateTime(t4)); // keep in mind that the time in the packet was the time at // the NTP server at sending time so one should take into account // the network latency (try ping!) and the processing of the data // ==> delay (850 - f4*1000); // 2) simply use it to round up the second // f > 0.5 => add 1 to the second before adjusting the RTC // (or lower threshold eg 0.4 if one keeps network latency etc in mind) // 3) a SW RTC might be more precise, => ardomic clock :) // convert NTP to UNIX time, differs seventy years = 2208988800 seconds // NTP starts Jan 1, 1900 // Unix time starts on Jan 1 1970. const unsigned long seventyYears = 2208988800UL; t1 -= seventyYears; t2 -= seventyYears; t3 -= seventyYears; t4 -= seventyYears; digitalClockDisplay(DateTime(t4)); // Adjust timezone and DST... in my case substract 4 hours for Chile Time // or work in UTC? t4 -= (3 * 3600L); // Notice the L for long calculations!! t4 += 1; // adjust the delay(1000) at begin of loop! if (f4 > 0.4) t4++; // adjust fractional part, see above RTC.adjust(DateTime(t4)); // Serial.print("RTC after : "); digitalClockDisplay(RTC.now()); // Serial.println(); Serial.println("done ..."); } else{ Serial.println("No UDP available ..."); } } //long getNtpTime() //{ // IPAddress timeServer(193,204,114,232); // sendNTPpacket(timeServer); // send an NTP packet to a time server // // while (true){ // delay(1000); // // wait to see if a reply is available // if ( Udp.parsePacket() ) { // // We've received a packet, read the data from it // Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer // // //the timestamp starts at byte 40 of the received packet and is four bytes, // // or two words, long. First, esxtract the two words: // unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); // unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); // // combine the four bytes (two words) into a long integer // // this is NTP time (seconds since Jan 1 1900): // unsigned long secsSince1900 = highWord << 16 | lowWord; // // now convert NTP time into Arduino Time format: // // Time starts on Jan 1 1970. In seconds, that's 2208988800: // const unsigned long seventyYears = 2208988800UL; // // subtract seventy years: // unsigned long epoch = secsSince1900 - seventyYears; // // return epoch; // } // } // return 0; // return 0 if unable to get the time //} double getTemperatura(){ double K = 9.5; // mW/dec C – dissipation factor double V_IN = 5.0; //double R1 = 10000.0; //resistance put in parallel double R1 = 2000.0; //resistance put in parallel double adc_raw = analogRead(READING_PIN); double V = adc_raw / 1024 * V_IN; //calculate resistance // double R_th = (R1 * V) / (V_IN - V); double R_th =((10240000/adc_raw) - R1); double kelvin = SteinhartHart(R_th) - V*V/(K * R_th); double celsius = kelvin - 273.15; return celsius; } String digitalClockDisplay(DateTime t){ String ret; // digital clock display of the time ret.concat(t.hour()); ret.concat(":"); ret.concat(printDigits(t.minute())); ret.concat(":"); ret.concat(printDigits(t.second())); ret.concat(" - "); ret.concat(t.day()); ret.concat("/"); ret.concat(t.month()); ret.concat("/"); ret.concat(t.year()); return ret; } //int adjustDstEurope() //{ // // last sunday of march // int beginDSTDate= (31 - (5* year() /4 + 4) % 7); // // Serial.println(beginDSTDate); // int beginDSTMonth=3; // //last sunday of october // int endDSTDate= (31 - (5 * year() /4 + 1) % 7); // // Serial.println(endDSTDate); // int endDSTMonth=10; // // DST is valid as: // if (((month() > beginDSTMonth) && (month() < endDSTMonth)) // || ((month() == beginDSTMonth) && (day() >= beginDSTDate)) // || ((month() == endDSTMonth) && (day() <= endDSTDate))) // return 7200; // DST europe = utc +2 hour // else return 3600; // nonDST europe = utc +1 hour //} void logTemp(){ double temp; String data; File myFile; temp = getTemperatura(); data = digitalClockDisplay(RTC.now()); char buffer[10]; myFile = SD.open("test.txt", FILE_WRITE); myFile.println( "Data " + data + " - Temperatura: " + dtostrf(temp, 5, 2, buffer)); myFile.close(); } void setup(){ Serial.begin(9600); // start the Ethernet connection and the server: Ethernet.begin(mac,localIP,mask,gw); if (!SD.begin(4)) { Serial.println("Inizializzazione SD fallita."); return; } getNtpTime(); server.begin(); // pinMode(10, OUTPUT); logTemp(); Serial.println("Setup completo."); } void loop() { double temp; String data; String readString = ""; unsigned long i; char clientline[BUFSIZ]; int index = 0; i++; if ( i == 900000){ logTemp(); i=0; } // listen for incoming clients EthernetClient client = server.available(); if (client) { index = 0; // an http request ends with a blank line while (client.connected()) { if (client.available()) { char c = client.read(); Serial.println(c); //readString.concat(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c != '\n' && c != '\r') { clientline[index] = c; index++; if (index >= BUFSIZ) index = BUFSIZ -1; } } Serial.println(clientline); if ( !login){ if( ( readString.indexOf('Nome=pippo') > 0 && readString.indexOf('Pdw=pluto') > 0 ) ){ login=true; } else { login=false; } } readString = ""; if (login){ // temp = getTemperatura(); // data = digitalClockDisplay(); // // // send a standard http response header // client.println("HTTP/1.1 200 OK"); // client.println("Content-Type: text/html"); // client.println(); // // client.print("Data "); // client.println(data); // client.println("
"); // client.print("Temperatura "); // client.println(temp); // client.println("C\n"); // client.println("
"); menuPage(client); } else{ loginPage(client); } client.stop(); break; } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } }