-
Notifications
You must be signed in to change notification settings - Fork 0
/
clocksilencer.ino
191 lines (169 loc) · 4.83 KB
/
clocksilencer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <DFPlayerMini_Fast.h>
#include <HardwareSerial.h>
//#include <ESP32Time.h> //we quit using this crappy internal RTC
#include <WiFi.h>
#include "RTClib.h"
RTC_DS3231 rtc;
HardwareSerial hwSerial(1);
DFPlayerMini_Fast myDFPlayer;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
const char *ssid = "SSID";
const char *password = "PASS";
RTC_DATA_ATTR bool rtcTimeWasAlreadySetFromNTP = false;
bool noWifi;
int myDow, mySec, myMin, myHour;
int sleepHelper;
void setup() {
pinMode(4, OUTPUT);
pinMode(14,OUTPUT);
//digitalWrite(14, HIGH);//give power to rtc
Serial.begin(115200);
hwSerial.begin(9600, SERIAL_8N1, 16, 17); // second serial port to serve mp3 player
if (rtcTimeWasAlreadySetFromNTP == false) {
synchroniseExternalTime();
}
}
void loop() {
//---this is serial output to make sure we have correct time
//DateTime now = rtc.now();
DateTime now =grabTime();
//--- here are regular conditions
myDow = now.dayOfTheWeek();
mySec = now.second();
myMin = now.minute();
myHour = now.hour();
Serial.print("It is ");
Serial.print(myHour);
Serial.print(":");
Serial.print(myMin);
Serial.print(":");
Serial.println(mySec);
if (myMin > 31) {
sleepHelper = 59 - myMin;
} else {
sleepHelper = 29 - myMin;
};
if (myHour > 7 and myHour < 19 and mySec == 55 and sleepHelper == 0) { //according to the DFPlayer documentation, it needs 3-5 seconds to start
startPlayer();
}
//---------ringing conditions----------
if (mySec == 0 and myMin == 0) { // at full hour
if (myHour > 7 and myHour < 19) { // between 8 and 18
// here another condition
ringer(myHour);
}
}
if (mySec == 0 and myMin == 30) { // at half hour
if (myHour > 7 and myHour < 19) { // between 8 and 18
// here another condition
ringer(1);
}
}
// -----deep sleep conditions--------
if ((sleepHelper > 2)) {
deepsleep(sleepHelper);
}
delay(1000); //if there is a short time to chime, just work with delay
}
void ringer(int myhours2) {
//run mosfet here
Serial.print("Ringing:");
Serial.println(myhours2);
if (myhours2 > 12) {
myhours2 -= 12;
}
for (int n = 1; n <= myhours2; n++) {
Serial.print("Ring nr:");
Serial.println(n);
if (n == 1) {
myDFPlayer.playFromMP3Folder(1);
delay(2250);
} // ring the chime
else {
myDFPlayer.playFromMP3Folder(2);
delay(2250);
}
}
while (myDFPlayer.isPlaying()) {
};
digitalWrite(4, LOW);
Serial.println("Mosfet low");
}
void deepsleep(int sleepHelper) {
int countMicro = ((sleepHelper - 1) * 1000000 * 60);
esp_sleep_enable_timer_wakeup(countMicro);
Serial.println("Deepsleep");
Serial.print("Wake up in ");
Serial.print(sleepHelper);
Serial.print(" minutes, at ");
Serial.print(myHour);
Serial.print(":");
Serial.println(myMin += sleepHelper);
esp_deep_sleep_start();
}
void synchroniseExternalTime() {
WiFi.begin(ssid, password);
Serial.println("Looking for the WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
// if wifi found, break loop
}
Serial.println("WiFi connected");
//---------set with NTP---------------
// we have to rebuild this function to synchronise ext rtc
configTime(3600, 3600, "europe.pool.ntp.org");
//==== //this need to be further modified to meet the requirements of RTClib
struct tm timeinfo;
getLocalTime(&timeinfo);
int yr = timeinfo.tm_year + 1900;
int mt = timeinfo.tm_mon + 1;
int dy = timeinfo.tm_mday;
int hr = timeinfo.tm_hour;
int mi = timeinfo.tm_min;
int se = timeinfo.tm_sec;
digitalWrite(14,HIGH);
rtc.begin();
rtc.adjust(DateTime(yr, mt, dy, hr, mi, se));
Serial.println("RTC updated");
DateTime now = rtc.now();
Serial.print("It is ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
WiFi.disconnect();
WiFi.mode(WIFI_OFF); // disconnect after synchronisation to save power
rtcTimeWasAlreadySetFromNTP = true; // time was updated
Serial.println("WiFi turned off");
digitalWrite(14,LOW); //turn off power for RTC
}
void startPlayer() {
digitalWrite(4, HIGH);
Serial.println("Mosfet high");
hwSerial.begin(9600, SERIAL_8N1, 16, 17);
Serial.println("Hardware Serial started");
delay(100);
while (!myDFPlayer.begin(hwSerial)) {
}; //initializing mp3 player
myDFPlayer.volume(25);
delay(10);
}
DateTime grabTime(){
digitalWrite(14,HIGH);
while (!rtc.begin()) {
Serial.println("Couldn't find RTC");
delay(10);
}
DateTime now = rtc.now();
digitalWrite(14,LOW);
return now;
}