SHARE
TWEET

Arduino NTP REQUEST with ethercard library

subnet Jan 19th, 2014 243 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <EtherCard.h>
  2.  
  3. #include <Wire.h>
  4. #include "RTClib.h"
  5.  
  6. #define TIME_ZONE               +1
  7. #define INTERVAL                1000
  8.  
  9. static byte mymac[] = {0x00,0x1A,0x4B,0x38,0x0C,0x5C};
  10. byte Ethernet::buffer[700];
  11.  
  12. static byte ntpServer[] = {193,204,114,232};
  13. static byte srcPort = 0;
  14. unsigned long lastTime = 0;
  15.  
  16. uint32_t timeStamp;
  17. boolean requestSent;
  18. boolean timeSet;
  19.  
  20. void setup () {
  21.  
  22.   Serial.begin(57600);
  23.   Serial.println("NTP Demo");  
  24.   Serial.println();
  25.   requestSent = false;
  26.   timeSet = false;
  27.   connessione_eth();
  28. }
  29.  
  30.  
  31.  void loop() {
  32.   richiesta_ntp();
  33.   }
  34.  
  35. void connessione_eth(){
  36.  
  37.     if (!ether.begin(sizeof Ethernet::buffer, mymac, 4))
  38.     Serial.println( "Failed to access Ethernet controller");
  39.  else
  40.    Serial.println("Ethernet controller initialized");
  41.  
  42.   if (!ether.dhcpSetup())
  43.     Serial.println("Failed to get configuration from DHCP");
  44.   else
  45.     Serial.println("DHCP configuration done");
  46.  
  47.   ether.printIp("IP Address:\t", ether.myip);
  48.   ether.printIp("Netmask   :\t", ether.mymask);
  49.   ether.printIp("Gateway   :\t", ether.gwip);
  50.  
  51. }
  52.  
  53. void richiesta_ntp(){
  54.  
  55.  ether.packetLoop(ether.packetReceive());
  56.  
  57.     if(requestSent && ether.ntpProcessAnswer(&timeStamp, srcPort)) {
  58.       Serial.println("NTP answer received");
  59.       Serial.println();
  60.       Serial.print("Timestamp: ");
  61.       Serial.println(timeStamp);
  62.       Serial.println();
  63.       requestSent = false;
  64.       timeSet = true;
  65.     }
  66.  
  67.   if (!timeSet){
  68.     unsigned long time = millis();
  69.     if(time - lastTime > INTERVAL) {
  70.       lastTime = time;
  71.       ether.ntpRequest(ntpServer, srcPort);
  72.       Serial.println("NTP request sent");
  73.       requestSent = true;
  74.  
  75.     }
  76.   }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top