Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EtherCard.h>
- #include <Wire.h>
- #include "RTClib.h"
- #define TIME_ZONE +1
- #define INTERVAL 1000
- static byte mymac[] = {0x00,0x1A,0x4B,0x38,0x0C,0x5C};
- byte Ethernet::buffer[700];
- static byte ntpServer[] = {193,204,114,232};
- static byte srcPort = 0;
- unsigned long lastTime = 0;
- uint32_t timeStamp;
- boolean requestSent;
- boolean timeSet;
- void setup () {
- Serial.begin(57600);
- Serial.println("NTP Demo");
- Serial.println();
- requestSent = false;
- timeSet = false;
- connessione_eth();
- }
- void loop() {
- richiesta_ntp();
- }
- void connessione_eth(){
- if (!ether.begin(sizeof Ethernet::buffer, mymac, 4))
- Serial.println( "Failed to access Ethernet controller");
- else
- Serial.println("Ethernet controller initialized");
- if (!ether.dhcpSetup())
- Serial.println("Failed to get configuration from DHCP");
- else
- Serial.println("DHCP configuration done");
- ether.printIp("IP Address:\t", ether.myip);
- ether.printIp("Netmask :\t", ether.mymask);
- ether.printIp("Gateway :\t", ether.gwip);
- }
- void richiesta_ntp(){
- ether.packetLoop(ether.packetReceive());
- if(requestSent && ether.ntpProcessAnswer(&timeStamp, srcPort)) {
- Serial.println("NTP answer received");
- Serial.println();
- Serial.print("Timestamp: ");
- Serial.println(timeStamp);
- Serial.println();
- requestSent = false;
- timeSet = true;
- }
- if (!timeSet){
- unsigned long time = millis();
- if(time - lastTime > INTERVAL) {
- lastTime = time;
- ether.ntpRequest(ntpServer, srcPort);
- Serial.println("NTP request sent");
- requestSent = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement