Skip to content

Commit

Permalink
Change to core NTP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 6, 2020
1 parent 6c09978 commit 94a4c27
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 139 deletions.
6 changes: 4 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
[common_env_data]
upload_speed = 460800
monitor_speed = 74880
monitor_rts = 0
monitor_dtr = 1
framework = arduino
platform = espressif8266
build_unflags =
Expand All @@ -34,8 +36,6 @@ lib_deps =
DS18B20
ArduinoLog
DoubleResetDetect
Time
NtpClientLib
CircularBuffer
LCBUrl
https://github.com/lbussy/AsyncWiFiManager.git
Expand All @@ -46,6 +46,8 @@ build_type = release
[env:d1_mini]
upload_speed = ${common_env_data.upload_speed}
monitor_speed = ${common_env_data.monitor_speed}
monitor_rts = ${common_env_data.monitor_rts}
monitor_dtr = ${common_env_data.monitor_dtr}
framework = ${common_env_data.framework}
platform = ${common_env_data.platform}
; build_unflags = ${common_env_data.build_unflags}
Expand Down
8 changes: 2 additions & 6 deletions src/bubbles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,12 @@ void Bubbles::start() {
single->lastVes = 0.0;

// Set starting time
NtpHandler *ntpTime = NtpHandler::getInstance();
ntpTime->update();
single->lastTime = ntpTime->Time;
strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime()));
}

void Bubbles::update() { // Regular update loop, once per minute
// Handle NTP Time
NtpHandler *ntpTime = NtpHandler::getInstance();
ntpTime->update();
single->lastTime = ntpTime->Time;
strlcpy(single->lastTime, getJsonTime(),sizeof(getJsonTime()));

// Store last values
single->lastBpm = single->getRawBpm();
Expand Down
4 changes: 2 additions & 2 deletions src/bubbles.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SOFTWARE. */

#include "config.h"
#include "jsonconfig.h"
#include "ntphandler.h"
#include "ntp.h"
#include "DS18B20.h"
#include "OneWire.h"
#include <ArduinoLog.h>
Expand Down Expand Up @@ -59,7 +59,7 @@ class Bubbles {
// Other Declarations
void handleInterrupts(void);
void update(); // Call every 60 seconds
char* lastTime;
char lastTime[22];
float getAvgAmbient();
float getAvgVessel();
float getAvgBpm();
Expand Down
7 changes: 3 additions & 4 deletions src/bubserial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ void serial() { // Start serial with auto-detected rate (default to BAUD)
}

void printTimestamp(Print* _logOutput) {
NtpHandler *ntpTime = NtpHandler::getInstance();
ntpTime->update();
char locTime[prefLen] = {'\0'};
strlcpy(locTime, ntpTime->Time, sizeof(locTime));
char locTime[22] = {'\0'};
strlcpy(locTime, getDTS().c_str(), getDTS().length());
_logOutput->print(locTime);
}


#else // DISABLE_LOGGING

void serial(){}
Expand Down
2 changes: 1 addition & 1 deletion src/bubserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SOFTWARE. */

#include "config.h"
#include "tools.h"
#include "ntphandler.h"
#include "ntp.h"
#include <ArduinoLog.h>

void serial();
Expand Down
12 changes: 5 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ void setup() {
doWiFi();
}

NtpHandler *ntpTime = NtpHandler::getInstance();
ntpTime->start();

initWebServer(); // Turn on web server
mdnssetup(); // Set up mDNS responder
execspiffs(); // Check for pending SPIFFS update
loadBpm() ; // Get last BPM reading if it was a controlled reboot
setClock(); // Set NTP Time
initWebServer(); // Turn on web server
mdnssetup(); // Set up mDNS responder
execspiffs(); // Check for pending SPIFFS update
loadBpm() ; // Get last BPM reading if it was a controlled reboot

Log.notice(F("Started %s version %s (%s) [%s]." CR), API_KEY, version(), branch(), build());
}
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SOFTWARE. */
#include "pushhelper.h"
#include "bubbles.h"
#include "mdns.h"
#include "ntp.h"
#include <DoubleResetDetect.h>
#include <ArduinoLog.h>
#include <Arduino.h>
Expand Down
152 changes: 152 additions & 0 deletions src/ntp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)

