forked from dajensen/esp32GrowLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32GrowLight.ino
310 lines (264 loc) · 9.27 KB
/
esp32GrowLight.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "wifi_config.h"
#include <WiFi.h>
//#include <PubSubClient.h>
#include "TimeSpec.h"
#include "timer_config.h"
#include <ezTime.h>
//#include <M5StickC.h> This is just a thing, not an M5Stick
WiFiClient wifiClient;
String uniqueId;
//PubSubClient mqttClient(wifiClient);
Timezone LocalTime;
#define LOCAL_TIMEZONE "America/Denver"
boolean light_state = false;
boolean prev_desired_state = false;
boolean manual_override = false;
//const int STATUS_LED = 5; // 10 for esp32 M5StickC, 5 for esp32 Thing
const int POWER_PIN = 5; // 26 for esp32 M5StickC, 5 for esp32 Thing
const int BUTTON_PIN = 0;
/*
static const char *ENUMERATE_TOPIC = "616b7b49-aab4-4cbb-a7a8-ba7ed744dc11/Enumerate";
static const char *STATUS_TOPIC = "616b7b49-aab4-4cbb-a7a8-ba7ed744dc11/Status";
static const char *OFFLINE_TOPIC = "616b7b49-aab4-4cbb-a7a8-ba7ed744dc11/Offline";
static const char *LIGHTON_TOPIC = "616b7b49-aab4-4cbb-a7a8-ba7ed744dc11/LightOn";
static const char *LIGHTOFF_TOPIC = "616b7b49-aab4-4cbb-a7a8-ba7ed744dc11/LightOff";
void mqttCallback(char *topic, byte* payload, unsigned int len) {
if(strcmp(topic, ENUMERATE_TOPIC) == 0) {
onEnumerate(payload, len);
}
else if(strcmp(topic, LIGHTON_TOPIC) == 0) {
onLightOn(payload, len);
}
else if(strcmp(topic, LIGHTOFF_TOPIC) == 0) {
onLightOff(payload, len);
}
}
*/
void PrintMacAddr(const unsigned char *mac) {
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
void GetUniqueId(String &id, const char *prefix)
{
const int WL_MAC_ADDR_LENGTH_MINE = 12;
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "<prefix>-"
uint8_t mac[WL_MAC_ADDR_LENGTH_MINE];
WiFi.macAddress(mac);
String macID = "";
PrintMacAddr(mac);
if(mac[4] < 0x10)
macID += '0';
macID += String(mac[4], HEX);
if(mac[5] < 0x10)
macID += '0';
macID += String(mac[5], HEX);
macID.toUpperCase();
id = String(prefix) + "-" + macID;
}
void create_status_message(String &dest, String id, boolean status) {
dest = id + "," + String(status);
}
/*
void connect_mqtt(PubSubClient &client, const char *server, const unsigned int port, const char *id) {
client.setServer(server, port);
delay(5000); // This is required for the esp32 mqtt library. Otherwise the whole device sometimes hangs when a reconnect happens. Others are having the same trouble.
client.connect(id);
while(!client.connected()) {
// This is a bad block of code because it doesn't do anything to reconnect WiFi. That's something that should be done.
if(WiFi.status() != WL_CONNECTED)
printStatus("DISCONNECTED FROM WiFi ...", "");
delay(500);
Serial.print(":");
}
Serial.println("After connecting: " + String(client.connected()));
Serial.println("Mqtt State: " + String(client.state()));
String msg;
create_status_message(msg, id, light_state);
client.publish(STATUS_TOPIC, msg.c_str());
Serial.println("Published: " + msg);
client.subscribe(ENUMERATE_TOPIC);
client.subscribe(LIGHTON_TOPIC);
client.subscribe(LIGHTOFF_TOPIC);
}
*/
/*
void initLCD() {
// put your setup code here, to run once:
M5.begin();
M5.Lcd.setRotation(1);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(32, 0, 2);
M5.Lcd.println("Grow Lights");
}
void printStatus(const char *wifiStatus, const char *mqttStatus) {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(32, 0, 2);
M5.Lcd.println("Grow Lights");
M5.Lcd.setCursor(0, 30);
M5.Lcd.println(wifiStatus);
M5.Lcd.println(mqttStatus);
}
*/
void print_times() {
Serial.println("");
Serial.println("ON TIMES:");
for (byte i = 0; i < (sizeof(on_times) / sizeof(on_times[0])); i++){
TimeSpec time = on_times[i];
Serial.println(String(time.hour) + ":" + String(time.minute));
}
Serial.println("");
Serial.println("OFF TIMES:");
for (byte i = 0; i < (sizeof(on_times) / sizeof(on_times[0])); i++){
TimeSpec time = off_times[i];
Serial.println(String(time.hour) + ":" + String(time.minute));
}
}
void setup() {
digitalWrite(POWER_PIN, 0);
//digitalWrite(STATUS_LED, 1); // Inverted for the built-in light
//pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(POWER_PIN, OUTPUT);
//pinMode(STATUS_LED, OUTPUT);
//Serial.setDebugOutput(true);
//initLCD();
Serial.begin(115200);
//delay(5000); // This is necessary for the ESP32 to get serial messages
Serial.println("..... STARTING .....");
Serial.println("Connecting to WiFi ...");
WiFi.begin(cfg.ssid, cfg.password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Waiting for WiFi..........");
delay(500);
}
GetUniqueId(uniqueId, "Growlight");
Serial.println("UniqueID: " + uniqueId);
Serial.println("Connected to WiFi, NOT Connecting to MQTT ...");
/*
mqttClient.setCallback(mqttCallback);
connect_mqtt(mqttClient, cfg.mqtt_server, cfg.mqtt_port, uniqueId.c_str());
printStatus("Connected to WiFi", "Connected to MQTT");
*/
waitForSync(); // from ezTime, means wait for NTP to get date and time.
LocalTime.setLocation(LOCAL_TIMEZONE);
Serial.println("Local time: " + LocalTime.dateTime());
Serial.println("Hour: " + String(LocalTime.hour()) + " Minute: " + String(LocalTime.minute()));
print_times();
}
void loop() {
/*
mqttClient.loop();
if(!mqttClient.connected()){
Serial.print("-");
printStatus("Connected to WiFi", "Connecting to MQTT ...");
connect_mqtt(mqttClient, cfg.mqtt_server, cfg.mqtt_port, uniqueId.c_str());
printStatus("Connected to wifi", "Connected to MQTT");
}
*/
check_timer();
checkButtonPress();
}
void check_timer() {
/*const int CHECK_FREQUENCY = 5000;
static int lastPrintTime = 0;
int currentTime = millis();
if(currentTime - lastPrintTime > CHECK_FREQUENCY || lastPrintTime == 0) {
Serial.println("Local time: " + LocalTime.dateTime());
int current_second_of_day = LocalTime.hour() * SECONDS_PER_HOUR + LocalTime.minute() * SECONDS_PER_MINUTE + LocalTime.second();
Serial.print("Second Of Day: " + String(current_second_of_day));
Serial.println("");
lastPrintTime = currentTime;
boolean desiredState = current_second_of_day > start_time.secondval() && current_second_of_day < stop_time.secondval();
Serial.println("Desired light state: " + String(desiredState));
if(desiredState != light_state && (!manual_override || desiredState != prev_desired_state)){
handleButtonPress();
Serial.println("Set light_state to " + String(light_state));
}
if(desiredState != prev_desired_state) {
Serial.println("Clearing manual override");
manual_override = false;
}
prev_desired_state = desiredState;
}
*/
const int CHECK_FREQUENCY = 60000;
static int lastPrintTime = 0;
int currentTime = millis();
if(currentTime - lastPrintTime > CHECK_FREQUENCY || lastPrintTime == 0) {
int current_second_of_day = LocalTime.hour() * SECONDS_PER_HOUR + LocalTime.minute() * SECONDS_PER_MINUTE + LocalTime.second();
Serial.println("Checking at " + String(LocalTime.hour()) + ":" + String(LocalTime.minute()) + ":" + String(LocalTime.second()));
for (byte i = 0; i < (sizeof(on_times) / sizeof(on_times[0])); i++){
if (current_second_of_day - on_times[i].secondval() < 31 && current_second_of_day - on_times[i].secondval() > -31 ) {
setLightState(1);
}
}
for (byte i = 0; i < (sizeof(off_times) / sizeof(off_times[0])); i++){
if (current_second_of_day - off_times[i].secondval() < 31 && current_second_of_day - off_times[i].secondval() > -31 ) {
setLightState(0);
}
}
lastPrintTime = currentTime;
}
}
void checkButtonPress() {
static unsigned long lastDebounceTime = 0;
static unsigned long debounceDelay = 100;
static int lastReading = digitalRead(BUTTON_PIN);
int reading = digitalRead(BUTTON_PIN);
if(reading != lastReading) {
if(millis() - lastDebounceTime > debounceDelay) {
handleButtonPress();
Serial.println("BUTTONS!!!!!");
}
lastDebounceTime = millis();
}
}
void onEnumerate(byte *msg, unsigned int len) {
Serial.println("Enumerate");
String topicMsg;
create_status_message(topicMsg, uniqueId, light_state);
// mqttClient.publish(STATUS_TOPIC, topicMsg.c_str());
}
/*
void onLightOn(byte *msg, unsigned int len) {
Serial.println("OnLightOn");
if(strncmp((const char *)msg, uniqueId.c_str(), uniqueId.length()) == 0) {
setLightState(true);
manual_override = true;
}
}
void onLightOff(byte *msg, unsigned int len) {
Serial.println("OnLightOff");
if(strncmp((const char *)msg, uniqueId.c_str(), uniqueId.length()) == 0) {
setLightState(false);
manual_override = true;
}
}
*/
void handleButtonPress() {
setLightState(!light_state);
manual_override = true;
}
void setLightState(bool onOff) {
if(onOff)
Serial.println("Light state: On");
else
Serial.println("Light state: Off");
digitalWrite(POWER_PIN, onOff);
//digitalWrite(STATUS_LED, !onOff); // Inverted for the built-in light
light_state = onOff;
String msg;
create_status_message(msg, uniqueId, light_state);
// mqttClient.publish(STATUS_TOPIC, msg.c_str());
}