First Release Version 0.01

This commit is contained in:
Christian Mittring
2018-03-02 13:25:47 +01:00
parent d1cbf407d6
commit e6144d978a
7 changed files with 993 additions and 0 deletions

188
LichtWecker/Alarm.ino Normal file
View File

@@ -0,0 +1,188 @@
void run_alarm(long currentMillis){
if (test_alarm())
{
Serial.println("Start Alarm");
start_wake_light(currentMillis);
setAlarmTimer(15);
}
else{
if (run_wake_up(currentMillis)){
run_wake_up_beep(currentMillis);
}
}
}
void stop_alarm(){
Serial.println("Stop Alarm");
stop_wake_light();
stop_alarm_beep();
setAlarmTimer(0);
}
void setAlarmTimer(int Minutes){
if (Minutes == 0)
{
time_on = -1;
minutes_sleep = -1;
}
else{
unsigned long currentMillis = millis();
time_on = currentMillis + ((Minutes *60) *1000);
}
}
void handleSleep(long intervall, long currentMillis){
if(time_on < 0){
return;
}
else if (currentMillis > time_on)
{
stop_alarm();
}
}
//####################################################################################################
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("Alarm 1 an", "Alarm 2 an");
}
else if (A1)
{
display_text("Alarm 1 an", "Alarm 2 aus");
}
else if (A2)
{
display_text("Alarm 1 aus", "Alarm 2 an");
}
else
{
display_text("Alarm 1 aus", "Alarm 2 aus");
}
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;
}
}
String get_alarm_string(){
String alarm_string_1 = "";
String alarm_string_2 = "";
bool A1_en = Clock.checkAlarmEnabled(1);
bool A2_en = Clock.checkAlarmEnabled(2);
if (A1_en == true){
if (al1m < 10)
alarm_string_1 = "A1:" + String(al1h) + ":0" + String(al1m);
else
alarm_string_1 = "A1:" + String(al1h) + ":" + String(al1m);
}
if (A2_en == true){
if (al2m < 10)
alarm_string_2 = "A2:" + String(al2h) + ":0" + String(al2m);
else
alarm_string_2 = "A2:" + String(al2h) + ":" + String(al2m);
}
return alarm_string_1 + " " + alarm_string_2;
}
//####################################################################################################
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("Alarm 1", String(A1Hour) + ":" +String(A1Minute));
//delay_gui(1000);
}
void set_alarm2(){
Clock.setA2Time(0, al2h, al2m, 0b1000000, 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("Alarm 2", String(A1Hour) + ":" +String(A1Minute));
//delay_gui(1000);
}

226
LichtWecker/LichtWecker.ino Normal file
View File

@@ -0,0 +1,226 @@
// Including the ESP8266 WiFi library
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiManager.h>
#include <math.h>
#include <SPI.h>
#include <DS3231.h>
#include <WiFiUdp.h>
//#include <ArduinoOTA.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include "FastLED.h"
#define NUM_LEDS 38
// Data pin that led data will be written out over
#define DATA_PIN 14
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define CLOCK_PIN 13
// This is an array of leds. One item for each led in your strip.
CRGB leds[NUM_LEDS];
RTClib RTC;
DS3231 Clock;
//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);
const String NodeName = "LichtWecker";
//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)
//Pin Relay
const int outBeep = 9;
//Alarm Started?
bool alarm_started = false;
long startTime = 0;
bool lamp_on = false;
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);
//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
lcd.backlight();
Wire.begin();
display_text("Not for Sale","Prototyp by CHM");
delay(2000);
display_text("L","");
delay(200);
display_text("Li","");
delay(200);
display_text("Lic","");
delay(200);
display_text("Lich","");
delay(200);
display_text("Licht","");
delay(200);
display_text("Licht W","");
delay(200);
display_text("Licht We","");
delay(200);
display_text("Licht Wec","");
delay(200);
display_text("Licht Weck","");
delay(200);
display_text("Licht Wecke","");
delay(200);
display_text("Licht Wecker","");
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
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(outBeep, OUTPUT); // set pin to input
digitalWrite(outBeep, LOW); // turn on pullup resistors
show_black();
previousMillis = millis();
//-------------------------------------------------
//OTA
// No authentication by default
/*
ArduinoOTA.setPassword(NodeName.c_str());
ArduinoOTA.setHostname(NodeName.c_str());
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(currentMillis);
if (block_gui < 0)
show_current_time(intervall,currentMillis);
}
else{
show_time_infos(intervall,currentMillis);
previousMillis = currentMillis;
}
test_pin();
handleSleep(intervall, currentMillis);
delay(100);
}

272
LichtWecker/Uhr.ino Normal file
View File

@@ -0,0 +1,272 @@
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);
}
}
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", "Zeit gespeichert");
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("Jahr", 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("Stunde:", Stu);
}
else if(setItem == 5)
{
display_text("Minute:", Min);
}
else if(setItem == 6)
{
display_text("Sekunde:", String(Sekunde));
}
else if(setItem == 11)
{
display_text("Alarm 1", "Stunde: "+ Stu);
}
else if(setItem == 12)
{
display_text("Alarm 1", "Minute: " + Min);
}
else if(setItem == 13)
{
display_text("Alarm 2", "Stunde: " + Stu);
}
else if(setItem == 14)
{
display_text("Alarm 2", "Minute: " + Min);
}
else if (Sekunde % 2 == 0){
display_text(Stu + ":" + Min, get_alarm_string());
}
else{
display_text(Stu + "." + Min, get_alarm_string());
}
}
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("Tag:",Da);
}
else if(setItem == 2)
{
display_text("Monat:",Mon);
}
else{
display_text(Da + "." + Mon + "." + Year, get_alarm_string());
}
}
//########################################################################################################
//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;
}

43
LichtWecker/beeper.ino Normal file
View File

@@ -0,0 +1,43 @@
void run_wake_up_beep(long currentMillis){
if (alarm_started == true)
{
long delta = (currentMillis - startTime)/1000;
//if (delta % 2 == 0){
// beepOff();
//}
//else{
beepOn();
//}
}
}
void stop_alarm_beep(){
beepOff();
}
void toggle_alarm_beep(){
int currentState = digitalRead(outBeep);
if (currentState == HIGH){
beepOff();
}
else{
beepOn();
}
}
void beepOff(){
int currentState = digitalRead(outBeep);
if (currentState != LOW){
Serial.println("ALARM");
digitalWrite(outBeep, LOW);
}
}
void beepOn(){
int currentState = digitalRead(outBeep);
if (currentState != HIGH){
Serial.println("ALARM");
digitalWrite(outBeep, HIGH);
}
}

65
LichtWecker/display.ino Normal file
View File

@@ -0,0 +1,65 @@
String current_string = "";
void display_text(String text1, String text2){
if (current_string != text1 + text2){
Serial.println("Display:");
lcd.clear();
lcd.setCursor(freespace(text1), 0);
lcd.print(text1);
Serial.println(text1);
lcd.setCursor(freespace(text2), 1);
lcd.print(text2);
Serial.println(text2);
//ledMatrix.clear();
//ledMatrix.setText(text);
//ledMatrix.drawText();
//ledMatrix.commit();
current_string = text1 + text2;
}
}
int freespace(String text){
if (text.length() > 14)
{
return 1;
}
else{
int freespace = 16 - text.length();
freespace = freespace / 2;
return freespace;
}
}
bool light_on = true;
void toggle_bg_light(){
if (light_on == true){
setBrightnes(false);
}
else{
setBrightnes(true);
}
}
void setBrightnes(bool on_off){
if (on_off == true){
lcd.backlight();
light_on = true;
}
else{
lcd.noBacklight();
light_on = false;
}
}
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;
}

110
LichtWecker/light.ino Normal file
View File

@@ -0,0 +1,110 @@
//Show LED Colors
void show_white(){
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RBG>(leds, NUM_LEDS);
for(int i=0; i < NUM_LEDS; i = i+1 )
{
leds[i] = CRGB::White;
}
FastLED.show();
Serial.println("Light: WHITE");
}
//Show LED Colors
void show_black(){
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RBG>(leds, NUM_LEDS);
for(int i=0; i < NUM_LEDS; i = i+1 )
{
leds[i] = CRGB::Black;
}
FastLED.show();
Serial.println("Light: BLACK");
}
//Show LED Colors
void show_green(){
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RBG>(leds, NUM_LEDS);
for(int i=0; i < NUM_LEDS; i = i+1 )
{
leds[i] = CRGB::Green;
}
FastLED.show();
Serial.println("Light: GREEN");
}
void set_color(int r, int g, int b){
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RBG>(leds, NUM_LEDS);
for(int i=0; i < NUM_LEDS; i = i+1 )
{
leds[i].setRGB( r, g, b);
}
FastLED.show();
Serial.println("Light: ");
Serial.println(r);
Serial.println(b);
Serial.println(g);
}
bool run_wake_up(long currentMillis){
if (alarm_started == true)
{
long delta = (currentMillis - startTime)/1000;
if (delta < 220){
set_color(30 + int(delta), int(delta)/4, 0);
}
else if(delta < 405){
set_color(255, int(delta - 220)+55, int((delta - 220)/4));
}
else if(delta < 605){
set_color(255, 255, int((delta - 405))+55);
}
else{
if (delta % 2 == 0){
show_white();
}
else{
show_black();
}
return true;
}
}
else if (lamp_on == true){
}
else{
show_black();
}
return false;
}
void switch_lamp(){
if(lamp_on == false){
lamp_on = true;
show_white();
}
else{
lamp_on = false;
show_black();
}
}
void start_wake_light(long currentMillis){
if (alarm_started == false)
{
alarm_started = true;
startTime = currentMillis;
set_color(30, 0, 0);
}
}
void stop_wake_light(){
if (alarm_started == true)
{
alarm_started = false;
startTime = 0;
show_black();
}
}

89
LichtWecker/pinInput.ino Normal file
View File

@@ -0,0 +1,89 @@
void test_pin(){
handle_Time_switch();
handle_Set_switch();
handle_Alarm_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{
if (alarm_started == true)
{
stop_alarm();
}
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){
switch_lamp();
delay(200);
}
}
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();
toggle_bg_light();
}
}
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);
}
}