Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 2017-09-10 Hiroki Mori
- */
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <time.h>
- // ========== CONFIG STARTS HERE ==========//
- /*
- Address of NTP server from which we would grab time data
- */
- #define NTPSERVER ""
- /*
- +----------+------------------------------------------------------+---------+
- | TIME VAL | DESCRIPTION | GMT VAL |
- +----------+------------------------------------------------------+---------+
- | 43200 | International Date Line West | -12:00 |
- | -39600 | Midway Island, Samoa | -11:00 |
- | -36000 | Hawaii | -10:00 |
- | -34200 | French Polynesia, Marquesas Islands | -09:30 |
- | -32400 | Alaska | -09:00 |
- | -28800 | Pacific Time (US & Canada) | -08:00 |
- | -25200 | Mountain Time (US & Canada) | -07:00 |
- | -21600 | Central Time (US & Canada), Guadalajara, Mexico city | -06:00 |
- | -18000 | Eastern time (US & Canada) | -05:00 |
- | -16200 | Venezuela | -04:30 |
- | -14400 | Atlantic time (Canada), Manaus, Santiago | -04:00 |
- | -12600 | Newfoundland | -03:30 |
- | -10800 | Greenland, Brasilia, Montevideo | -03:00 |
- | -7200 | Mid-Atlantic | -02:00 |
- | -3600 | Azores | -01:00 |
- | 0 | GMT: Dublin, Edinburgh, Lisbon, London | 00:00 |
- | 3600 | Amsterdam, Berlin, Rome, Vienna, Prague, Brussels | +01:00 |
- | 7200 | Athens, Istanbul, Beirut, Cairo, Jerusalem | +02:00 |
- | 10800 | St. Petersburg, Minsk, Baghdad, Moscow | +03:00 |
- | 12600 | Iran | +03:30 |
- | 14400 | Volgograd, Baku, Yerevan | +04:00 |
- | 16200 | Afghanistan | +04:30 |
- | 18000 | Yekaterinburg, Tashkent | +05:00 |
- | 19800 | Chennai, Kolkata, Mumbai, New Delhi | +05:30 |
- | 20700 | Nepal | +05:45 |
- | 21600 | Omsk, Almaty | +06:00 |
- | 23400 | Myanmar, Cocos Islands | +06:30 |
- | 25200 | | +07:00 |
- | 28800 | Krasnoyarsk, Ulaan Bataar, Perth | +08:00 |
- | 32400 | Irkutsk | +09:00 |
- | 34200 | Australian Central Standard Time | +09:30 |
- | 36000 | Yakutsk, Canberra, Melbourne, Sydney, Hobart | +10:00 |
- | 37800 | Lord Howe Standard Time | +10:30 |
- | 39600 | Vladivostok, Solomon Is., New Caledonia | +11:00 |
- | 41400 | Norfolk Islan | +11:30 |
- | 43200 | Magadan, Auckland, Wellington | +12:00 |
- | 45900 | New Zealand, Chatham Island | +12:45 |
- | 46800 | Nuku'alofa | +13:00 |
- | 50400 | Kiribati, Line Islands | +14:00 |
- +----------+------------------------------------------------------+---------+
- */
- #define TIMEOFFSET 32400
- /*
- Output pin to produce DCF77 signal to
- */
- #define DCF77OUT 13
- /*
- WIFI Network credentials
- */
- const char ssid[] = "";
- const char passwd[] = "";
- // ========== CONFIG ENDS HERE ========== //
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, NTPSERVER, TIMEOFFSET, 0);
- int count = 0;
- int unixtime;
- int starttime;
- long lobit = 0; // 0 - 28
- long hibit = 0; // 29 - 59
- bool isConnected() {
- return WiFi.status() == WL_CONNECTED;
- }
- bool waitConnect() {
- int i = 0;
- Serial.println("now waiting");
- while(!isConnected()) {
- Serial.print(".");
- delay(1000);
- ++i;
- if(i > 60)
- break;
- }
- if(WiFi.status() != WL_CONNECTED)
- return false;
- // Print network details
- Serial.println("connected!");
- Serial.print("IPAddress: ");
- Serial.println(WiFi.localIP());
- return true;
- }
- void timer0_ISR (void) {
- int curbit;
- int sec;
- sec = count / 10;
- if(lobit != 0) {
- if(count < 290) {
- curbit = (lobit >> sec) & 1;
- } else {
- curbit = (hibit >> (sec - 29)) & 1;
- }
- if(sec != 59) {
- if (count % 10 == 0) {
- digitalWrite(DCF77OUT, HIGH);
- } else if(count % 10 == 1 && curbit == 0) {
- digitalWrite(DCF77OUT, LOW);
- } else if(count % 10 == 2 && curbit == 1) {
- digitalWrite(DCF77OUT, LOW);
- }
- }
- }
- if(count == 590) {
- mkdcf77();
- }
- if(count % 10 == 0)
- ++unixtime;
- ++count;
- if(count == 600)
- count = 0;
- timer0_write(ESP.getCycleCount() + 8000000L); // 8MHz == 100ms
- }
- void mkdcf77()
- {
- int minutes, hours, week, day, month, year;
- int parity;
- int i;
- struct tm *date;
- int nexttime = unixtime + 2 + 60;
- time_t now = nexttime;
- minutes = (nexttime % 3600) / 60;
- hours = (nexttime % 86400L) / 3600;
- week = ((nexttime / 86400L) + 3) % 7 + 1;
- date = localtime(&now);
- day = date->tm_mday;
- month = date->tm_mon + 1;
- year = date->tm_year-100;
- // Print encoded time data
- Serial.print(year);
- Serial.print("-");
- Serial.print(month);
- Serial.print("-");
- Serial.print(day);
- Serial.print("/");
- Serial.print(week);
- Serial.print(" ");
- Serial.print(hours);
- Serial.print(":");
- Serial.println(minutes);
- lobit = 0;
- hibit = 0;
- lobit |= 1 << 18; // CET
- lobit |= 1 << 20; // Start Bit
- lobit |= (minutes % 10) << 21; // Minutes
- lobit |= (minutes / 10) << 25;
- hibit |= (hours % 10); // Hours
- hibit |= (hours / 10) << 4;
- hibit |= (day % 10) << 7; // Day of month
- hibit |= (day / 10) << 11;
- hibit |= week << 13; // Day of week
- hibit |= (month % 10) << 16; // Month number
- hibit |= (month / 10) << 20;
- hibit |= (year % 10) << 21; // Year within century
- hibit |= (year / 10) << 25;
- parity = 0;
- for(i = 21; i <= 27; ++i) {
- parity += (lobit >> i) & 1;
- }
- if(parity & 1)
- lobit |= 1 << 28;
- parity = 0;
- for(i = 0; i <= 5; ++i) {
- parity += (hibit >> i) & 1;
- }
- if(parity & 1)
- hibit |= 1 << 6;
- parity = 0;
- for(i = 7; i <= 28; ++i) {
- parity += (hibit >> i) & 1;
- }
- if(parity & 1)
- hibit |= 1 << 29;
- }
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, passwd);
- if(waitConnect() == false) {
- Serial.println("Can't connect");
- ESP.deepSleep(0);
- return;
- }
- timeClient.begin();
- timeClient.update();
- unixtime = timeClient.getEpochTime();
- starttime = unixtime;
- count = (unixtime % 60) * 10;
- Serial.println(unixtime);
- if(unixtime < 946652400) { // before 2000/01/01 00:00:00
- Serial.println("Can't get time");
- ESP.deepSleep(0);
- return;
- }
- WiFi.disconnect();
- delay(1);
- WiFi.mode(WIFI_OFF);
- WiFi.forceSleepBegin();
- delay(1);
- pinMode(DCF77OUT, OUTPUT);
- noInterrupts();
- timer0_isr_init();
- timer0_attachInterrupt(timer0_ISR);
- timer0_write(ESP.getCycleCount() + 8000000L); // 8MHz == 100ms
- interrupts();
- }
- void loop()
- {
- if(unixtime - starttime > 60 * 5) {
- ESP.deepSleep(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement