name=Time
version=1.5
author=Michael Margolis
maintainer=Paul Stoffregen
sentence=Timekeeping functionality for Arduino
paragraph=Date and Time functions, with provisions to synchronize to external time sources like GPS and NTP (Internet). This library is often used together with TimeAlarms and DS1307RTC.
category=Timing
url=Arduino Playground - HomePage
architectures=*
la riga dove non va più gia al primo utlizzo e questa
setSyncProvider(getNtpTime); //// si blocca a questo comando/riga, quindi NON fa la richiesta
comunque se lo carichi su arduino e lo vorresti provare, vedi tu stesso.
Quindi Fa il ping ma non fa l'aggiornamento data.
#include <SPI.h>
#include <TimeLib.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "utility/w5100.h"
#include <ICMPPing.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 1, 2}; // IP scelto per la scheda ethernet
int Persona2, Persona1;
unsigned long TempoLetturaPresenza = 0L;
int timeZone = 1;
EthernetUDP Udp;
byte packetBuffer[48];
time_t prevDisplay = 0;
IPAddress timeServer(192, 168, 1, 1);
String QuestaDATA;
unsigned long OrarioSincornizzato, DifferenzaMillis;
byte PrimoTelefono[] = {192, 168, 1, 11};
byte SecondoTelefono[] = {192, 168, 1, 12};
SOCKET pingSocket = 0;
char buffer [256];
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
unsigned long Tempo = 0L;
void setup()
{
delay(5000);
Ethernet.begin(mac, ip);
Serial.begin(9600);
}
void loop()
{
Persona1 = 0; Persona2 = 0;
ICMPEchoReply echoReply = ping(PrimoTelefono, 1);
if (echoReply.status == SUCCESS) {
Persona2 = 1;
SpegniAntifurto();
TempoLetturaPresenza = 0L;
}
ICMPEchoReply echoReply1 = ping(SecondoTelefono, 1);
if (echoReply1.status == SUCCESS) {
Persona1 = 1;
SpegniAntifurto();
TempoLetturaPresenza = 0L;
}
if ( ((millis() / 1000L) > TempoLetturaPresenza) && (TempoLetturaPresenza > 0L) ) {
AccendiAntifurto();
}
if ( (Persona1 == 0) && (Persona2 == 0) && ((millis() / 1000L) > TempoLetturaPresenza) )
{
Serial.println(F("RITARDO PER ATTIVAZIONE ANTIFURTO !!!"));
TempoLetturaPresenza = ((millis() / 1000L) + 15L);
}
delay(10000); //ritardo 10 secondi
AggiornaOra();
}
void AccendiAntifurto() {
Serial.println(F("\n++++++++++++++++++++++++++++> ANTIFURTO INCLUSO DA ASSENZA PHONE <+++++++++++++++++++++++++++++\n"));
TempoLetturaPresenza = ((millis() / 1000L) + 16L);
}
void SpegniAntifurto() {
Serial.println(F("\n----------------------------> ANTIFURTO ESCLUSO DA PRESENZA PHONE <-----------------------------\n"));
}
void AggiornaOra()
{
Serial.print(F("--> ")); Serial.print(dayShortStr(weekday()) ); Serial.print(F(" ")); Serial.print(QuestaDATA); Serial.println(F(" <--"));
QuestaDATA = "";
setSyncProvider(getNtpTime); //// si blocca a questo comando/riga, quindi NON fa la richiesta
///realizzo l'ora legale dall'ultima domenica di marzo all'ultima domenica di ottobre
if (month() >= 4 && month() <= 9) {
timeZone = 2;
} else {
timeZone = 1;
}
if (month() == 10) {
if ( ( (weekday() == 1) && ((day() + 7) > 31) && (hour() > 2) ) || ( (weekday() > 1) && ((day() + 7) > 31) ) ) {
timeZone = 1;
} else {
timeZone = 2;
}
}
if (month() == 3) {
if ( ( (weekday() == 1) && ((day() + 7) > 31) && (hour() > 1) ) || ( (weekday() > 1) && ((day() + 7) > 31) ) ) {
timeZone = 2;
}
}
if ( day() < 10) {
QuestaDATA += "0";
} QuestaDATA += day(); QuestaDATA += "/";
if ( month() < 10) {
QuestaDATA += "0";
} QuestaDATA += month(); QuestaDATA += "/";
QuestaDATA += year(); QuestaDATA += "..";
if ( hour() < 10) {
QuestaDATA += "0";
} QuestaDATA += hour(); QuestaDATA += ":";
if ( minute() < 10) {
QuestaDATA += "0";
} QuestaDATA += minute(); QuestaDATA += ":";
if ( second() < 10) {
QuestaDATA += "0";
} QuestaDATA += second();
OrarioSincornizzato = (hour() * 60L * 60L) ;
OrarioSincornizzato = OrarioSincornizzato + (minute() * 60L) ;
OrarioSincornizzato = OrarioSincornizzato + second() ;
DifferenzaMillis = (millis() / 1000L);
Serial.print(F("--> ")); Serial.print(dayShortStr(weekday()) ); Serial.print(F(" ")); Serial.print(QuestaDATA); Serial.print(F(" <-- DA INTERNET "));
}
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ;
//Serial.print(F("---Rcihiesta NTP --->"));
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500)
{
int SIZE = Udp.parsePacket();
if (SIZE >= 48) {
//Serial.print(F("NTP Ricevuto "));
Udp.read(packetBuffer, 48);
unsigned long secsSince1900;
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
//Serial.print(F("Nessun NTP :-( "));
return 0;
}
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, 48);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, 48);
Udp.endPacket();
}