Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. // This is a demo of the RBBB running as webserver with the EtherCard
  2. // 2010-05-28 <jc@wippler.nl>
  3. //
  4. // License: GPLv2
  5.  
  6. #include <EtherCard.h>  // https://github.com/njh/EtherCard
  7. // Ethernet mac address - must be unique on your network
  8. const byte myMac[] PROGMEM = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
  9. const char NTP_REMOTEHOST[] PROGMEM = "ntp.bit.nl";  // NTP server name
  10. const unsigned int NTP_REMOTEPORT = 123;             // NTP requests are to port 123
  11. const unsigned int NTP_LOCALPORT = 8888;             // Local UDP port to use
  12. const unsigned int NTP_PACKET_SIZE = 48;             // NTP time stamp is in the first 48 bytes of the message
  13. byte Ethernet::buffer[350];                          // Buffer must be 350 for DHCP to work
  14. BufferFiller bfill;
  15.  
  16. void setup() {
  17.   Serial.begin(9600);
  18.   Serial.println(F("\n[EtherCard NTP Client]"));
  19.   // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  20.   if (ether.begin(sizeof Ethernet::buffer, myMac, SS) == 0)
  21.     Serial.println(F("Failed to access Ethernet controller"));
  22.   if (!ether.dhcpSetup())
  23.     Serial.println(F("DHCP failed"));
  24.   ether.printIp("IP:  ", ether.myip);
  25.   ether.printIp("GW:  ", ether.gwip);
  26.   ether.printIp("DNS: ", ether.dnsip);
  27.   if (!ether.dnsLookup(NTP_REMOTEHOST))
  28.     Serial.println("DNS failed");
  29.   uint8_t ntpIp[IP_LEN];
  30.   ether.copyIp(ntpIp, ether.hisip);
  31.   ether.printIp("NTP: ", ntpIp);
  32.   ether.udpServerListenOnPort(&udpReceiveNtpPacket, NTP_LOCALPORT);
  33.   Serial.println("Started listening for response.");
  34.   sendNTPpacket(ntpIp);
  35. }
  36.  
  37. void loop () {
  38.   word len = ether.packetReceive();
  39.   word pos = ether.packetLoop(len);
  40.  
  41.   if (pos)  // check if valid tcp data is received
  42.     ether.httpServerReply(homePage()); // send web page data
  43. }
  44.  
  45. void sendNTPpacket(const uint8_t* remoteAddress) {
  46.   // buffer to hold outgoing packet
  47.   byte packetBuffer[ NTP_PACKET_SIZE];
  48.   // set all bytes in the buffer to 0
  49.   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  50.   // Initialize values needed to form NTP request
  51.   // (see URL above for details on the packets)
  52.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  53.   packetBuffer[1] = 0;            // Stratum, or type of clock
  54.   packetBuffer[2] = 6;            // Polling Interval
  55.   packetBuffer[3] = 0xEC;         // Peer Clock Precision
  56.   // 8 bytes of zero for Root Delay & Root Dispersion
  57.   packetBuffer[12]  = 49;
  58.   packetBuffer[13]  = 0x4E;
  59.   packetBuffer[14]  = 49;
  60.   packetBuffer[15]  = 52;
  61.   // all NTP fields have been given values, now
  62.   // you can send a packet requesting a timestamp:
  63.   ether.sendUdp(packetBuffer, NTP_PACKET_SIZE, NTP_LOCALPORT, remoteAddress, NTP_REMOTEPORT );
  64.   Serial.println("NTP request sent.");
  65. }
  66. void udpReceiveNtpPacket(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *packetBuffer, uint16_t len) {
  67.   Serial.println("NTP response received.");
  68.   // the timestamp starts at byte 40 of the received packet and is four bytes,
  69.   // or two words, long. First, extract the two words:
  70.   unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  71.   unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  72.   // combine the four bytes (two words) into a long integer
  73.   // this is NTP time (seconds since Jan 1 1900):
  74.   unsigned long secsSince1900 = highWord << 16 | lowWord;
  75.   // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  76.   const unsigned long seventyYears = 2208988800UL;
  77.   // subtract seventy years:
  78.   unsigned long epoch = secsSince1900 - seventyYears;
  79.   // print Unix time:
  80.   Serial.print("Unix time = ");
  81.   Serial.println(epoch);
  82.  
  83. }
  84.  
  85. static word homePage() {
  86.   long t = millis() / 1000;
  87.   word h = t / 3600;
  88.   byte m = (t / 60) % 60;
  89.   byte s = t % 60;
  90.   bfill = ether.tcpOffset();
  91.   bfill.emit_p(PSTR(
  92.     "HTTP/1.0 200 OK\r\n"
  93.     "Content-Type: text/html\r\n"
  94.     "Pragma: no-cache\r\n"
  95.     "\r\n"
  96.     "<meta http-equiv='refresh' content='1'/>"
  97.     "<title>RBBB server</title>"
  98.     "<h1>$D$D:$D$D:$D$D</h1>"),
  99.       h/10, h%10, m/10, m%10, s/10, s%10);
  100.   return bfill.position();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement