{"id":643,"date":"2015-11-02T09:26:10","date_gmt":"2015-11-02T00:26:10","guid":{"rendered":"http:\/\/goji2100.s199.coreserver.jp\/blog\/?p=643"},"modified":"2022-05-03T12:13:45","modified_gmt":"2022-05-03T03:13:45","slug":"arduino%e6%99%82%e8%a8%88%e3%82%92esp-wroom-02%e3%81%a7ntp%e5%90%8c%e6%9c%9f%e3%81%95%e3%81%9b%e3%82%8b","status":"publish","type":"post","link":"https:\/\/goji2100.com\/blog\/?p=643","title":{"rendered":"Arduino\u6642\u8a08\u3092ESP-WROOM-02\u3067NTP\u540c\u671f\u3055\u305b\u308b"},"content":{"rendered":"<p>\u524d\u8a18\u4e8b\u306e\u300c<a href=\"http:\/\/goji2100.com\/blog\/?p=576\">Arduino\uff0b7Seg LED\uff0bRTC\u3067\u30c7\u30b8\u30bf\u30eb\u6642\u8a08<\/a>\u300d\u306bESP-WROOM-02\u3092\u63a5\u7d9a\u3057\u3066\u3001NTP\u6642\u523b\u540c\u671f\u3057\u307e\u3059\u3002<br \/>\nWROOM\u306fWi-Fi\u30a2\u30af\u30bb\u30b9\u30dd\u30a4\u30f3\u30c8\u7d4c\u7531\u306760\u79d2\u6bce\uff08\u6b63\u5e38\u52d5\u4f5c\u304c\u78ba\u8a8d\u3067\u304d\u305f\u3089\u9593\u9694\u306f\u9577\u304f\u3059\u308b\uff09\u306b\u30bf\u30a4\u30e0\u30b5\u30fc\u30d0\u306b\u63a5\u7d9a\u3057\u3001\u6a19\u6e96\u6642\u523b\u3092\u53d7\u4fe1\u3057\u307e\u3059\u3002\u53d7\u4fe1\u3057\u305f\u6642\u523b\u60c5\u5831\u306fUART\u7d4c\u7531\u3067Arduino UNO\u306b\u9001\u51fa\u3057\u6642\u523b\u3092\u540c\u671f\u3055\u305b\u307e\u3059\u3002<br \/>\nArduino UNO\u5074\u306e\u30b9\u30b1\u30c3\u30c1\u306f\u305d\u306e\u307e\u307e\u3067\u3059\u3002<\/p>\n<p>Arduino\uff0bESP-WROOM-02\u30c7\u30b8\u30bf\u30eb\u30af\u30ed\u30c3\u30af\u5168\u4f53\u5199\u771f<\/p>\n<div id=\"attachment_655\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1949.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-655\" class=\"size-medium wp-image-655\" src=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1949-300x210.jpg\" alt=\"Arduino\uff0bESP-WROOM-02\" width=\"300\" height=\"210\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1949-300x210.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1949.jpg 860w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-655\" class=\"wp-caption-text\">Arduino\uff0bESP-WROOM-02<\/p><\/div>\n<p><!--more \u3010\u7d9a\u304d\u3092\u8aad\u3080\u3011-->ESP-WROOM-02\u30c9\u30fc\u30bf\u5199\u771f<\/p>\n<div id=\"attachment_657\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1923.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-657\" class=\"size-medium wp-image-657\" src=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1923-300x234.jpg\" alt=\"ESP-WROOM-02\u30c9\u30fc\u30bf\" width=\"300\" height=\"234\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1923-300x234.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/IMG_1923.jpg 860w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-657\" class=\"wp-caption-text\">ESP-WROOM-02\u30c9\u30fc\u30bf<\/p><\/div>\n<p>ESP-WROOM-02\u56de\u8def\u56f3<\/p>\n<div id=\"attachment_646\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/ArduinoCLKsch.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-646\" class=\"size-medium wp-image-646\" src=\"http:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/ArduinoCLKsch-300x189.png\" alt=\"ESP-WROOM-02 \u30c9\u30fc\u30bf\u56de\u8def\u56f3\" width=\"300\" height=\"189\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/ArduinoCLKsch-300x189.png 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/11\/ArduinoCLKsch.png 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-646\" class=\"wp-caption-text\">ESP-WROOM-02 \u30c9\u30fc\u30bf\u56de\u8def\u56f3<\/p><\/div>\n<p>NTPGet.ino<\/p>\n<p><span style=\"color: #ff0000;\">\uff0a2015\/12\/23 WiFi\u63a5\u7d9a\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f(\u884c\u756a\u53f776-83)<\/span><\/p>\n<pre class=\"lang:arduino decode:true \" title=\"NTPGet.ino\">#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;WiFiUdp.h&gt;\r\n\r\nchar ssid[] = \"YourNetworkSSID\";\r\nchar pass[] = \"YourNetworkPassword\";\r\n\r\n#define DEBUGP(x)  \/\/ Serial.print(x)\r\n#define LED1  13\r\n\r\n#define NTP_SERVER_NAME  \"time.nist.gov\"\r\n#define LOCAL_PORT  8888\r\n\r\nWiFiUDP udp;\r\nIPAddress timeServerIP;\r\n\r\n#define NTP_PACKET_SIZE  48   \/\/ NTP time stamp is in the first 48 bytes of the message\r\nbyte ntpPacketBuff[NTP_PACKET_SIZE];  \/\/ buffer to hold incoming and outgoing packets\r\n#define TIME_ZONE (9 * 60 * 60)\r\n\r\nvoid sendNTPpacket()\r\n{\r\n  WiFi.hostByName(NTP_SERVER_NAME, timeServerIP);\r\n  memset(ntpPacketBuff, 0, NTP_PACKET_SIZE);\r\n  ntpPacketBuff[ 0] = 0b11100011;  \/\/ LI, Version, Mode\r\n  ntpPacketBuff[ 1] = 0;           \/\/ Stratum, or type of clock\r\n  ntpPacketBuff[ 2] = 6;           \/\/ Polling Interval\r\n  ntpPacketBuff[ 3] = 0xEC;        \/\/ Peer Clock Precision\r\n  ntpPacketBuff[12] = 49;\r\n  ntpPacketBuff[13] = 0x4E;\r\n  ntpPacketBuff[14] = 49;\r\n  ntpPacketBuff[15] = 52;\r\n  udp.beginPacket(timeServerIP, 123);\r\n  udp.write(ntpPacketBuff, NTP_PACKET_SIZE);\r\n  udp.endPacket();\r\n}\r\n\r\nuint32_t recvNTPpacket()\r\n{\r\n  if (udp.parsePacket() &gt;= NTP_PACKET_SIZE) {\r\n    udp.read(ntpPacketBuff, NTP_PACKET_SIZE);\r\n    uint32_t timess = \r\n      (word(ntpPacketBuff[40], ntpPacketBuff[41]) &lt;&lt; 16 |\r\n       word(ntpPacketBuff[42], ntpPacketBuff[43]))\r\n       - 2208988800UL + (TIME_ZONE);\r\n    return (timess);\r\n  }\r\n  return 0;\r\n}\r\n\r\nbyte monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r\nvoid breakTime(unsigned long times, byte *tm)\r\n{\r\n  tm[5] = times % 60; times \/= 60;\r\n  tm[4] = times % 60; times \/= 60;\r\n  tm[3] = times % 24; times \/= 24;\r\n  tm[6] = ((times + 4) % 7) + 1;\r\n  times = (times + 366 + 365) * 100; \/\/ offset from 1968\/ 1\/ 1\r\n  tm[0] = (times \/ 36525) - 2;\r\n  times = (times % 36525) \/ 100 ;\r\n  times++;\r\n\u00a0 monthDays[1] = ((((tm[0] + 1970) % 4) == 0) ? 29 : 28); \/\/ 151121\r\n  for (tm[1] = 0; tm[1] &lt; 12; tm[1]++) {\r\n    if (times &lt;= monthDays[tm[1]]) break;\r\n    times -= monthDays[tm[1]];\r\n  }\r\n  tm[1]++;\r\n  tm[2] = times;\r\n}\r\n\r\n\r\nvoid setup()\r\n{\r\n  pinMode(LED1, OUTPUT);\r\n  Serial.begin(115200);\r\n  \/\/ 151223 &gt;&gt;\r\n  DEBUGP(\"\\n\\nConnecting to \"); DEBUGP(ssid); DEBUGP(\" \");\r\n  WiFi.begin(ssid, pass);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    DEBUGP(\".\");\r\n    delay(500);\r\n  }\r\n  DEBUGP(\"\\nWiFi connected\\n\");\r\n  DEBUGP(\"IP address: \"); DEBUGP(WiFi.localIP()); DEBUGP(\"\\n\");\r\n  \/\/ &lt;&lt;\r\n  DEBUGP(\"Starting UDP\");\r\n  udp.begin(LOCAL_PORT);\r\n  DEBUGP(\"Local port: \");\r\n  DEBUGP(udp.localPort()); DEBUGP(\"\\n\");\r\n}\r\n\r\nvoid loop() {\r\n  unsigned long times, pre_ms;\r\n  byte tm[7];\r\n\r\n  while (1) {\r\n    pre_ms = millis();\r\n    digitalWrite(LED1, LOW);\r\n    sendNTPpacket();\r\n    for (int loop = 0; loop &lt; 200; loop++) {\r\n      if (times = recvNTPpacket()) {\r\n        breakTime(times, tm);\r\n        tm[0] -= (2000 - 1970);\r\n        for (int j = 0; j &lt; 6; j++) {\r\n          if (tm[j] &lt; 10) Serial.print(\"0\");\r\n          Serial.print(tm[j]);\r\n        }\r\n        Serial.print(\"\\r\");\r\n        digitalWrite(LED1, HIGH);\r\n        break;\r\n      }\r\n      delay(10);\r\n    }\r\n    delay(1000);\r\n    delay((1000 * 58) + 2000 - (millis() - pre_ms));\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a18\u4e8b\u306e\u300cArduino\uff0b7Seg LED\uff0bRTC\u3067\u30c7\u30b8\u30bf\u30eb\u6642\u8a08\u300d\u306bESP-WROOM-02\u3092\u63a5\u7d9a\u3057\u3066\u3001NTP\u6642\u523b\u540c\u671f\u3057\u307e\u3059\u3002 WROOM\u306fWi-Fi\u30a2\u30af\u30bb\u30b9\u30dd\u30a4\u30f3\u30c8\u7d4c\u7531\u306760\u79d2\u6bce\uff08\u6b63\u5e38\u52d5\u4f5c\u304c\u78ba\u8a8d\u3067\u304d\u305f\u3089\u9593\u9694\u306f\u9577\u304f\u3059\u308b\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,26,1],"tags":[27,33,32],"_links":{"self":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643"}],"collection":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=643"}],"version-history":[{"count":14,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":1507,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions\/1507"}],"wp:attachment":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}