Initial Release, first working Version

This commit is contained in:
Christian Mittring
2018-01-05 23:03:28 +01:00
parent 644c0c8484
commit eb83a649bb
5 changed files with 836 additions and 0 deletions

138
Wecker/Alarm.ino Normal file
View File

@@ -0,0 +1,138 @@
void run_alarm(){
if (test_alarm())
{
display_text("ALARM");
delay_gui(1000);
switch_radio(false);
setSleepTimer(30);
}
}
//####################################################################################################
void switch_alarm_mode(){
bool A1 = Clock.checkAlarmEnabled(1);
bool A2 = Clock.checkAlarmEnabled(2);
if (A1 && A2){
Clock.turnOffAlarm(1);
Clock.turnOffAlarm(2);
}
else if (A1 && !A2){
Clock.turnOffAlarm(1);
Clock.turnOnAlarm(2);
}
else if (!A1 && A2){
Clock.turnOnAlarm(1);
Clock.turnOnAlarm(2);
}
else if (!A1 && !A2){
Clock.turnOnAlarm(1);
Clock.turnOffAlarm(2);
}
A1 = Clock.checkAlarmEnabled(1);
A2 = Clock.checkAlarmEnabled(2);
if (A1 && A2)
{
display_text("A1/2");
}
else if (A1)
{
display_text("A1");
}
else if (A2)
{
display_text("A2");
}
else
{
display_text("A Off");
}
delay_gui(1000);
}
void set_alarm_item(){
if (setItem == 0)
{
setItem = 10;
}
setItem = setItem + 1;
if(setItem == 13)
{
set_alarm1();
}
else if (setItem > 14)
{
set_alarm2();
setItem = 0;
}
}
//####################################################################################################
bool test_alarm(){
bool A1 = Clock.checkIfAlarm(1);
bool A2 = Clock.checkIfAlarm(2);
bool A1_en = Clock.checkAlarmEnabled(1);
bool A2_en = Clock.checkAlarmEnabled(2);
return ((A1 && A1_en) || (A2 && A2_en));
}
//########################################################################################################
//Helpers
void add_al1_hour(int i){
al1h = al1h + i;
if (al1h > 23){
al1h = 0;
}
}
void add_al1_minute(int i){
al1m = al1m + i;
if (al1m > 59){
al1m = al1m - 60;
}
}
void add_al2_hour(int i){
al2h = al2h + i;
if (al2h > 23){
al2h = 0;
}
}
void add_al2_minute(int i){
al2m = al2m + i;
if (al2m > 59){
al2m = al2m - 60;
}
}
void set_alarm1(){
Clock.setA1Time(0, al1h, al1m, 0, 1000, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
Serial.println("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(A1Second) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
display_text("A1");
delay(1000);
display_text(String(A1Hour) + ":" +String(A1Minute));
delay(1000);
}
void set_alarm2(){
Clock.setA2Time(0, al2h, al2m, 100, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
Serial.println("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
display_text("A2");
delay(1000);
display_text(String(A1Hour) + ":" +String(A1Minute));
delay_gui(1000);
}

293
Wecker/Uhr.ino Normal file
View File

@@ -0,0 +1,293 @@
void show_current_time(long intervall, long currentMillis){
if(intervall > 16000){
previousMillis = currentMillis;
}
if (intervall > 14000){
display_date(d,M,y);
}
/*else if (intervall > 12000){
display_year(d,M,y);
}
else if (intervall > 10000){
display_text(dow);
}
else if (intervall > 9000){
display_text(readDHT());
}
else if (intervall > 8000){
display_text("Temp");
}*/
else{
display_time(h,m,s);
}
setBrightnes(h);
}
void show_time_infos(long intervall, long currentMillis){
if(setItem == 1 || setItem == 2){
display_date(d,M,y);
}
else if(setItem == 2){
display_date(d,M,y);
}
else if(setItem == 3){
display_year(d,M,y);
}
else if(setItem == 4 || setItem == 5 || setItem == 6){
display_time(h,m,s);
}
else if(setItem == 11 || setItem == 12){
display_time(al1h,al1m,0);
}
else if(setItem == 13 || setItem == 14){
display_time(al2h,al2m,0);
}
}
//####################################################################################################
//Setup
void switch_set_item(){
setItem = setItem + 1;
if(setItem > 6)
{
set_clock();
setItem = 0;
display_text(" OK ");
delay_gui(1000);
}
}
void set_time(){
if(setItem == 1){
add_day(1);
}
else if(setItem == 2){
add_month(1);
}
else if(setItem == 3){
add_year(1);
}
else if(setItem == 4){
add_hour(1);
}
else if(setItem == 5){
add_minute(1);
}
else if(setItem == 6){
add_second(1);
}
else if(setItem == 11){
add_al1_hour(1);
}
else if(setItem == 12){
add_al1_minute(1);
}
else if(setItem == 13){
add_al2_hour(1);
}
else if(setItem == 14){
add_al2_minute(1);
}
}
//###################################################################################################################################
//Time Code
void update_Time(){
DateTime now = RTC.now();
h = now.hour(); //24-hr
m = now.minute();
s = now.second();
d = now.day();
M = now.month();
y = now.year();
if (summertime_EU(y,M,d,h,1))
{
h = h + 1;
if (h == 24){
h = 0;
}
}
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)));
}
//###################################################################################################################################
//Display Code
void display_year(int Day, int Month, int Year){
display_text(String(Year));
}
void display_time(int Stunde, int Minute, int Sekunde){
String Min = String(Minute);
String Stu = String(Stunde);
if (Minute < 10)
{
Min = "0" + Min;
}
if (Stunde < 10)
{
Stu = "0" + Stu;
}
if(setItem == 4)
{
display_text("h :" + Stu);
}
else if(setItem == 5)
{
display_text("m :" + Min);
}
else if(setItem == 6)
{
display_text("s :" + String(Sekunde));
}
else if(setItem == 11)
{
display_text("h1:" + Stu);
}
else if(setItem == 12)
{
display_text("m1:" + Min);
}
else if(setItem == 13)
{
display_text("h2:" + Stu);
}
else if(setItem == 14)
{
display_text("m2:" + Min);
}
else if (Sekunde % 2 == 0){
display_text(Stu + ":" + Min);
}
else{
display_text(Stu + "." + Min);
}
}
void display_date(int Day, int Month, int Year){
String Mon = String(Month);
String Da = String(Day);
if (Day < 10)
{
Da = "0" + Da;
}
if (Month < 10)
{
Mon = "0" + Mon;
}
if(setItem == 1)
{
display_text("T :" + Da);
}
else if(setItem == 2)
{
display_text("M :" + Mon);
}
else{
display_text(Da + "." + Mon);
}
}
void display_text(String text){
Serial.println("Display:");
Serial.println(text);
ledMatrix.clear();
ledMatrix.setText(text);
ledMatrix.drawText();
ledMatrix.commit();
}
void setBrightnes(int h){
if (h > 20 || h < 7){
ledMatrix.setIntensity(0);
}
else{
ledMatrix.setIntensity(4);
}
}
//########################################################################################################
//Helpers
void add_second(int i){
s = s + i;
if (s > 59)
{
s = s - 60;
}
}
void add_hour(int i){
h = h + i;
if (h > 23){
h = 0;
}
}
void add_minute(int i){
m = m + i;
if (m > 59){
m = m - 60;
}
}
void add_month(int i){
M = M + i;
if (m > 12){
m = 1;
}
}
void add_day(int i){
d = d + i;
if (d > 31){
d = 1;
}
}
void add_year(int i){
y = y + i;
if (y > 2030){
y = 2017;
}
}
void set_clock(){
Clock.setClockMode(false); // set to 24h
//setClockMode(true); // set to 12h
Clock.setYear(y-2000);
Clock.setMonth(M);
Clock.setDate(d);
//Clock.setDoW(DoW);
if (summertime_EU(y,M,d,h,1))
{
Clock.setHour(h-1);
}
else{
Clock.setHour(h);
}
Clock.setMinute(m);
Clock.setSecond(s);
}
boolean summertime_EU(int year, byte month, byte day, byte hour, byte tzHours)
// European Daylight Savings Time calculation by "jurs" for German Arduino Forum
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
// return value: returns true during Daylight Saving Time, false otherwise
{
if (month<3 || month>10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if (month>3 && month<10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if (month==3 && (hour + 24 * day)>=(1 + tzHours + 24*(31 - (5 * year /4 + 4) % 7)) || month==10 && (hour + 24 * day)<(1 + tzHours + 24*(31 - (5 * year /4 + 1) % 7)))
return true;
else
return false;
}

198
Wecker/Wecker.ino Normal file
View File

@@ -0,0 +1,198 @@
// Including the ESP8266 WiFi library
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiManager.h>
#include <math.h>
#include <SPI.h>
#include "LedMatrix.h"
#include <DS3231.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
RTClib RTC;
DS3231 Clock;
//Display
#define NUMBER_OF_DEVICES 4
#define CS_PIN 10
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
const String NodeName = "Wecker";
//Pin Taster
const int pinTime = 3; //Pin fuer Toggle_Power und Einstellen der Uhrzeit (Lang)
const int pinSet = 0; //Pin fuer Sleep und Zeit_Einstellung im Alarm/Time Set Mode
const int pinAlarm = 2; //Pin fuer Alarm An/Aus und setzen der Alarmzeiten (Lang)
const int pinInput = 1; //Pin für die Eingangswahl
const int pinSleep = 9; //Pin Unbenutzt
//Pin Relay
const int outInput = 12;
const int outPower = 16;
int al1h = 0;
int al1m = 0;
int al2h = 0;
int al2m = 0;
long previousMillis = 0;
void setup() {
// put your setup code here, to run once:
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
Wire.begin();
//Setup Display
ledMatrix.init();
ledMatrix.setTextAlignment(0);
ledMatrix.setRotation(true);
ledMatrix.setIntensity(0);
display_text("U");
delay(400);
display_text("Uh");
delay(400);
display_text("Uhr");
delay(400);
display_text("Uhr");
//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());
delay(1000);
//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) + ", ");
delay(1000);
//Pin Setup
pinMode(pinTime, INPUT); // set pin to input
digitalWrite(pinTime, HIGH); // turn on pullup resistors
pinMode(pinSet, INPUT); // set pin to input
digitalWrite(pinSet, HIGH); // turn on pullup resistors
pinMode(pinAlarm, INPUT); // set pin to input
digitalWrite(pinAlarm, HIGH); // turn on pullup resistors
pinMode(pinInput, INPUT); // set pin to input
digitalWrite(pinInput, HIGH); // turn on pullup resistors
pinMode(pinSleep, INPUT); // set pin to input
digitalWrite(pinSleep, HIGH); // turn on pullup resistors
pinMode(outPower, OUTPUT); // set pin to input
digitalWrite(outPower, HIGH); // turn on pullup resistors
pinMode(outInput, OUTPUT); // set pin to input
digitalWrite(outInput, HIGH); // turn on pullup resistors
previousMillis = millis();
//-------------------------------------------------
//OTA
// No authentication by default
ArduinoOTA.setPassword((const char *)"ota-password");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
//-------------------------------------------------
}
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();
//OTA
unsigned long currentMillis = millis();
long intervall = currentMillis - previousMillis;
release_gui(currentMillis);
// put your main code here, to run repeatedly:
if(setItem == 0){
update_Time();
run_alarm();
if (block_gui < 0)
show_current_time(intervall,currentMillis);
}
else{
show_time_infos(intervall,currentMillis);
previousMillis = currentMillis;
}
test_pin();
handleSleep(intervall, currentMillis);
delay(80);
}
void delay_gui(int millis_s){
block_gui = millis() + millis_s;
}
void release_gui(int currentMillis){
if (block_gui < currentMillis)
block_gui = -1;
else
return;
}

98
Wecker/pinInput.ino Normal file
View File

@@ -0,0 +1,98 @@
void test_pin(){
handle_Time_switch();
handle_Set_switch();
handle_Alarm_switch();
handle_Input_switch();
}
void handle_Alarm_switch(){
int state = digitalRead(pinAlarm);
if(state == LOW && setItem == 0) {
int presstime = get_pin_delay(pinAlarm);
if (presstime > 10){
set_alarm_item();
delay(200);
wait_to_release(pinAlarm);
}
else{
switch_alarm_mode();
delay(200);
}
}
else if(state == LOW){
set_alarm_item();
delay(200);
}
}
void handle_Set_switch(){
int state = digitalRead(pinSet);
if (state == LOW && setItem > 0){
set_time();
delay(200);
}
else if (state == LOW && setItem == 0){
round_sleep();
delay(200);
}
}
void handle_Input_switch(){
int state = digitalRead(pinInput);
/*if (Set == LOW && setItem > 0){
set_time();
delay(200);
}
else */
if (state == LOW){
switch_input();
delay(200);
}
wait_to_release(pinInput);
}
void handle_Time_switch(){
int state = digitalRead(pinTime);
if(state == LOW && setItem == 0) {
int presstime = get_pin_delay(pinTime);
if (presstime > 10){
switch_set_item();
delay(200);
wait_to_release(pinTime);
}
else{
toggle_power();
}
}
else if(state == LOW){
switch_set_item();
delay(200);
}
}
int get_pin_delay(int pin){
int state = digitalRead(pin);
int delay_count = 0;
while(state == LOW && delay_count < 11)
{
state = digitalRead(pin);
delay(100);
delay_count = delay_count +1;
}
return delay_count;
}
void wait_to_release(int pin){
int state = digitalRead(pin);
while(state == LOW)
{
state = digitalRead(pin);
delay(100);
}
}

109
Wecker/radio.ino Normal file
View File

@@ -0,0 +1,109 @@
void round_sleep(){
if (minutes_sleep > 60){
minutes_sleep = 60;
}
else if (minutes_sleep > 45){
minutes_sleep = 45;
}
else if (minutes_sleep > 30){
minutes_sleep = 30;
}
else if (minutes_sleep > 15){
minutes_sleep = 15;
}
else if (minutes_sleep > 0){
minutes_sleep = 0;
}
else{
minutes_sleep = 90;
}
setSleepTimer(minutes_sleep);
}
void setSleepTimer(int Minutes){
if (Minutes == 0)
{
time_on = -1;
minutes_sleep = -1;
}
else{
unsigned long currentMillis = millis();
time_on = currentMillis + ((Minutes *60) *1000);
powerOn();
}
display_text("S " + String(Minutes));
delay_gui(1000);
}
void handleSleep(long intervall, long currentMillis){
if(time_on < 0){
return;
}
else if (currentMillis > time_on)
{
powerOff();
time_on = -1;
minutes_sleep = -1;
}
}
void switch_input(){
int currentIn = digitalRead(outInput);
if(currentIn == LOW){
switch_in(true);
}
else{
switch_radio(true);
}
}
void switch_radio(bool display_switch){
digitalWrite(outInput, LOW);
if(display_switch){
display_text("Radio");
delay_gui(500);
}
}
void switch_in(bool display_switch){
digitalWrite(outInput, HIGH);
if(display_switch){
display_text("In");
delay_gui(500);
}
}
void toggle_power(){
int currentState = digitalRead(outPower);
if (currentState == LOW){
powerOff();
}
else{
powerOn();
}
}
void powerOff(){
time_on = -1;
int currentState = digitalRead(outPower);
if (currentState != HIGH){
Serial.println("OFF");
digitalWrite(outPower, HIGH);
display_text("OFF");
delay_gui(1000);
}
switch_in(false);
}
void powerOn(){
int currentState = digitalRead(outPower);
if (currentState != LOW){
Serial.println("ON");
digitalWrite(outPower, LOW);
display_text("ON");
delay_gui(1000);
}
}