#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 跳转至页
arduino获取网络时间程序
只看楼主 1楼
共1条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【笔记】生成报错synthdesignERROR被打赏50分 | |
【STM32H7S78-DK评测】LTDC+DMA2D驱动RGBLCD屏幕被打赏50分 | |
【STM32H7S78-DK评测】Coremark基准测试被打赏50分 | |
【STM32H7S78-DK评测】浮点数计算性能测试被打赏50分 | |
【STM32H7S78-DK评测】Execute in place(XIP)模式学习笔记被打赏50分 | |
每周了解几个硬件知识+buckboost电路(五)被打赏10分 | |
【换取逻辑分析仪】RA8 PMU 模块功能寄存器功能说明被打赏20分 | |
野火启明6M5适配SPI被打赏20分 | |
NUCLEO-U083RC学习历程2-串口输出测试被打赏20分 | |
【笔记】STM32CUBEIDE的Noruletomaketarget编译问题被打赏50分 |