这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 合作大赛 » arduino获取网络时间程序

共1条 1/1 1 跳转至

arduino获取网络时间程序

高工
2015-06-15 11:14:48     打赏
#include <EtherCard.h>


#define RELAY_PIN        2
#define SECONDS_IN_DAY          86400
#define START_YEAR              1900
#define TIME_ZONE               +8
static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static byte ntpServer[] = {193,204,114,232};
static byte srcPort = 0;
unsigned long lastTime =0;
uint32_t timeStamp;
boolean requestSent;
unsigned int day,month,year,hour, minute,second;
#define INTERVAL                1000

static byte mymac[]  = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[]   = {192,168,1,2};
byte Ethernet::buffer[700];

char* on  = "ON";
char* off = "OFF";

boolean relayStatus;
char* relayLabel;
char* linkLabel;

void setup () {

  Serial.begin(57600);
  Serial.println("WebRelay Demo");

  if(!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
  else 
  Serial.println("DHCP configuration done");  

  ether.printIp("My IP: ", ether.myip);
  // ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  
  relayStatus = false;
  relayLabel = off;
  linkLabel = on;
    requestSent = false;
}

void loop() {

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if(pos) {

    if(strstr((char *)Ethernet::buffer + pos, "GET /?ON") != 0) {
      relayStatus = true;
      relayLabel = on;
      linkLabel = off;
    } else if(strstr((char *)Ethernet::buffer + pos, "GET /?OFF") != 0) {
      relayStatus = false;
      relayLabel = off;
      linkLabel = on;
    }
    digitalWrite(RELAY_PIN, relayStatus); 
    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><meta name='viewport' content='width=200px'/></head><body>"
      "<div style='position:absolute;width:200px;height:200px;top:50%;left:50%;margin:-100px 0 0 -100px'>"
      "<div style='font:bold 14px verdana;text-align:center'>Relay is $S</div>"
      "<br><div style='text-align:center'>"
      "<a href='/?$S'><img src='http://www.lucadentella.it/files/bt_$S.png'></a>"
       "<br><a href='\'>$D/$D/$D</a><br>"
       "<a href='\'>Time:$D:$D:$D</a>"
      "</div></div></body></html>"
      ), relayLabel, linkLabel, linkLabel,day,month,year,hour,minute,second);

      ether.httpServerReply(bfill.position());
    }
    distime();

}

void distime()
{
    if(requestSent && ether.ntpProcessAnswer(&timeStamp, srcPort)) {
    Serial.println("NTP answer received");
    Serial.print("Timestamp: ");
    Serial.println(timeStamp);
    printDate(timeStamp + 3600 * TIME_ZONE+100);
    requestSent = false;
  }

unsigned long  time = millis();  
  if(time - lastTime > INTERVAL) {
    lastTime = time;
    ether.ntpRequest(ntpServer, srcPort);
    Serial.println("NTP request sent");
    requestSent = true;
  }
}
void printDate(uint32_t timeStamp) {

  year = START_YEAR;
  while(1) {
    uint32_t seconds;
    if(isLeapYear(year)) seconds = SECONDS_IN_DAY * 366;
    else seconds = SECONDS_IN_DAY * 365;
    if(timeStamp >= seconds) {
      timeStamp -= seconds;
      year++;
    } else break;
  }

   month = 0;
  while(1) {    
    uint32_t seconds = SECONDS_IN_DAY * days_in_month[month];
    if(isLeapYear(year) && month == 1) seconds = SECONDS_IN_DAY * 29;
    if(timeStamp >= seconds) {
      timeStamp -= seconds;
      month++;
    } else break;
  }  
  month++;

day = 1;
  while(1) {
    if(timeStamp >= SECONDS_IN_DAY) {
      timeStamp -= SECONDS_IN_DAY;
      day++;
    } else break;
  }  

hour = timeStamp / 3600;
   minute = (timeStamp - (uint32_t)hour * 3600) / 60;
second = (timeStamp - (uint32_t)hour * 3600) - minute * 60;

  Serial.println("Current date and time:");

  if(day < 10) Serial.print("0");
  Serial.print(day);
  Serial.print("/");

  if(month < 10) Serial.print("0");
  Serial.print(month);
  Serial.print("/");  

  Serial.println(year);

  if(hour < 10) Serial.print("0");
  Serial.print(hour);
  Serial.print(":");

  if(minute < 10) Serial.print("0");
  Serial.print(minute);
  Serial.print(":");

  if(second < 10) Serial.print("0");
  Serial.println(second);

}

boolean isLeapYear(unsigned int year) {

  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}



共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]