-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowtime-flag.ino
149 lines (120 loc) · 4.66 KB
/
flowtime-flag.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
// Platform libraries.
#include <Arduino.h> // To add IntelliSense for platform constants.
#include <ESP8266WiFi.h> // Required dependency for the NTPtimeESP library.
// Third-party libraries.
#include <WiFiManager.h> // To manage network configuration and connection.
#include "NTPtimeESP.h" // To get the current time through NTP.
// My classes.
#include "speed-servo.h"
// Read configuration and secrets.
#include "config.h"
WiFiManager wifiManager;
NTPtime ntpClient;
SpeedServo servo;
int flowTime1BeginMinutesOfDay;
int flowTime1EndMinutesOfDay;
int flowTime2BeginMinutesOfDay;
int flowTime2EndMinutesOfDay;
bool isNowFlowTimeLast;
int ntpCurrentRetryCount = 1;
void setup() {
initSerial();
initServo();
servo.moveSlowTo(FLOWTIME_END_POSITION + 30);
initNetwork();
initTimeServerConnection();
flowTime1BeginMinutesOfDay = getMinutesOfDay(FLOWTIME1_BEGIN_HOUR, FLOWTIME1_BEGIN_MINUTE);
flowTime1EndMinutesOfDay = getMinutesOfDay(FLOWTIME1_END_HOUR, FLOWTIME1_END_MINUTE);
flowTime2BeginMinutesOfDay = getMinutesOfDay(FLOWTIME2_BEGIN_HOUR, FLOWTIME2_BEGIN_MINUTE);
flowTime2EndMinutesOfDay = getMinutesOfDay(FLOWTIME2_END_HOUR, FLOWTIME2_END_MINUTE);
isNowFlowTimeLast = false;
Serial.printf("Configured flow times: %d:%d (%d) - %d:%d (%d) and %d:%d (%d) - %d:%d (%d).\n", FLOWTIME1_BEGIN_HOUR, FLOWTIME1_BEGIN_MINUTE, flowTime1BeginMinutesOfDay, FLOWTIME1_END_HOUR, FLOWTIME1_END_MINUTE, flowTime1EndMinutesOfDay, FLOWTIME2_BEGIN_HOUR, FLOWTIME2_BEGIN_MINUTE, flowTime2BeginMinutesOfDay, FLOWTIME2_END_HOUR, FLOWTIME2_END_MINUTE, flowTime2EndMinutesOfDay);
Serial.println(F("Setup completed."));
servo.moveSlowTo(FLOWTIME_END_POSITION);
}
void initSerial() {
Serial.begin(115200);
Serial.println();
Serial.println(F("Initializing serial connection DONE."));
}
void initServo() {
Serial.print(F("Initializing the servo..."));
servo.attach(PIN_SERVO);
servo.moveSlowTo(FLOWTIME_END_POSITION);
Serial.println(F("DONE."));
}
void initNetwork() {
Serial.printf("Initializing connection to the network with MAC address %s using WiFiManager (SSID: %s)...\n", WiFi.macAddress().c_str(), WIFI_AP_SSID);
wifiManager.autoConnect(WIFI_AP_SSID, WIFI_AP_PASSWORD);
Serial.printf("WiFi connected. IP address: %s, MAC address: %s\n", WiFi.localIP().toString().c_str(), WiFi.macAddress().c_str());
}
void initTimeServerConnection() {
Serial.printf("Initializing time server connection to %s...\n", NTP_SERVER_HOST);
ntpClient.init(NTP_SERVER_HOST);
}
void loop() {
strDateTime currentTime = getCurrentTime();
if (currentTime.valid) {
bool isNowFlowTime = isFlowTime(currentTime);
if (isNowFlowTime != isNowFlowTimeLast) {
if (isNowFlowTime) {
beginFlowTime();
}
else {
endFlowTime();
}
}
isNowFlowTimeLast = isNowFlowTime;
}
delay(60000);
}
void beginFlowTime() {
Serial.println(F("Starting flow time..."));
servo.moveSlowTo(WAVE_START_POSITION);
delay(500);
servo.moveSlowTo(WAVE_END_POSITION);
delay(500);
servo.moveSlowTo(WAVE_START_POSITION);
delay(500);
servo.moveSlowTo(WAVE_END_POSITION);
delay(500);
servo.moveSlowTo(FLOWTIME_START_POSITION);
}
void endFlowTime() {
Serial.println(F("Ending flow time..."));
servo.moveSlowTo(FLOWTIME_END_POSITION);
}
strDateTime getCurrentTime() {
Serial.print(F("Getting current time..."));
ntpClient.setRecvTimeout(59);
strDateTime currentTime = ntpClient.getNTPtime(TIME_ZONE, DST_ADJUSTMENT);
if(currentTime.valid){
ntpCurrentRetryCount = 1; // Reset the retry counter;
ntpClient.printDateTime(currentTime);
return currentTime;
} else {
ntpCurrentRetryCount++;
if (ntpCurrentRetryCount <= NTP_MAX_RETRY_COUNT) {
Serial.println(F("Getting current time failed, waiting and trying again..."));
delay(NTP_RETRY_DELAY_MSEC);
return getCurrentTime();
} else {
Serial.println(F("Getting current time failed, no more retries."));
strDateTime invalidCurrentTime;
invalidCurrentTime.valid = false;
return invalidCurrentTime;
}
}
}
bool isFlowTime(strDateTime currentTime) {
byte currentHour = currentTime.hour;
byte currentMinute = currentTime.minute;
int currentMinutesOfDay = getMinutesOfDay(currentHour, currentMinute);
Serial.printf("Current time: %d:%d (%d)\n", currentHour, currentMinute, currentMinutesOfDay);
bool isFlowTime1 = flowTime1BeginMinutesOfDay <= currentMinutesOfDay && currentMinutesOfDay <= flowTime1EndMinutesOfDay;
bool isFlowTime2 = flowTime2BeginMinutesOfDay <= currentMinutesOfDay && currentMinutesOfDay <= flowTime2EndMinutesOfDay;
return isFlowTime1 || isFlowTime2;
}
int getMinutesOfDay(byte hours, byte minutes) {
return 60 * hours + minutes;
}