├── .gitattributes ├── .gitignore ├── ESPDailyTaskNTP.cpp ├── ESPDailyTaskNTP.h ├── credentials └── credentials.h ├── examples └── OncePerDayNTP │ ├── OncePerDayNTP.ino │ ├── Sparkfun.ino │ └── keys_0lgRlppWnWu7GyRD7brV.json └── keywords.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /ESPDailyTaskNTP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ESPdailyTask library for ESP8266 3 | 4 | 5 | This routine gets the unixtime from a NTP server and adjusts it to the time zone and the 6 | Middle European summer time if requested 7 | 8 | Copyright (c) 2016 Andreas Spiess 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | V1.0 2016-8-3 29 | 30 | */ 31 | 32 | #include "ESPDailyTaskNTP.h" 33 | 34 | extern "C" { 35 | #include "user_interface.h" // this is for the RTC memory read/write functions 36 | } 37 | 38 | SNTPtime NTPch("ch.pool.ntp.org"); 39 | 40 | #define OFF 0 41 | #define ON 1 42 | 43 | const unsigned long ONE_SECOND = 1000000; 44 | // const unsigned long ONE_SECOND = 1000; // for testing 45 | const unsigned long ONE_HOUR = 3595*ONE_SECOND; //120 seconds per day faster to be sure we are not late 46 | //const unsigned long ONE_HOUR = 2000000; 47 | //= 60 * 60 * ONE_SECOND; // number of microseconds (for deep_sleep of one hour) 48 | 49 | enum statusDef { 50 | RESET, 51 | COUNTING, 52 | CHECK, 53 | WORK 54 | }; 55 | 56 | typedef struct { 57 | byte markerFlag; 58 | byte counter; 59 | statusDef status; 60 | } rtcStore __attribute__((aligned(4))); 61 | rtcStore rtcMem; 62 | 63 | 64 | int wakeUpTime; 65 | byte _resetPin; 66 | char *_ssid, *_password; 67 | double _timeZone=0.0; 68 | unsigned long _sleepTime; 69 | 70 | strDateTime _wakeup, _actualTime; 71 | 72 | 73 | 74 | //Constructor 75 | ESPDailyTaskNTP::ESPDailyTaskNTP(int hours,int minutes, double timeZone, char *mySSID, char *myPASSWORD, byte RESET_PIN){ 76 | _wakeup.hour = hours; 77 | _wakeup.minute=minutes; 78 | _wakeup.second = 0; 79 | _resetPin=RESET_PIN; 80 | _ssid = mySSID; 81 | _password=myPASSWORD; 82 | _timeZone=timeZone; 83 | if (_resetPin!=99) pinMode(_resetPin,INPUT_PULLUP); 84 | } 85 | 86 | //Constructor 87 | ESPDailyTaskNTP::ESPDailyTaskNTP(int hours, int minutes, double timeZone, char *mySSID, char *myPASSWORD){ 88 | 89 | byte RESET_PIN=99; 90 | 91 | } 92 | 93 | 94 | void ESPDailyTaskNTP::sleepOneDay() { 95 | 96 | int _secondsToWait; 97 | 98 | unsigned long entry=millis(); 99 | 100 | system_rtc_mem_read(65, &rtcMem, sizeof(rtcMem)); 101 | if ((_resetPin!=99 && digitalRead(_resetPin)==0 )|| rtcMem.markerFlag!=85) rtcMem.status=RESET; 102 | 103 | switch (rtcMem.status) { 104 | 105 | case RESET: 106 | rtcMem.markerFlag = 85; 107 | rtcMem.counter = 0; 108 | _sleepTime=1; 109 | rtcMem.status = CHECK; 110 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 111 | printRtcMem("RESET "); 112 | ESP.deepSleep(_sleepTime, WAKE_RF_DEFAULT); 113 | break; 114 | 115 | case COUNTING: 116 | if (rtcMem.counter==0) { 117 | _sleepTime=1; 118 | rtcMem.status=CHECK; 119 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 120 | printRtcMem("COUNTING ZERO "); 121 | ESP.deepSleep(_sleepTime, WAKE_RF_DEFAULT); 122 | } 123 | else { 124 | rtcMem.counter--; 125 | _sleepTime=ONE_HOUR; 126 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 127 | printRtcMem("COUNTING DOWN "); 128 | ESP.deepSleep(_sleepTime, WAKE_RF_DISABLED); 129 | } 130 | Serial.print("This call took "); 131 | Serial.print(millis()-entry); 132 | Serial.println(" milliseconds"); 133 | break; 134 | 135 | case CHECK: 136 | _secondsToWait = adjustTime(); 137 | if (_secondsToWait>120) { 138 | if (_secondsToWait>3600) { 139 | rtcMem.counter = (int)(_secondsToWait/3600); 140 | rtcMem.status=COUNTING; 141 | _sleepTime=ONE_HOUR; 142 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 143 | printRtcMem("CHECK "); 144 | ESP.deepSleep(_sleepTime, WAKE_RF_DISABLED); 145 | } 146 | else { 147 | rtcMem.status=WORK; 148 | _sleepTime=_secondsToWait*ONE_SECOND; 149 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 150 | printRtcMem("CHECK AND WAIT FOR WORK "); 151 | ESP.deepSleep(_sleepTime, WAKE_RF_DEFAULT); 152 | } 153 | } 154 | else { 155 | rtcMem.status=WORK; 156 | _sleepTime=1; 157 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 158 | printRtcMem("CHECK 3 "); 159 | ESP.deepSleep(_sleepTime, WAKE_RF_DEFAULT); 160 | } 161 | break; 162 | 163 | case WORK: 164 | break; 165 | 166 | } 167 | } 168 | 169 | int ESPDailyTaskNTP::adjustTime() { 170 | long _currentSecs,_wakeUpSecs; 171 | int _seconds; 172 | unsigned long entry=millis(); 173 | WiFi.begin (_ssid, _password); 174 | while (WiFi.status() != WL_CONNECTED && millis()-entry<10000) { 175 | Serial.print("."); 176 | delay(500); 177 | } 178 | if (millis()-entry>10000) ESP.deepSleep(1, WAKE_RF_DEFAULT); // no connection possible, try again 179 | Serial.println("WiFi connected"); 180 | 181 | entry=millis(); 182 | while(!NTPch.setSNTPtime()&& millis()-entry<10000) Serial.println("x"); 183 | 184 | if (millis()-entry<10000) { 185 | _actualTime = NTPch.getTime(_timeZone, true); 186 | NTPch.printDateTime(_actualTime); 187 | _currentSecs = ((_actualTime.hour * 60) + _actualTime.minute) * 60 + _actualTime.second; 188 | _wakeUpSecs = ((_wakeup.hour * 60) + _wakeup.minute) * 60 + _wakeup.second; 189 | _seconds=(_wakeUpSecs-_currentSecs>0) ? (_wakeUpSecs-_currentSecs) : (_wakeUpSecs-_currentSecs+(24*3600)); 190 | Serial.printf("_currentSecs: %3d\r\n",_currentSecs); 191 | Serial.printf("_wakeUpSecs: %3d\r\n",_wakeUpSecs); 192 | Serial.printf("_secondsToGo: %3d\r\n",_seconds); 193 | return _seconds; 194 | } else ESP.deepSleep(1, WAKE_RF_DEFAULT); // Setting NTP time was not successful 195 | } 196 | 197 | void ESPDailyTaskNTP::printRtcMem(String place) { 198 | Serial.print(place); 199 | Serial.print(" "); 200 | // Serial.print("rtc marker: "); 201 | // Serial.print(rtcMem.markerFlag); 202 | Serial.print("Status: "); 203 | Serial.print(rtcMem.status); 204 | Serial.print(", markerFlag: "); 205 | Serial.print(rtcMem.markerFlag); 206 | Serial.print(", counter: "); 207 | Serial.print(rtcMem.counter); 208 | Serial.print(", sleepTime: "); 209 | Serial.print(_sleepTime); 210 | Serial.println(); 211 | } 212 | 213 | 214 | void ESPDailyTaskNTP::backToSleep() { 215 | rtcMem.counter = 23; //24 hours to sleep 216 | _sleepTime=ONE_HOUR; 217 | rtcMem.status=COUNTING; 218 | system_rtc_mem_write(65, &rtcMem, sizeof(rtcMem)); 219 | printRtcMem("WORK "); 220 | ESP.deepSleep(_sleepTime, WAKE_RF_DISABLED); 221 | } 222 | -------------------------------------------------------------------------------- /ESPDailyTaskNTP.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESPdailyTask library for ESP8266 3 | 4 | 5 | This routine gets the unixtime from a NTP server and adjusts it to the time zone and the 6 | Middle European summer time if requested 7 | 8 | Copyright (c) 2016 Andreas Spiess 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | V1.0 2016-8-3 29 | 30 | */ 31 | 32 | #ifndef _ESP_DAILY_TASK_H 33 | #define _ESP_DAILY_TASK_H 34 | 35 | #include 36 | #include 37 | 38 | 39 | class ESPDailyTaskNTP { 40 | 41 | public: 42 | ESPDailyTaskNTP(int hours,int minutes, double timeZone, char *mySSID, char *myPASSWORD); 43 | ESPDailyTaskNTP(int hours,int minutes, double timeZone, char *mySSID, char *myPASSWORD, byte RESET_PIN); 44 | 45 | void sleepOneDay(); 46 | void backToSleep(); 47 | 48 | private: 49 | void printRtcMem(String place); 50 | void processCurrentTime(int time); 51 | int adjustTime(); 52 | }; 53 | 54 | #endif // _ESP_DAILY_TASK_H 55 | -------------------------------------------------------------------------------- /credentials/credentials.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | char my_SSID[] = " "; // ssid of your accesspoint 4 | char my_PASSWORD[] = " "; // password of access point 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /examples/OncePerDayNTP/OncePerDayNTP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | OncePerDay example for ESP8266 4 | This routine gets the unixtime from a NTP server and adjusts it to the time zone and the 5 | Middle European summer time if requested 6 | 7 | Copyright (c) 2016 Andreas Spiess 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | V1.1 2016-8-15 28 | 29 | */ 30 | 31 | /* 32 | Content of credential.h 33 | 34 | char my_SSID[]= " "; // ssid of your accesspoint 35 | char my_PASSWORD[]= " "; // password of access point 36 | 37 | Move credentials.h into the Arduino libraries folder (top level) 38 | 39 | */ 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | char SNTP_SERVER[]="ch.pool.ntp.org"; 47 | 48 | SNTPtime NTPwork(SNTP_SERVER); 49 | 50 | #define RESET_PIN D2 51 | 52 | ESPDailyTaskNTP dailyTask(12, 0, 1.0, my_SSID, my_PASSWORD, RESET_PIN); // Hour to do the task 53 | 54 | void setup() { 55 | Serial.begin(115200); 56 | Serial.println(); Serial.println("Start"); Serial.println(); 57 | 58 | dailyTask.sleepOneDay(); 59 | 60 | // ------------------ put the code for your daily task here ------------------------------- 61 | 62 | Serial.println("............ W O R K ..............................."); 63 | NTPwork.setSNTPtime(); 64 | strDateTime _time = NTPwork.getTime(1.0, 1); 65 | NTPwork.printDateTime(_time); 66 | 67 | sendSparkfun(1, "AndreasSpiess", _time.year, _time.month, _time.day, _time.hour, _time.minute); 68 | // ----------------------- end of code for your daily task------------------------------- 69 | 70 | // and back to sleep once daily code is done 71 | dailyTask.backToSleep(); 72 | } 73 | 74 | void loop() { 75 | // sleeping so wont get here 76 | } 77 | -------------------------------------------------------------------------------- /examples/OncePerDayNTP/Sparkfun.ino: -------------------------------------------------------------------------------- 1 | 2 | WiFiClient client; 3 | 4 | 5 | bool sendSparkfun(byte sparkfunType, String youtubename,int year, int month, int day, int hour, int minute) { 6 | 7 | // Use Sparkfun testing stream 8 | const char* host = "data.sparkfun.com"; 9 | const char* streamId = "0lgRlppWnWu7GyRD7brV"; 10 | const char* privateKey = "D6GZ6jj7k7cMGJRzM1n6"; 11 | 12 | Serial.print("Connecting to "); Serial.print(host); 13 | 14 | int retries = 5; 15 | while (!client.connect(host, 80) && (retries-- > 0)) { 16 | Serial.print("."); 17 | } 18 | Serial.println(); 19 | if (!client.connected()) { 20 | Serial.println("Failed to connect, going back to sleep"); 21 | return false; 22 | } 23 | 24 | String url = "/input/"; 25 | url += streamId; 26 | url += "?private_key="; 27 | url += privateKey; 28 | url += "&youtubename="; 29 | url += youtubename; 30 | url += "&year="; 31 | url += year; 32 | url += "&month="; 33 | url += month; 34 | url += "&day="; 35 | url += day; 36 | url += "&hour="; 37 | url += hour; 38 | url += "&minute="; 39 | url += minute; 40 | 41 | Serial.println(); 42 | Serial.print("sparkfun: "); 43 | 44 | 45 | Serial.print("Request URL: "); Serial.println(url); 46 | 47 | client.print(String("GET ") + url + 48 | " HTTP/1.1\r\n" + 49 | "Host: " + host + "\r\n" + 50 | "Connection: close\r\n\r\n"); 51 | 52 | int timeout = 5 * 10; // 5 seconds 53 | while (!client.available() && (timeout-- > 0)) { 54 | delay(100); 55 | } 56 | 57 | if (!client.available()) { 58 | Serial.println("No response, going back to sleep"); 59 | return false; 60 | } 61 | Serial.println(F("disconnected")); 62 | return true; 63 | } 64 | -------------------------------------------------------------------------------- /examples/OncePerDayNTP/keys_0lgRlppWnWu7GyRD7brV.json: -------------------------------------------------------------------------------- 1 | {"title":"TestStream OncePerDay","outputUrl":"http://data.sparkfun.com/output/0lgRlppWnWu7GyRD7brV","inputUrl":"http://data.sparkfun.com/input/0lgRlppWnWu7GyRD7brV","manageUrl":"http://data.sparkfun.com/streams/0lgRlppWnWu7GyRD7brV","publicKey":"0lgRlppWnWu7GyRD7brV","privateKey":"D6GZ6jj7k7cMGJRzM1n6","deleteKey":"r3yO3kkaXaHAYJMLApbv"} -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Bridge 3 | ####################################### 4 | 5 | ####################################### 6 | # Class (KEYWORD1) 7 | ####################################### 8 | 9 | ESPDailyTask KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | sleep1Day KEYWORD2 16 | backToSleep KEYWORD2 17 | timeAdjustFromDateHeader KEYWORD2 18 | 19 | --------------------------------------------------------------------------------