diff --git a/src/ConfigClass.h b/include/ConfigClass.h similarity index 100% rename from src/ConfigClass.h rename to include/ConfigClass.h diff --git a/include/network/time_client.h b/include/network/time_client.h new file mode 100644 index 0000000..8d02ac3 --- /dev/null +++ b/include/network/time_client.h @@ -0,0 +1,7 @@ +#include + +void ntp_setup(); +String getFormattedTime(); + +/** time is not set due to NTP failed or NTP disabled */ +const String TIME_NOT_READY = "0000/00/00 00:00:00"; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 63d2ddd..03cdfb8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,14 +10,15 @@ [platformio] description = "EnvBoyX" -default_envs = esp32dev +#default_envs = esp32dev +default_envs = esp12e [env] extra_scripts = pre:build_script/create_embed.py [env:esp32dev] -platform = https://github.com/platformio/platform-espressif32.git +platform = espressif32 board = esp32dev framework = arduino board_build.mcu = esp32 diff --git a/src/ConfigClass_defaultValue.cpp b/src/ConfigClass_defaultValue.cpp index df07a9a..8207925 100644 --- a/src/ConfigClass_defaultValue.cpp +++ b/src/ConfigClass_defaultValue.cpp @@ -22,7 +22,7 @@ void Config::loadDefaultValue() { this->addConfig(ConfigNames::DISPLAY_FLIP, ConfigValues::DISPLAY_FLIP_OFF); this->addConfig(ConfigNames::DISPLAY_BRIGHTNESS, "255"); - this->addConfig(ConfigNames::DISPLAY_RECONFIG, ConfigValues::DISPLAY_RECONFIG_ON); // v44; + this->addConfig(ConfigNames::DISPLAY_RECONFIG, ConfigValues::DISPLAY_RECONFIG_SKIP); // v44: 追加(ON) v47: デフォルトOFF this->addConfig(ConfigNames::DISPLAY_AUTODIM_LUX, "5"); // v45: この明るさ以下がDuration秒続けば消灯 this->addConfig(ConfigNames::DISPLAY_AUTODIM_WAIT_SEC, "10"); // v45: 消灯までの時間 second diff --git a/src/config_names.cpp b/src/config_names.cpp index c04dda8..20f544a 100644 --- a/src/config_names.cpp +++ b/src/config_names.cpp @@ -9,6 +9,8 @@ const String ConfigNames::OPMODE = "opMode"; const String ConfigNames::DISPLAY_FLIP = "displayFlip"; const String ConfigNames::DISPLAY_BRIGHTNESS = "displayBrightness"; + +/** Show and wait for reconfig screen on boot */ const String ConfigNames::DISPLAY_RECONFIG = "displayWaitForReconfigure"; const String ConfigNames::DISPLAY_AUTODIM_LUX = "displayAutoDimLux"; // v45 diff --git a/src/main_normal.cpp b/src/main_normal.cpp index 3a88001..6ba4471 100644 --- a/src/main_normal.cpp +++ b/src/main_normal.cpp @@ -26,6 +26,7 @@ #include "sensors/stastics.h" #include "watchdog.h" #include "wifiutil.h" +#include "network/time_client.h" WiFiClient net; @@ -170,6 +171,8 @@ void setup_normal() { mainlog("TimerCall version: " + String(timer.VERSION, 2)); init_sensors(); + ntp_setup(); + // TimerCall add_timer_tasks(); timer.forceRunStasticsOnce(); diff --git a/src/network/time_client.cpp b/src/network/time_client.cpp new file mode 100644 index 0000000..660c254 --- /dev/null +++ b/src/network/time_client.cpp @@ -0,0 +1,54 @@ +#include + +#ifdef esp32 +#include + +#endif +#ifdef ESP8266 +#include +#endif + +#include "log.h" +#include "network/time_client.h" + +void ntp_setup() { + + cfglog("Communicating NTP server."); + + configTzTime("JST-9", "10.1.0.10"); + + cfglog("Communicating NTP server done."); +} + +#ifdef esp32 +struct tm timeinfo; + +String getFormattedTime() { + if (!getLocalTime(&timeinfo, 1)) { + return TIME_NOT_READY; + } + + char buf[64]; + strftime(buf, 64, "%Y/%m/%d %H:%M:%S", &timeinfo); + // String s = fmt.Sprintf(&timeinfo, "%A, %B %d %Y %H:%M:%S"); + return String(buf); +} +#endif +#ifdef ESP8266 +time_t t; +struct tm *tm; + +String getFormattedTime() { + + char buf[50]; + + t = time(NULL); + tm = localtime(&t); + + sprintf(buf, " %04d/%02d/%02d %02d:%02d:%02d", + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + + return String(buf); +} +#endif diff --git a/src/sensors/stastics.cpp b/src/sensors/stastics.cpp index fceeb8a..0d3cb41 100644 --- a/src/sensors/stastics.cpp +++ b/src/sensors/stastics.cpp @@ -5,11 +5,15 @@ #include "log.h" #include "TimerCall.h" +#include "network/time_client.h" + // 統計情報を取得 void updateStastics(std::vector &tasks) { const String STAT = "stastics"; DynamicJsonDocument doc(500); + String datetime = getFormattedTime(); + doc["datetime"] = datetime; doc["time"] = millis(); int idx = 0; @@ -30,19 +34,20 @@ void updateStastics(std::vector &tasks) { } String logmsg = ""; - if (DEBUG_BUILD) logmsg += "**DEBUG BUILD** "; - logmsg += "Statstics:"; #ifdef ESP32 doc["cputemp"] = temperatureRead(); // CPU温度 - logmsg += " ESP32: cpuTemp=" + String(temperatureRead()); + logmsg += "ESP32: cpuTemp=" + String(temperatureRead()); logmsg += " freeHeap=" + String(ESP.getFreeHeap()); #endif #ifdef ESP8266 - logmsg += " ESP8266: freeHeap=" + String(ESP.getFreeHeap()); + logmsg += "ESP8266: freeHeap=" + String(ESP.getFreeHeap()); #endif + logmsg += " datetime=" + datetime; + if (DEBUG_BUILD) logmsg += " **DEBUG BUILD** "; + statlog(logmsg); // これくらいは出しておかないと動いてるのかわからなくなるので出す String json = ""; diff --git a/src/utils/log.cpp b/src/utils/log.cpp index 00d46e3..83afb52 100644 --- a/src/utils/log.cpp +++ b/src/utils/log.cpp @@ -1,4 +1,5 @@ #include +#include "network/time_client.h" void real_log(String msgString, String prefixString) { char log[130]; @@ -6,7 +7,14 @@ void real_log(String msgString, String prefixString) { char msg[100]; prefixString.toCharArray(prefix, sizeof prefix); msgString.toCharArray(msg, sizeof msg); - snprintf(log, sizeof log, "%08lu %-10s: %s", millis(), prefix, msg); + + String datetime = getFormattedTime(); + + if (datetime != TIME_NOT_READY) { + snprintf(log, sizeof log, "%s %08lu %-10s: %s", datetime, millis(), prefix, msg); + } else { + snprintf(log, sizeof log, "%08lu %-10s: %s", millis(), prefix, msg); + } Serial.println(log); }