-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindicator.ino
More file actions
187 lines (158 loc) · 4.68 KB
/
bindicator.ino
File metadata and controls
187 lines (158 loc) · 4.68 KB
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
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WebServer.h>
#include "time_manager.h"
#include "oauth_handler.h"
#include "calendar_handler.h"
#include "secrets.h"
#include "display_handler.h"
#include "tasks.h"
#include "setup_server.h"
#include "animations.h"
#include "config_manager.h"
#include "serial_commands.h"
#include "button_handler.h"
#include "bindicator.h"
#include "bin_type.h"
OAuthHandler oauth(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GOOGLE_REDIRECT_URI);
CalendarHandler calendar(oauth);
DisplayHandler display;
bool hasRecycling, hasRubbish;
SetupServer setupServer(oauth);
bool inSetupMode = false;
const int BUTTON_PIN = 2;
const int LONG_PRESS_TIME = 3000;
BindicatorButton button(BUTTON_PIN, LONG_PRESS_TIME);
bool setupMDNS() {
if (!MDNS.begin("bindicator")) {
Serial.println("Error setting up MDNS responder!");
return false;
}
MDNS.addService("http", "tcp", 80);
Serial.println("mDNS responder started at bindicator.local");
return true;
}
bool tryWiFiConnection(int maxAttempts = 3) {
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
Serial.printf("\nWiFi connection attempt %d of %d\n", attempt, maxAttempts);
WiFi.setHostname("bindicator");
WiFi.begin(ConfigManager::getWifiSSID().c_str(),
ConfigManager::getWifiPassword().c_str());
int waitCount = 0;
while (WiFi.status() != WL_CONNECTED && waitCount < 20) {
delay(500);
Serial.print(".");
waitCount++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWiFi connected!");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
return true;
}
Serial.println("\nWiFi connection failed");
delay(1000);
}
return false;
}
void startSetupMode() {
delay(5000);
Serial.println("Entering setup mode");
inSetupMode = true;
WiFi.mode(WIFI_AP_STA);
Serial.println("WiFi mode set to AP+STA");
WiFi.setHostname("bindicator");
delay(100);
if (ConfigManager::isConfigured()) {
Serial.println("Connecting to main network...");
if (tryWiFiConnection()) {
Serial.println("\nConnected to main network");
Serial.print("Station IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Attempting to sync time...");
if (setupTime()) {
Serial.println("Time sync successful");
} else {
Serial.println("Time sync failed - calendar functions may be limited");
}
}
}
if (WiFi.status() != WL_CONNECTED) {
// default IP is 192.168.4.1
if (!WiFi.softAP("Bindicator Setup", AP_PASSWORD)) {
Serial.println("AP Start Failed");
return;
}
}
if (!setupMDNS()) {
Serial.println("mDNS setup failed");
}
setupServer.begin();
Serial.println("Web server started");
oauth.begin(setupServer.server);
Serial.println("OAuth handler initialized");
Command cmd = CMD_SHOW_SETUP_MODE;
xQueueSend(commandQueue, &cmd, 0);
}
void startNormalMode() {
if (!tryWiFiConnection()) {
Serial.println("Failed to connect to WiFi");
Command cmd = CMD_SHOW_ERROR_WIFI;
xQueueSend(commandQueue, &cmd, 0);
return;
}
xTaskCreatePinnedToCore(
calendarTask,
"CalendarTask",
8192,
NULL,
1,
NULL,
0
);
}
void setup() {
Serial.begin(115200);
Serial.println("Starting up...");
SerialCommands::begin();
delay(2000);
ConfigManager::begin();
display.begin();
oauth.begin(nullptr);
commandQueue = xQueueCreate(10, sizeof(Command));
xTaskCreatePinnedToCore(
animationTask,
"AnimationTask",
8192,
NULL,
2,
NULL,
1
);
if (ConfigManager::isInForcedSetupMode()) {
Serial.println("Force setup mode enabled");
startSetupMode();
ConfigManager::processSetupFlag();
return;
}
if (!ConfigManager::isConfigured() || oauth.loadRefreshToken().isEmpty()) {
Serial.println("No valid configuration found - entering setup mode");
startSetupMode();
} else {
Serial.println("Configuration loaded - starting normal mode");
startNormalMode();
}
button.begin();
Bindicator::initializeFromStorage();
}
void loop() {
SerialCommands::handle();
if (inSetupMode) {
setupServer.handleClient();
delay(50);
}
vTaskDelay(pdMS_TO_TICKS(10));
}