Browse Source

Return time in ISO format

fastled
Xose Pérez 7 years ago
parent
commit
a27c8a3145
4 changed files with 22 additions and 8 deletions
  1. +2
    -1
      code/espurna/config/general.h
  2. +2
    -2
      code/espurna/espurna.ino
  3. +3
    -2
      code/espurna/mqtt.ino
  4. +15
    -3
      code/espurna/ntp.ino

+ 2
- 1
code/espurna/config/general.h View File

@ -221,7 +221,8 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_TOPIC_RSSI "rssi"
#define MQTT_TOPIC_APP "app"
#define MQTT_TOPIC_INTERVAL "interval"
#define MQTT_TOPIC_HOSTNAME "hostname"
#define MQTT_TOPIC_HOSTNAME "host"
#define MQTT_TOPIC_TIME "time"
#define MQTT_TOPIC_ANALOG "analog"
// Periodic reports


+ 2
- 2
code/espurna/espurna.ino View File

@ -42,7 +42,7 @@ void heartbeat() {
unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
unsigned int free_heap = ESP.getFreeHeap();
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) NTP.getTimeDateString().c_str());
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
if (!mqttConnected()) {
DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
@ -62,7 +62,7 @@ void heartbeat() {
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif
#if (MQTT_REPORT_HOSTNAME)
//mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif
#if (MQTT_REPORT_IP)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());


+ 3
- 2
code/espurna/mqtt.ino View File

@ -99,8 +99,9 @@ void _mqttFlush() {
mqtt_message_t element = _mqtt_queue[i];
root[element.topic] = element.message;
}
root["time"] = NTP.getTimeDateString();
root["hostname"] = getSetting("hostname", HOSTNAME);
if (ntpConnected()) root[MQTT_TOPIC_TIME] = ntpDateTime();
root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname", HOSTNAME);
root[MQTT_TOPIC_IP] = getIP();
String output;
root.printTo(output);


+ 15
- 3
code/espurna/ntp.ino View File

@ -29,8 +29,21 @@ bool ntpConnected() {
return (timeStatus() == timeSet);
}
void ntpSetup() {
String ntpDateTime() {
if (!ntpConnected()) return String("Not set");
String value = NTP.getTimeDateString();
int hour = value.substring(0, 2).toInt();
int minute = value.substring(3, 5).toInt();
int second = value.substring(6, 8).toInt();
int day = value.substring(9, 11).toInt();
int month = value.substring(12, 14).toInt();
int year = value.substring(15, 19).toInt();
char buffer[20];
sprintf(buffer, "%04d/%02d/%02dT%02d:%02d:%02d", year, month, day, hour, minute, second);
return String(buffer);
}
void ntpSetup() {
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) {
if (error == noResponse) {
@ -40,11 +53,10 @@ void ntpSetup() {
}
wsSend("{\"ntpStatus\": false}");
} else {
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) NTP.getTimeDateString(NTP.getLastNTPSync()).c_str());
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
wsSend("{\"ntpStatus\": true}");
}
});
}
void ntpLoop() {


Loading…
Cancel
Save