Skip to content

Commit

Permalink
Add timezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
matjack1 committed Nov 7, 2023
1 parent d16a310 commit 104a4d6
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 412 deletions.
145 changes: 0 additions & 145 deletions src/Ntp.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions src/Ntp.h

This file was deleted.

5 changes: 4 additions & 1 deletion src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ bool ICACHE_FLASH_ATTR loadConfiguration(Config &config)
config.ntpServer = strdup(ntp["server"]);
}
config.ntpInterval = ntp["interval"];
config.timeZone = ntp["timezone"];
if (ntp.containsKey("tzinfo"))
{
config.tzInfo = strdup(ntp["tzinfo"]);
}
config.activateTime[0] = hardware["rtime"];
config.lockType[0] = hardware["ltype"];
config.relayType[0] = hardware["rtype"];
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Config {
bool removeParityBits = true;
IPAddress subnetIp;
const char *ssid;
int timeZone = 0;
char *tzInfo = (char *)"";
const char *wifiApIp = NULL;
const char *wifiApSubnet = NULL;
uint8_t wifipin = 255;
Expand Down
32 changes: 14 additions & 18 deletions src/helpers.esp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@ void ICACHE_FLASH_ATTR parseBytes(const char *str, char sep, byte *bytes, int ma
}
}

String ICACHE_FLASH_ATTR generateUid(int type = 0, int length = 12)
// This is part of the latest releases of Arduino for ESP8266
// https://github.com/SensorsIot/NTP-time-for-ESP8266-and-ESP32/
bool getNTPtime(int sec)
{

// nardev: this could be implemented in config, to choose default type of UID;

String uid;
if (type)
uint32_t start = millis();
time_t previousEpoch = epoch;
do
{
uid = now();
}
else
time(&epoch);
localtime_r(&epoch, &timeinfo);
delay(10);
} while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
if (timeinfo.tm_year <= (2016 - 1900))
{
// char *characters = "abcdefghijklmnopqrstuvwxyz0123456789"; small letters, ordered
// char *characters = "ABSDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; uppercase, ordered
const char *characters = "SN07YXRGP9DBOUVLJK6IEH1FWMT8Q4SA3Z52"; // randomized, uppercase

for (int i = 0; i < length; i++)
{
uid = uid + characters[random(0, 36)];
}
epoch = previousEpoch; // fallback to previous value, so that you can use the manually set time
return false; // the NTP call was not successful
}

return uid;
return true;
}
4 changes: 2 additions & 2 deletions src/log.esp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String d
root["src"] = src;
root["desc"] = desc;
root["data"] = data;
root["time"] = now();
root["time"] = epoch;
if (config.mqttEvents && config.mqttEnabled) // log to MQTT
{
root["cmd"] = "event";
Expand All @@ -32,7 +32,7 @@ void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype)
root["uid"] = uid;
root["username"] = username;
root["acctype"] = acctype;
root["timestamp"] = now();
root["timestamp"] = epoch;
File latestlog = SPIFFS.open("/latestlog.json", "a");
serializeJson(root, latestlog);
latestlog.print("\n");
Expand Down
16 changes: 9 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SOFTWARE.
#include <ESPAsyncWebServer.h>
#include <TimeLib.h>
#include <Ticker.h>
#include "Ntp.h"
#include <time.h>
#include <AsyncMqttClient.h>
#include <Bounce2.h>
#include "magicnumbers.h"
Expand Down Expand Up @@ -66,7 +66,6 @@ bool deactivateRelay[MAX_NUM_RELAYS] = {false, false, false, false};
#include "webh/esprfid.htm.gz.h"
#include "webh/index.html.gz.h"

NtpClient NTP;
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
Ticker wifiReconnectTimer;
Expand Down Expand Up @@ -95,10 +94,12 @@ uint8_t lastDoorbellState = 0;
uint8_t lastDoorState = 0;
uint8_t lastTamperState = 0;
unsigned long nextbeat = 0;
time_t epoch;
unsigned long openDoorMillis = 0;
unsigned long previousLoopMillis = 0;
unsigned long previousMillis = 0;
bool shouldReboot = false;
tm timeinfo;
unsigned long uptimeSeconds = 0;
unsigned long wifiPinBlink = millis();
unsigned long wiFiUptimeMillis = 0;
Expand Down Expand Up @@ -174,14 +175,15 @@ void ICACHE_RAM_ATTR loop()
{
currentMillis = millis();
deltaTime = currentMillis - previousLoopMillis;
uptimeSeconds = NTP.getUptimeSec();
uptimeSeconds = currentMillis / 1000;
previousLoopMillis = currentMillis;
getNTPtime(10);

openLockButton.update();
if (config.openlockpin != 255 && openLockButton.fell())
{
writeLatest(" ", "Button", 1);
mqttPublishAccess(now(), "true", "Always", "Button", " ");
mqttPublishAccess(epoch, "true", "Always", "Button", " ");
activateRelay[0] = true;
beeperValidAccess();
// TODO: handle other relays
Expand Down Expand Up @@ -307,10 +309,10 @@ void ICACHE_RAM_ATTR loop()

if (config.mqttEnabled && mqttClient.connected())
{
if ((unsigned)now() > nextbeat)
if ((unsigned)epoch > nextbeat)
{
mqttPublishHeartbeat(now(), uptimeSeconds);
nextbeat = (unsigned)now() + config.mqttInterval;
mqttPublishHeartbeat(epoch, uptimeSeconds);
nextbeat = (unsigned)epoch + config.mqttInterval;
#ifdef DEBUG
Serial.print("[ INFO ] Nextbeat=");
Serial.println(nextbeat);
Expand Down
8 changes: 4 additions & 4 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door open");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(now(), "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
for (int currentRelay = 0; currentRelay < config.numRelays; currentRelay++)
{
activateRelay[currentRelay] = true;
Expand All @@ -533,7 +533,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door open");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(now(), "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
const char *door = incomingMessage->door;
String stringDoor = String(door);
int currentRelay = stringDoor.toInt();
Expand All @@ -549,7 +549,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door close");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(now(), "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
const char *door = incomingMessage->door;
String stringDoor = String(door);
int currentRelay = stringDoor.toInt();
Expand Down Expand Up @@ -679,7 +679,7 @@ void onMqttConnect(bool sessionPresent)
#endif
writeEvent("INFO", "mqtt", "Connected to MQTT Server", "Session Present");
}
mqttPublishBoot(now());
mqttPublishBoot(epoch);

String stopic(config.mqttTopic);
stopic = stopic + "/cmd";
Expand Down

0 comments on commit 104a4d6

Please sign in to comment.