diff --git a/platformio.ini b/platformio.ini index 05c240d..f1e7698 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,6 +11,8 @@ [common_env_data] upload_speed = 460800 monitor_speed = 74880 +monitor_rts = 0 +monitor_dtr = 1 framework = arduino platform = espressif8266 build_unflags = @@ -34,8 +36,6 @@ lib_deps = DS18B20 ArduinoLog DoubleResetDetect - Time - NtpClientLib CircularBuffer LCBUrl https://github.com/lbussy/AsyncWiFiManager.git @@ -46,6 +46,8 @@ build_type = release [env:d1_mini] upload_speed = ${common_env_data.upload_speed} monitor_speed = ${common_env_data.monitor_speed} +monitor_rts = ${common_env_data.monitor_rts} +monitor_dtr = ${common_env_data.monitor_dtr} framework = ${common_env_data.framework} platform = ${common_env_data.platform} ; build_unflags = ${common_env_data.build_unflags} diff --git a/src/bubbles.cpp b/src/bubbles.cpp index 4c305b1..cb5f730 100644 --- a/src/bubbles.cpp +++ b/src/bubbles.cpp @@ -54,16 +54,12 @@ void Bubbles::start() { single->lastVes = 0.0; // Set starting time - NtpHandler *ntpTime = NtpHandler::getInstance(); - ntpTime->update(); - single->lastTime = ntpTime->Time; + strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime())); } void Bubbles::update() { // Regular update loop, once per minute // Handle NTP Time - NtpHandler *ntpTime = NtpHandler::getInstance(); - ntpTime->update(); - single->lastTime = ntpTime->Time; + strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime())); // Store last values single->lastBpm = single->getRawBpm(); diff --git a/src/bubbles.h b/src/bubbles.h index 4cc7b41..d1f4803 100644 --- a/src/bubbles.h +++ b/src/bubbles.h @@ -25,7 +25,7 @@ SOFTWARE. */ #include "config.h" #include "jsonconfig.h" -#include "ntphandler.h" +#include "ntp.h" #include "DS18B20.h" #include "OneWire.h" #include @@ -59,7 +59,7 @@ class Bubbles { // Other Declarations void handleInterrupts(void); void update(); // Call every 60 seconds - char* lastTime; + char lastTime[22]; float getAvgAmbient(); float getAvgVessel(); float getAvgBpm(); diff --git a/src/bubserial.cpp b/src/bubserial.cpp index 55b2673..157cb82 100644 --- a/src/bubserial.cpp +++ b/src/bubserial.cpp @@ -35,13 +35,12 @@ void serial() { // Start serial with auto-detected rate (default to BAUD) } void printTimestamp(Print* _logOutput) { - NtpHandler *ntpTime = NtpHandler::getInstance(); - ntpTime->update(); - char locTime[prefLen] = {'\0'}; - strlcpy(locTime, ntpTime->Time, sizeof(locTime)); + char locTime[22] = {'\0'}; + strlcpy(locTime, getDTS().c_str(), getDTS().length()); _logOutput->print(locTime); } + #else // DISABLE_LOGGING void serial(){} diff --git a/src/bubserial.h b/src/bubserial.h index 5b1febf..079427d 100644 --- a/src/bubserial.h +++ b/src/bubserial.h @@ -25,7 +25,7 @@ SOFTWARE. */ #include "config.h" #include "tools.h" -#include "ntphandler.h" +#include "ntp.h" #include void serial(); diff --git a/src/main.cpp b/src/main.cpp index 8ce0ee8..d85ccd6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,13 +42,11 @@ void setup() { doWiFi(); } - NtpHandler *ntpTime = NtpHandler::getInstance(); - ntpTime->start(); - - initWebServer(); // Turn on web server - mdnssetup(); // Set up mDNS responder - execspiffs(); // Check for pending SPIFFS update - loadBpm() ; // Get last BPM reading if it was a controlled reboot + setClock(); // Set NTP Time + initWebServer(); // Turn on web server + mdnssetup(); // Set up mDNS responder + execspiffs(); // Check for pending SPIFFS update + loadBpm() ; // Get last BPM reading if it was a controlled reboot Log.notice(F("Started %s version %s (%s) [%s]." CR), API_KEY, version(), branch(), build()); } diff --git a/src/main.h b/src/main.h index cb8a8e4..eaa69a8 100644 --- a/src/main.h +++ b/src/main.h @@ -36,6 +36,7 @@ SOFTWARE. */ #include "pushhelper.h" #include "bubbles.h" #include "mdns.h" +#include "ntp.h" #include #include #include diff --git a/src/ntp.cpp b/src/ntp.cpp new file mode 100644 index 0000000..e5bb5b5 --- /dev/null +++ b/src/ntp.cpp @@ -0,0 +1,152 @@ +/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy) + +This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles). + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ + +#include "ntp.h" + +// Check: +// https://github.com/dalmatianrex/articles/tree/master/makerpro-esp8266-ntp +// for additional functionality + +void setClock() { + Ticker blinker; + Log.notice(F("Entering blocking loop to get NTP time.")); + blinker.attach_ms(NTPBLINK, ntpBlinker); + configTime(0, 0, "pool.ntp.org", "time.nist.gov"); + time_t nowSecs = time(nullptr); + while (nowSecs < 8 * 3600 * 2) { +#ifdef LOG_LEVEL + Serial.print(F(".")); +#endif + _delay(500); + yield(); + nowSecs = time(nullptr); + } + blinker.detach(); +#ifdef LOG_LEVEL + Serial.println(); +#endif + Log.notice(F("NTP time set.")); + struct tm timeinfo; + gmtime_r(&nowSecs, &timeinfo); +} + +String getDTS() { + // Returns JSON-type string = 2019-12-20T13:59:39Z + time_t now; + time_t rawtime = time(&now); + struct tm ts; + ts = *localtime(&rawtime); + char dta[21] = {'\0'}; + strftime(dta, sizeof(dta), "%FT%TZ", &ts); + String dts = String(dta); + return dts; +} + +char * getJsonTime() { + char * dts = ""; + sprintf(dts, "%04u-%02u-%02uT%02u:%02u:%02uZ", getYear(), getMonth(), getDate(), getHour(), getMinute(), getSecond()); + return dts; +} + +int getYear() { + // tm_year = years since 1900 + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int year = 1900 + ts->tm_year; + return year; +} + +int getMonth() { + // tm_mon = months since January (0-11) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int month = ts->tm_mon; + return month; +} + +int getDate() { + // tm_mday = day of the month (1-31) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int day = ts->tm_mday; + return day; +} + +int getWday() { + // tm_wday = days since Sunday (0-6) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int wday = 1 + ts->tm_wday; + return wday; +} + +int getHour() { + // tm_hour = hours since midnight (0-23) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int hour = ts->tm_hour; + return hour; +} + +int getMinute() { + // tm_min = minutes after the hour (0-59) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int min = ts->tm_min; + return min; +} + +int getSecond() { + // tm_sec = seconds after the minute (0-60) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int sec = ts->tm_sec; + return sec; +} + +int getYDay() { + // tm_yday = days since January 1 (0-365) + time_t rawtime; + struct tm * ts; + time ( &rawtime ); + ts = gmtime ( &rawtime ); + int yday = ts->tm_yday; + return yday; +} + +void ntpBlinker() { + digitalWrite(LED, !(digitalRead(LED))); // Invert Current State of LED +} diff --git a/src/ntphandler.h b/src/ntp.h similarity index 62% rename from src/ntphandler.h rename to src/ntp.h index 4a629dc..e750847 100644 --- a/src/ntphandler.h +++ b/src/ntp.h @@ -20,40 +20,31 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef _NTPHANDLER_H -#define _NTPHANDLER_H +#ifndef _NTP_H +#define _NTP_H + +#ifdef ESP8266 +#include +#elif defined ESP32 +#include +#endif #include "config.h" #include "tools.h" -// #include -#include -#include +#include #include -#define NTP_TIMEOUT 1500 - +void setClock(); +String getDTS(); +char * getJsonTime(); +int getYear(); // tm_year +int getMonth(); // tm_mon +int getDate(); // tm_mday +int getWday(); // tm_wday +int getHour(); // tm_hour +int getMinute(); // tm_min +int getSecond(); // tm_sec +int getYDay(); // tm_yday void ntpBlinker(); -class NtpHandler { - private: - // Singleton Declarations - NtpHandler() {} - static NtpHandler *single; - // Other Declarations - bool syncEventTriggered; - NTPSyncEvent_t ntpEvent; - bool hasBeenSet; - void setup(); - void setJsonTime(); - - public: - // Singleton Declarations - static NtpHandler* getInstance(); - ~NtpHandler() {single = NULL;} - // Other Declarations - void start(); - void update(); - char Time[21]; // Hold the Time string -}; - -#endif // _NTPHANDLER_H +#endif // _NTP_H diff --git a/src/ntphandler.cpp b/src/ntphandler.cpp deleted file mode 100644 index 760fe27..0000000 --- a/src/ntphandler.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy) - -This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles). - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. */ - -#include "ntphandler.h" - -NtpHandler* NtpHandler::single = NULL; - -NtpHandler* NtpHandler::getInstance() { - if (!single) { - single = new NtpHandler(); - single->setup(); - } - return single; -} - -void NtpHandler::setup() { - single->syncEventTriggered = false; - single->hasBeenSet = false; - NTP.onNTPSyncEvent([](NTPSyncEvent_t event) { - single->ntpEvent = event; - single->syncEventTriggered = true; - }); -} - -void NtpHandler::start() { - Ticker blinker; - blinker.attach_ms(NTPBLINK, ntpBlinker); - NTP.setInterval(63); - NTP.setNTPTimeout(NTP_TIMEOUT); - NTP.begin(TIMESERVER, 0, false, 0); - Log.notice(F("Entering blocking loop to get NTP time.")); - single->update(); - while (!single->hasBeenSet) { - #ifdef LOG_LEVEL -#ifdef LOG_LEVEL - Serial.print(F(".")); -#endif - #endif - _delay(1000); - single->update(); - yield(); - } - #ifdef LOG_LEVEL -#ifdef LOG_LEVEL - Serial.println(); -#endif - single->setJsonTime(); - Log.notice(F("NTP Time set." CR)); - #endif - if (blinker.active()) blinker.detach(); // Turn off blinker - digitalWrite(LED, HIGH); // Turn off LED -} - -void NtpHandler::update() { - if (NTP.getLastNTPSync() > 0) { - single->hasBeenSet = true; - } - single->setJsonTime(); -} - -void NtpHandler::setJsonTime() { - sprintf(single->Time, "%04u-%02u-%02uT%02u:%02u:%02uZ", year(), month(), day(), hour(), minute(), second()); -} - -void ntpBlinker() { - digitalWrite(LED, !(digitalRead(LED))); // Invert Current State of LED -} diff --git a/src/webserver.cpp b/src/webserver.cpp index 2ef8d48..5aa5874 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -28,7 +28,7 @@ void initWebServer() { setWebAliases(); server.begin(); Log.notice(F("Async HTTP server started on port %l." CR) , PORT); - Log.verbose(F("Open: http://%s.local." CR), WiFi.hostname().c_str()); + Log.verbose(F("Open: http://%s.local to view controller application." CR), WiFi.hostname().c_str()); } void setWebAliases() {