This file is part of Lee Bussy's Brew Bubbbles (brew-bubbles).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#include "ntp.h"

// Check:
// https://github.com/dalmatianrex/articles/tree/master/makerpro-esp8266-ntp
// for additional functionality

void setClock() {
Ticker blinker;
Log.notice(F("Entering blocking loop to get NTP time."));
blinker.attach_ms(NTPBLINK, ntpBlinker);
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
#ifdef LOG_LEVEL
Serial.print(F("."));
#endif
_delay(500);
yield();
nowSecs = time(nullptr);
}
blinker.detach();
#ifdef LOG_LEVEL
Serial.println();
#endif
Log.notice(F("NTP time set."));
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
}

String getDTS() {
// Returns JSON-type string = 2019-12-20T13:59:39Z
time_t now;
time_t rawtime = time(&now);
struct tm ts;
ts = *localtime(&rawtime);
char dta[21] = {'\0'};
strftime(dta, sizeof(dta), "%FT%TZ", &ts);
String dts = String(dta);
return dts;
}

char * getJsonTime() {
char * dts = "";
sprintf(dts, "%04u-%02u-%02uT%02u:%02u:%02uZ", getYear(), getMonth(), getDate(), getHour(), getMinute(), getSecond());
return dts;
}

int getYear() {
// tm_year = years since 1900
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int year = 1900 + ts->tm_year;
return year;
}

int getMonth() {
// tm_mon = months since January (0-11)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int month = ts->tm_mon;
return month;
}

int getDate() {
// tm_mday = day of the month (1-31)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int day = ts->tm_mday;
return day;
}

int getWday() {
// tm_wday = days since Sunday (0-6)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int wday = 1 + ts->tm_wday;
return wday;
}

int getHour() {
// tm_hour = hours since midnight (0-23)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int hour = ts->tm_hour;
return hour;
}

int getMinute() {
// tm_min = minutes after the hour (0-59)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int min = ts->tm_min;
return min;
}

int getSecond() {
// tm_sec = seconds after the minute (0-60)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int sec = ts->tm_sec;
return sec;
}

int getYDay() {
// tm_yday = days since January 1 (0-365)
time_t rawtime;
struct tm * ts;
time ( &rawtime );
ts = gmtime ( &rawtime );
int yday = ts->tm_yday;
return yday;
}

void ntpBlinker() {
digitalWrite(LED, !(digitalRead(LED))); // Invert Current State of LED
}
51 changes: 21 additions & 30 deletions src/ntphandler.h → src/ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,31 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

#ifndef _NTPHANDLER_H
#define _NTPHANDLER_H
#ifndef _NTP_H
#define _NTP_H

#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif defined ESP32
#include <WiFi.h>
#endif

#include "config.h"
#include "tools.h"
// #include <TimeLib.h>
#include <NtpClientLib.h>
#include <ArduinoLog.h>
#include <Ticker.h>
#include <ArduinoLog.h>

#define NTP_TIMEOUT 1500

void setClock();
String getDTS();
char * getJsonTime();
int getYear(); // tm_year
int getMonth(); // tm_mon
int getDate(); // tm_mday
int getWday(); // tm_wday
int getHour(); // tm_hour
int getMinute(); // tm_min
int getSecond(); // tm_sec
int getYDay(); // tm_yday
void ntpBlinker();

class NtpHandler {
private:
// Singleton Declarations
NtpHandler() {}
static NtpHandler *single;
// Other Declarations
bool syncEventTriggered;
NTPSyncEvent_t ntpEvent;
bool hasBeenSet;
void setup();
void setJsonTime();

public:
// Singleton Declarations
static NtpHandler* getInstance();
~NtpHandler() {single = NULL;}
// Other Declarations
void start();
void update();
char Time[21]; // Hold the Time string
};

#endif // _NTPHANDLER_H
#endif // _NTP_H
Loading

0 comments on commit 94a4c27

Please sign in to comment.