[Fixed]: Alarm Time wrong after summer/winter time change

[Added]: EEPROM writing summer/winter alarm time saved
[Added]: WiFi support
[Added]: Time setting over NTP every hour
This commit is contained in:
Christian Mittring
2018-03-31 17:37:39 +02:00
parent e6144d978a
commit dcac376b7e
7 changed files with 211 additions and 76 deletions

View File

@@ -10,6 +10,16 @@ void run_alarm(long currentMillis){
run_wake_up_beep(currentMillis);
}
}
//Test if Summer->Winter time change (this should also change the Alarm times!)
bool dst = summertime_EU(y,M,d,h,1);
//Serial.println(String(dst) + ":" + String(summer));
if (dst != summer)
{
summer = dst;
set_alarm1();
set_alarm2();
write_eeprom();
}
}
void stop_alarm(){
@@ -165,7 +175,15 @@ void add_al2_minute(int i){
void set_alarm1(){
Clock.setA1Time(0, al1h, al1m, 0, 1000, true, false, false);
int al1h_utc = al1h;
if (summertime_EU(y,M,d,h,1))
{
al1h_utc = al1h - 1;
if (al1h_utc < 0){
al1h_utc = 23;
}
}
Clock.setA1Time(0, al1h_utc, al1m, 0, 1000, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
@@ -177,7 +195,15 @@ void set_alarm1(){
void set_alarm2(){
Clock.setA2Time(0, al2h, al2m, 0b1000000, true, false, false);
int al2h_utc = al2h;
if (summertime_EU(y,M,d,h,1))
{
al2h_utc = al2h - 1;
if (al2h_utc < 0){
al2h_utc = 23;
}
}
Clock.setA2Time(0, al2h_utc, al2m, 0b1000000, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, AlarmBits;
bool A1Dy, A1h12, A1PM;

View File

@@ -2,15 +2,20 @@
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiManager.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <math.h>
#include <SPI.h>
#include <DS3231.h>
#include <WiFiUdp.h>
//#include <ArduinoOTA.h>
#include <ArduinoOTA.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_I2C.h> //https://github.com/agnunez/ESP8266-I2C-LCD1602
#include <NTPtimeESP.h> //https://github.com/SensorsIot/NTPtimeESP
#include <EEPROM.h>
//LED Band Setup
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include "FastLED.h"
#define NUM_LEDS 38
@@ -21,17 +26,16 @@
// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];
//RealTimeClock
RTClib RTC;
DS3231 Clock;
//NTP Setup
NTPtime NTPch("ch.pool.ntp.org"); // Choose server pool as required
//Display
//#define NUMBER_OF_DEVICES 4
//#define CS_PIN 10
//LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
// Create new LCD03 instance
LiquidCrystal_I2C lcd(0x3f, 16, 2);
//ConfigData (WiFi Name and Pin-Numbers)
const String NodeName = "LichtWecker";
//Pin Taster
const int pinTime = 3; //Pin fuer Toggle_Power und Einstellen der Uhrzeit (Lang)
@@ -41,20 +45,49 @@ const int pinAlarm = 2; //Pin fuer Alarm An/Aus und setzen der Alarmzeiten (Lang
//Pin Relay
const int outBeep = 9;
//To know if connected to wifi or not:
bool wifi_connected = false;
//For Summertime change notification (change alarmtime) this should be saved in the EEPROM!
bool summer = false;
//Alarm Started?
bool alarm_started = false;
long startTime = 0;
bool lamp_on = false;
//Alarm 1 Time
int al1h = 0;
int al1m = 0;
//Alarm 2 Time
int al2h = 0;
int al2m = 0;
//Alarm run Counter
long time_on = -1;
int minutes_sleep = -1;
//Current Date and Time from RTC
int h = 0;
int m = 0;
int s = 0;
int d = 0;
int M = 0;
int y = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String dow = "-";
int setItem = 0;
long block_gui = -1;
long previousMillis = 0;
void configModeCallback (WiFiManager *myWiFiManager) {
//if you used auto generated SSID, print it
//Serial.println(myWiFiManager->getConfigPortalSSID());
display_text("Wifi: Licht","PW: Wecker");
}
void setup() {
// put your setup code here, to run once:
// Initializing serial port for debugging purposes
@@ -62,10 +95,6 @@ void setup() {
delay(10);
//Setup Display
//ledMatrix.init();
//ledMatrix.setTextAlignment(0);
//ledMatrix.setRotation(true);
//ledMatrix.setIntensity(0);
// Initialise the LCD
lcd.begin(4,5); // sda=0, scl=2
// Turn on the backlight
@@ -95,42 +124,10 @@ void setup() {
delay(200);
display_text("Licht Wecke","");
delay(200);
display_text("Licht Wecker","");
display_text("Licht Wecker","Connecting...");
delay(200);
WiFi.hostname(NodeName);
WiFi.mode(WIFI_OFF);
//WiFiManager intialisation. Once completed there is no need to repeat the process on the current board
//WiFiManager wifiManager;
//wifiManager.autoConnect();
//Serial.println("WiFi connected");
//Serial.print("IP address: ");
//Serial.println(WiFi.localIP());
display_text("Licht Wecker","SW: 0.01");
delay(1000);
display_text("Licht Wecker","HW: 0.01");
delay(1000);
display_text("Licht Wecker","");
//Set Alarm Times
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
al1h = A1Hour;
al1m = A1Minute;
Serial.println("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
al2h = A1Hour;
al2m = A1Minute;
Serial.println("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
show_green();
delay(1000);
//Pin Setup
pinMode(pinTime, INPUT); // set pin to input
digitalWrite(pinTime, HIGH); // turn on pullup resistors
@@ -144,6 +141,75 @@ void setup() {
pinMode(outBeep, OUTPUT); // set pin to input
digitalWrite(outBeep, LOW); // turn on pullup resistors
//restore data
get_eeprom();
WiFi.hostname(NodeName);
//WiFiManager intialisation. Once completed there is no need to repeat the process on the current board
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(120);
//reset saved settings
if(digitalRead(pinSet) == LOW && digitalRead(pinTime) == LOW){
display_text("WiFi","Reset...");
wifiManager.resetSettings();
delay(1000);
ESP.reset();
delay(5000);
}
if (wifiManager.autoConnect("Licht", "Wecker")){
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
wifi_connected = true;
display_text("IP address",String(WiFi.localIP()));
delay(5000);
}
else{
WiFi.mode(WIFI_OFF);
}
display_text("Licht Wecker","SW: 0.03");
delay(1000);
display_text("Licht Wecker","HW: 0.02");
delay(1000);
display_text("Licht Wecker","");
//Set Alarm Times
DateTime now = RTC.now();
d = now.day();
M = now.month();
y = now.year();
h = now.hour(); //24-hr
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
if (summer)
{
al1h = A1Hour + 1;
}
else{
al1h = A1Hour;
}
al1m = A1Minute;
Serial.println("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
if (summer)
{
al2h = A1Hour + 1;
}
else{
al2h = A1Hour;
}
al2m = A1Minute;
Serial.println("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
show_green();
delay(1000);
show_black();
previousMillis = millis();
@@ -153,7 +219,7 @@ void setup() {
//-------------------------------------------------
//OTA
// No authentication by default
/*
ArduinoOTA.setPassword(NodeName.c_str());
ArduinoOTA.setHostname(NodeName.c_str());
@@ -176,31 +242,13 @@ void setup() {
});
ArduinoOTA.begin();
//-------------------------------------------------
*/
update_time_from_web();
}
long time_on = -1;
int minutes_sleep = -1;
int h = 0;
int m = 0;
int s = 0;
int d = 0;
int M = 0;
int y = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String dow = "-";
int setItem = 0;
long block_gui = -1;
void loop() {
//OTA
//ArduinoOTA.handle();
ArduinoOTA.handle();
//OTA
unsigned long currentMillis = millis();
@@ -221,6 +269,10 @@ void loop() {
}
test_pin();
handleSleep(intervall, currentMillis);
if(m == 1)
{
update_time_from_web();
}
delay(100);
}

View File

@@ -111,7 +111,7 @@ void update_Time(){
}
dow = daysOfTheWeek[Clock.getDoW()];
Serial.println(String(h) + ":" + String(m) + ":" + String(s) + " - " + String(d) + "." + String(M) + "." + String(y) + " | DST: " + String(summertime_EU(y,M,d,h,1)));
//Serial.println(String(h) + ":" + String(m) + ":" + String(s) + " - " + String(d) + "." + String(M) + "." + String(y) + " | DST: " + String(summertime_EU(y,M,d,h,1)));
}
//###################################################################################################################################

View File

@@ -3,14 +3,14 @@ String current_string = "";
void display_text(String text1, String text2){
if (current_string != text1 + text2){
Serial.println("Display:");
//Serial.println("Display:");
lcd.clear();
lcd.setCursor(freespace(text1), 0);
lcd.print(text1);
Serial.println(text1);
//Serial.println(text1);
lcd.setCursor(freespace(text2), 1);
lcd.print(text2);
Serial.println(text2);
//Serial.println(text2);
//ledMatrix.clear();
//ledMatrix.setText(text);
//ledMatrix.drawText();

25
LichtWecker/eeprom.ino Normal file
View File

@@ -0,0 +1,25 @@
uint addr = 0;
struct {
bool summertime = false;
} data;
void write_eeprom(){
EEPROM.begin(512);
// load EEPROM data into RAM, see it
data.summertime = summer;
Serial.println("Writing to EEPROM: "+String(data.summertime));
// replace values in EEPROM
EEPROM.put(addr,data);
EEPROM.commit();
EEPROM.end();
}
bool get_eeprom(){
EEPROM.begin(512);
EEPROM.get(addr,data);
Serial.println("Found: "+String(data.summertime));
EEPROM.end();
summer = data.summertime;
}

View File

@@ -19,7 +19,7 @@ void show_black(){
leds[i] = CRGB::Black;
}
FastLED.show();
Serial.println("Light: BLACK");
//Serial.println("Light: BLACK");
}
//Show LED Colors

32
LichtWecker/ntp.ino Normal file
View File

@@ -0,0 +1,32 @@
void update_time_from_web(){
//Serial.println(wifi_connected);
if (wifi_connected == true)
{
strDateTime dateTime;
// first parameter: Time zone in floating point (for India); second parameter: 1 for European summer time; 2 for US daylight saving time; 0 for no DST adjustment; (contributed by viewwer, not tested by me)
dateTime = NTPch.getNTPtime(1.0, 1);
// check dateTime.valid before using the returned time
// Use "setSendInterval" or "setRecvTimeout" if required
if(dateTime.valid){
NTPch.printDateTime(dateTime);
byte actualHour = dateTime.hour;
byte actualMinute = dateTime.minute;
byte actualsecond = dateTime.second;
int actualyear = dateTime.year;
byte actualMonth = dateTime.month;
byte actualday =dateTime.day;
byte actualdayofWeek = dateTime.dayofWeek;
if (actualMinute != m){
h = actualHour;
m = actualMinute;
s = actualsecond;
y = actualyear;
M = actualMonth;
d = actualday;
set_clock();
Serial.println("Time Updated!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}
}