Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ntp client #120

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
7 changes: 7 additions & 0 deletions include/network/time_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <Arduino.h>

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";
5 changes: 3 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigClass_defaultValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/config_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/main_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "sensors/stastics.h"
#include "watchdog.h"
#include "wifiutil.h"
#include "network/time_client.h"

WiFiClient net;

Expand Down Expand Up @@ -170,6 +171,8 @@ void setup_normal() {
mainlog("TimerCall version: " + String(timer.VERSION, 2));
init_sensors();

ntp_setup();

// TimerCall
add_timer_tasks();
timer.forceRunStasticsOnce();
Expand Down
54 changes: 54 additions & 0 deletions src/network/time_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <Arduino.h>

#ifdef esp32
#include <esp_sntp.h>

#endif
#ifdef ESP8266
#include <time.h>
#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
13 changes: 9 additions & 4 deletions src/sensors/stastics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
#include "log.h"
#include "TimerCall.h"

#include "network/time_client.h"

// 統計情報を取得
void updateStastics(std::vector<TimerCall::TimerCallTask> &tasks) {
const String STAT = "stastics";

DynamicJsonDocument doc(500);
String datetime = getFormattedTime();
doc["datetime"] = datetime;
doc["time"] = millis();

int idx = 0;
Expand All @@ -30,19 +34,20 @@ void updateStastics(std::vector<TimerCall::TimerCallTask> &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 = "";
Expand Down
10 changes: 9 additions & 1 deletion src/utils/log.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#include <Arduino.h>
#include "network/time_client.h"

void real_log(String msgString, String prefixString) {
char log[130];
char prefix[10];
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);
}

Expand Down