NTP basic sync (#124)

* NTP basic sync
This commit is contained in:
Owen Carter 2021-06-08 20:42:14 +02:00 committed by GitHub
parent 84b55bad91
commit b33f9258e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 8 deletions

View File

@ -31,6 +31,7 @@
// Functions from the main .ino
extern void flashLED(int flashtime);
extern void setLamp(int newVal);
extern void printLocalTime(bool extraData);
// External variables declared in the main .ino
extern char myName[];
@ -56,6 +57,7 @@ extern bool autoLamp;
extern bool filesystem;
extern String critERR;
extern bool debugData;
extern bool haveTime;
extern int sketchSize;
extern int sketchSpace;
extern String sketchMD5;
@ -73,15 +75,14 @@ static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %
httpd_handle_t stream_httpd = NULL;
httpd_handle_t camera_httpd = NULL;
void serialDump() {
Serial.println("\r\nPreferences file: ");
dumpPrefs(SPIFFS);
if (critERR.length() > 0) {
Serial.printf("\r\n\r\nA critical error has occurred when initialising Camera Hardware, see startup megssages\r\n");
}
Serial.println();
// Module
Serial.printf("Name: %s\r\n", myName);
if (haveTime) {
Serial.print("Time: ");
printLocalTime(true);
}
Serial.printf("Firmware: %s (base: %s)\r\n", myVer, baseVersion);
float sketchPct = 100 * sketchSize / sketchSpace;
Serial.printf("Sketch Size: %i (total: %i, %.1f%% used)\r\n", sketchSize, sketchSpace, sketchPct);
@ -123,9 +124,15 @@ void serialDump() {
Serial.printf("Freq: %i MHz\r\n", ESP.getCpuFreqMHz());
Serial.printf("Heap: %i, free: %i, min free: %i, max block: %i\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap());
Serial.printf("Psram: %i, free: %i, min free: %i, max block: %i\r\n", ESP.getPsramSize(), ESP.getFreePsram(), ESP.getMinFreePsram(), ESP.getMaxAllocPsram());
// Filesystems
if (filesystem) {
Serial.printf("Spiffs: %i, used: %i\r\n", SPIFFS.totalBytes(), SPIFFS.usedBytes());
}
Serial.println("Preferences file: ");
dumpPrefs(SPIFFS);
if (critERR.length() > 0) {
Serial.printf("\r\n\r\nA critical error has occurred when initialising Camera Hardware, see startup megssages\r\n");
}
Serial.println();
return;
}
@ -522,6 +529,15 @@ static esp_err_t dump_handler(httpd_req_t *req){
// System
d+= sprintf(d,"<h2>System</h2>\n");
if (haveTime) {
struct tm timeinfo;
if(getLocalTime(&timeinfo)){
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%H:%M:%S, %A, %B %d %Y", &timeinfo);
//print like "const char*"
d+= sprintf(d,"Time: %s<br>\n", timeStringBuff);
}
}
int64_t sec = esp_timer_get_time() / 1000000;
int64_t upDays = int64_t(floor(sec/86400));
int upHours = int64_t(floor(sec/3600)) % 24;

View File

@ -5,6 +5,7 @@
#include <DNSServer.h>
#include <ArduinoOTA.h>
#include "src/parsebytes.h"
#include "time.h"
/* This sketch is a extension/expansion/reork of the 'official' ESP32 Camera example
@ -170,6 +171,17 @@ const int pwmMax = pow(2,pwmresolution)-1;
bool otaEnabled = true;
#endif
#if defined(NTPSERVER)
bool haveTime = true;
const char* ntpServer = NTPSERVER;
const long gmtOffset_sec = NTP_GMT_OFFSET;
const int daylightOffset_sec = NTP_DST_OFFSET;
#else
bool haveTime = false;
const char* ntpServer = "";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
#endif
// Critical error string; if set during init (camera hardware failure) it
// will be returned for all http requests
@ -226,6 +238,18 @@ void setLamp(int newVal) {
}
}
void printLocalTime(bool extraData=false) {
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
} else {
Serial.println(&timeinfo, "%H:%M:%S, %A, %B %d %Y");
}
if (extraData) {
Serial.printf("NTP Server: %s, GMT Offset: %li(s), DST Offset: %i(s)\r\n", ntpServer, gmtOffset_sec, daylightOffset_sec);
}
}
void WifiSetup() {
// Feedback that we are now attempting to connect
flashLED(300);
@ -642,6 +666,15 @@ void setup() {
Serial.println("OTA is disabled");
}
// Set time via NTP server when enabled
if (haveTime) {
Serial.print("Time: ");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime(true);
} else {
Serial.println("Time functions disabled");
}
// Now we have a network we can start the two http handlers for the UI and Stream.
startCameraServer(httpPort, streamPort);
@ -672,8 +705,6 @@ void setup() {
} else {
Serial.printf("\r\nCamera unavailable due to initialisation errors.\r\n\r\n");
}
Serial.print("\r\nThis is the 4.0 alpha\r\n - Face detection has been removed!\r\n");
// Used when dumping status; these are slow functions, so just do them once during startup
sketchSize = ESP.getSketchSize();
@ -682,6 +713,9 @@ void setup() {
// As a final init step chomp out the serial buffer in case we have recieved mis-keys or garbage during startup
while (Serial.available()) Serial.read();
// While in Beta; Warn!
Serial.print("\r\nThis is the 4.0 alpha\r\n - Face detection has been removed!\r\n");
}
void loop() {

View File

@ -113,6 +113,19 @@ struct station stationList[] = {{"ssid1", "pass1", true},
*/
// #define OTA_PASSWORD "SuperVisor"
/* NTP
* Uncomment the following to enable the on-board clock
* Pick a nearby pool server from: https://www.ntppool.org/zone/@
* Set the GMT offset to match your timezone IN SECONDS;
* see https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
* 1hr = 3600 seconds; do the math ;-)
* Default is CET (Central European Time), eg GMT + 1hr
* The DST offset is usually 1 hour (again, in seconds) if used in your country.
*/
//#define NTPSERVER "<EDIT THIS>.pool.ntp.org"
//#define NTP_GMT_OFFSET 3600
//#define NTP_DST_OFFSET 3600
/*
* Camera Defaults
*