From 2cd7a8717aff1d277b4777d7f90a3e086ed9e619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 4 Jun 2019 13:07:04 +0200 Subject: [PATCH] Upgraded NoFUSS support to 0.3, added build header to request, fixed bug in device string size --- code/espurna/config/all.h | 1 + code/espurna/config/buildtime.h | 97 +++++++++++++++++++++++++++++++++ code/espurna/nofuss.ino | 9 ++- code/espurna/utils.ino | 39 ++++--------- code/platformio.ini | 2 +- 5 files changed, 115 insertions(+), 33 deletions(-) create mode 100644 code/espurna/config/buildtime.h diff --git a/code/espurna/config/all.h b/code/espurna/config/all.h index 2494890c..1eef6bd5 100644 --- a/code/espurna/config/all.h +++ b/code/espurna/config/all.h @@ -28,6 +28,7 @@ #include "arduino.h" #include "hardware.h" #include "defaults.h" +#include "buildtime.h" #include "deprecated.h" #include "general.h" #include "dependencies.h" diff --git a/code/espurna/config/buildtime.h b/code/espurna/config/buildtime.h new file mode 100644 index 00000000..a9b854d1 --- /dev/null +++ b/code/espurna/config/buildtime.h @@ -0,0 +1,97 @@ +/* + * + * Created: 29.03.2018 + * + * Authors: + * + * Assembled from the code released on Stackoverflow by: + * Dennis (instructable.com/member/nqtronix) | https://stackoverflow.com/questions/23032002/c-c-how-to-get-integer-unix-timestamp-of-build-time-not-string + * and + * Alexis Wilke | https://stackoverflow.com/questions/10538444/do-you-know-of-a-c-macro-to-compute-unix-time-and-date + * + * Assembled by Jean Rabault + * + * UNIX_TIMESTAMP gives the UNIX timestamp (unsigned long integer of seconds since 1st Jan 1970) of compilation from macros using the compiler defined __TIME__ macro. + * This should include Gregorian calendar leap days, in particular the 29ths of February, 100 and 400 years modulo leaps. + * + * Careful: __TIME__ is the local time of the computer, NOT the UTC time in general! + * + */ + +#ifndef COMPILE_TIME_H_ +#define COMPILE_TIME_H_ + +// Some definitions for calculation +#define SEC_PER_MIN 60UL +#define SEC_PER_HOUR 3600UL +#define SEC_PER_DAY 86400UL +#define SEC_PER_YEAR (SEC_PER_DAY*365) + +// extracts 1..4 characters from a string and interprets it as a decimal value +#define CONV_STR2DEC_1(str, i) (str[i]>'0'?str[i]-'0':0) +#define CONV_STR2DEC_2(str, i) (CONV_STR2DEC_1(str, i)*10 + str[i+1]-'0') +#define CONV_STR2DEC_3(str, i) (CONV_STR2DEC_2(str, i)*10 + str[i+2]-'0') +#define CONV_STR2DEC_4(str, i) (CONV_STR2DEC_3(str, i)*10 + str[i+3]-'0') + +// Custom "glue logic" to convert the month name to a usable number +#define GET_MONTH(str, i) (str[i]=='J' && str[i+1]=='a' && str[i+2]=='n' ? 1 : \ + str[i]=='F' && str[i+1]=='e' && str[i+2]=='b' ? 2 : \ + str[i]=='M' && str[i+1]=='a' && str[i+2]=='r' ? 3 : \ + str[i]=='A' && str[i+1]=='p' && str[i+2]=='r' ? 4 : \ + str[i]=='M' && str[i+1]=='a' && str[i+2]=='y' ? 5 : \ + str[i]=='J' && str[i+1]=='u' && str[i+2]=='n' ? 6 : \ + str[i]=='J' && str[i+1]=='u' && str[i+2]=='l' ? 7 : \ + str[i]=='A' && str[i+1]=='u' && str[i+2]=='g' ? 8 : \ + str[i]=='S' && str[i+1]=='e' && str[i+2]=='p' ? 9 : \ + str[i]=='O' && str[i+1]=='c' && str[i+2]=='t' ? 10 : \ + str[i]=='N' && str[i+1]=='o' && str[i+2]=='v' ? 11 : \ + str[i]=='D' && str[i+1]=='e' && str[i+2]=='c' ? 12 : 0) + +// extract the information from the time string given by __TIME__ and __DATE__ +#define __TIME_SECOND__ (CONV_STR2DEC_2(__TIME__, 6)) +#define __TIME_MINUTE__ (CONV_STR2DEC_2(__TIME__, 3)) +#define __TIME_HOUR__ (CONV_STR2DEC_2(__TIME__, 0)) +#define __TIME_DAY__ (CONV_STR2DEC_2(__DATE__, 4)) +#define __TIME_MONTH__ (GET_MONTH(__DATE__, 0)) +#define __TIME_YEAR__ (CONV_STR2DEC_4(__DATE__, 7)) + +// Days in February +#define _UNIX_TIMESTAMP_FDAY(year) \ + (((year) % 400) == 0UL ? 29UL : \ + (((year) % 100) == 0UL ? 28UL : \ + (((year) % 4) == 0UL ? 29UL : \ + 28UL))) + +// Days in the year +#define _UNIX_TIMESTAMP_YDAY(year, month, day) \ + ( \ + /* January */ day \ + /* February */ + (month >= 2 ? 31UL : 0UL) \ + /* March */ + (month >= 3 ? _UNIX_TIMESTAMP_FDAY(year) : 0UL) \ + /* April */ + (month >= 4 ? 31UL : 0UL) \ + /* May */ + (month >= 5 ? 30UL : 0UL) \ + /* June */ + (month >= 6 ? 31UL : 0UL) \ + /* July */ + (month >= 7 ? 30UL : 0UL) \ + /* August */ + (month >= 8 ? 31UL : 0UL) \ + /* September */+ (month >= 9 ? 31UL : 0UL) \ + /* October */ + (month >= 10 ? 30UL : 0UL) \ + /* November */ + (month >= 11 ? 31UL : 0UL) \ + /* December */ + (month >= 12 ? 30UL : 0UL) \ + ) + +// get the UNIX timestamp from a digits representation +#define _UNIX_TIMESTAMP(year, month, day, hour, minute, second) \ + ( /* time */ second \ + + minute * SEC_PER_MIN \ + + hour * SEC_PER_HOUR \ + + /* year day (month + day) */ (_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * SEC_PER_DAY \ + + /* year */ (year - 1970UL) * SEC_PER_YEAR \ + + ((year - 1969UL) / 4UL) * SEC_PER_DAY \ + - ((year - 1901UL) / 100UL) * SEC_PER_DAY \ + + ((year - 1601UL) / 400UL) * SEC_PER_DAY \ + ) + +// the UNIX timestamp +#define __UNIX_TIMESTAMP__ (_UNIX_TIMESTAMP(__TIME_YEAR__, __TIME_MONTH__, __TIME_DAY__, __TIME_HOUR__, __TIME_MINUTE__, __TIME_SECOND__)) + +#endif \ No newline at end of file diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index 4ec35c36..5f28007f 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -54,16 +54,15 @@ void _nofussConfigure() { } else { - char buffer[20]; - snprintf_P(buffer, sizeof(buffer), PSTR("%s-%s"), APP_NAME, DEVICE); - NoFUSSClient.setServer(nofussServer); - NoFUSSClient.setDevice(buffer); + NoFUSSClient.setDevice(APP_NAME "_" DEVICE); NoFUSSClient.setVersion(APP_VERSION); + NoFUSSClient.setBuild(String(__UNIX_TIMESTAMP__)); DEBUG_MSG_P(PSTR("[NOFUSS] Server : %s\n"), nofussServer.c_str()); - DEBUG_MSG_P(PSTR("[NOFUSS] Dervice: %s\n"), buffer); + DEBUG_MSG_P(PSTR("[NOFUSS] Dervice: %s\n"), APP_NAME "_" DEVICE); DEBUG_MSG_P(PSTR("[NOFUSS] Version: %s\n"), APP_VERSION); + DEBUG_MSG_P(PSTR("[NOFUSS] Build: %s\n"), String(__UNIX_TIMESTAMP__).c_str()); DEBUG_MSG_P(PSTR("[NOFUSS] Enabled\n")); } diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 11e6f974..ca280b8f 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -98,35 +98,19 @@ String getEspurnaWebUI() { } String buildTime() { - - const char time_now[] = __TIME__; // hh:mm:ss - unsigned int hour = atoi(&time_now[0]); - unsigned int minute = atoi(&time_now[3]); - unsigned int second = atoi(&time_now[6]); - - const char date_now[] = __DATE__; // Mmm dd yyyy - const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - unsigned int month = 0; - for ( int i = 0; i < 12; i++ ) { - if (strncmp(date_now, months[i], 3) == 0 ) { - month = i + 1; - break; - } - } - unsigned int day = atoi(&date_now[3]); - unsigned int year = atoi(&date_now[7]); - - char buffer[20]; - snprintf_P( - buffer, sizeof(buffer), PSTR("%04d-%02d-%02d %02d:%02d:%02d"), - year, month, day, hour, minute, second - ); - - return String(buffer); - + #if NTP_SUPPORT + return ntpDateTime(__UNIX_TIMESTAMP__); + #else + char buffer[20]; + snprintf_P( + buffer, sizeof(buffer), PSTR("%04d-%02d-%02d %02d:%02d:%02d"), + __TIME_YEAR__, __TIME_MONTH__, __TIME_DAY__, + __TIME_HOUR__, __TIME_MINUTE__, __TIME_SECOND__ + ); + return String(buffer); + #endif } - unsigned long getUptime() { static unsigned long last_uptime = 0; @@ -422,6 +406,7 @@ void info() { DEBUG_MSG_P(PSTR("[MAIN] SDK version: %s\n"), ESP.getSdkVersion()); DEBUG_MSG_P(PSTR("[MAIN] Core version: %s\n"), getCoreVersion().c_str()); DEBUG_MSG_P(PSTR("[MAIN] Core revision: %s\n"), getCoreRevision().c_str()); + DEBUG_MSG_P(PSTR("[MAIN] Build time: %lu\n"), __UNIX_TIMESTAMP__); DEBUG_MSG_P(PSTR("\n")); // ------------------------------------------------------------------------- diff --git a/code/platformio.ini b/code/platformio.ini index 1d41ba3c..3884e3c2 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -96,7 +96,7 @@ lib_deps = https://github.com/xoseperez/justwifi.git#2.0.2 https://github.com/madpilot/mDNSResolver#4cfcda1 https://github.com/xoseperez/my92xx#3.0.1 - https://bitbucket.org/xoseperez/nofuss.git#0.2.5 + https://bitbucket.org/xoseperez/nofuss.git#0.3.0 https://github.com/xoseperez/NtpClient.git#0942ebc OneWire PZEM004T