-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoggyDoorbell.ino
288 lines (235 loc) · 8.21 KB
/
DoggyDoorbell.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
#include <WiFi.h>
#include <Preferences.h>
#include <HTTPClient.h>
// Pin definitions
const int buttonPin = 19; // Button input pin
// Global objects
Preferences preferences;
HTTPClient http;
// State variables
bool buttonState = false;
bool lastButtonState = false;
String triggerURL = "";
// WiFi credentials will be loaded from preferences
String activeSSID = "";
String activePassword = "";
// Menu system
const char* MENU_PROMPT = "\n=== Doggy Doorbell Menu ===\n"
"1. Test doorbell trigger\n"
"2. Scan and connect to WiFi\n"
"3. Configure trigger URL\n"
"4. Show current settings\n"
"Enter choice (1-4): ";
const char* WELCOME_MSG = "\n*********************************\n"
"* Welcome to Doggy Doorbell *\n"
"* Configuration Terminal *\n"
"*********************************\n";
// Wait for serial input with timeout
String readSerialTimeout(unsigned long timeout = 30000) {
String input = "";
unsigned long startTime = millis();
while ((millis() - startTime) < timeout) {
if (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (input.length() > 0) {
return input;
}
} else {
input += c;
}
}
delay(10);
}
return input;
}
// Scan for WiFi networks and return selected SSID
String scanAndSelectNetwork() {
Serial.println("\nScanning WiFi networks...");
int numNetworks = WiFi.scanNetworks();
if (numNetworks == 0) {
Serial.println("No networks found!");
return "";
}
Serial.printf("\nFound %d networks:\n", numNetworks);
for (int i = 0; i < numNetworks; ++i) {
Serial.printf("%d. %s (Signal: %d dBm)\n",
i + 1,
WiFi.SSID(i).c_str(),
WiFi.RSSI(i));
delay(10);
}
Serial.println("\nEnter number of network to connect to (1-" + String(numNetworks) + "): ");
String selection = readSerialTimeout();
int networkIndex = selection.toInt() - 1;
if (networkIndex >= 0 && networkIndex < numNetworks) {
return WiFi.SSID(networkIndex);
}
Serial.println("Invalid selection!");
return "";
}
// Configure WiFi settings
void configureWiFi() {
String newSSID = scanAndSelectNetwork();
if (newSSID.length() == 0) {
return;
}
Serial.printf("\nEnter password for %s: ", newSSID.c_str());
String newPassword = readSerialTimeout();
if (newPassword.length() == 0) {
Serial.println("Password entry timed out");
return;
}
// Save to preferences
preferences.begin("wifi-config", false);
preferences.putString("ssid", newSSID);
preferences.putString("password", newPassword);
preferences.end();
Serial.println("\nWiFi credentials saved. Reconnecting...");
// Attempt to connect with new credentials
WiFi.disconnect();
WiFi.begin(newSSID.c_str(), newPassword.c_str());
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.printf("\nConnected to %s\nIP: %s\n",
newSSID.c_str(),
WiFi.localIP().toString().c_str());
} else {
Serial.println("\nFailed to connect. Please check credentials.");
}
}
// Configure trigger URL
void configureTriggerURL() {
Serial.println("\nEnter trigger URL (e.g., https://trigger.esp8266-server.de/api/?id=...): ");
String newURL = readSerialTimeout();
if (newURL.length() == 0) {
Serial.println("URL entry timed out");
return;
}
// Basic URL validation
if (!newURL.startsWith("http://") && !newURL.startsWith("https://")) {
Serial.println("Invalid URL! Must start with http:// or https://");
return;
}
// Save to preferences
preferences.begin("wifi-config", false);
preferences.putString("trigger-url", newURL);
preferences.end();
triggerURL = newURL;
Serial.println("Trigger URL saved successfully!");
}
// Show current settings
void showSettings() {
preferences.begin("wifi-config", true);
Serial.println("\nCurrent Settings:");
Serial.println("----------------");
Serial.printf("SSID: %s\n", preferences.getString("ssid", "Not set").c_str());
Serial.printf("Password: %s\n", "********");
Serial.printf("WiFi Status: %s\n",
WiFi.status() == WL_CONNECTED ? "Connected" : "Disconnected");
if (WiFi.status() == WL_CONNECTED) {
Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
}
Serial.printf("Trigger URL: %s\n",
preferences.getString("trigger-url", "Not set").c_str());
preferences.end();
}
// Process menu choices
void handleMenu() {
Serial.print(MENU_PROMPT);
String choice = readSerialTimeout();
switch (choice.toInt()) {
case 1:
Serial.println("\nTesting doorbell trigger...");
triggerDoorbell();
break;
case 2:
configureWiFi();
break;
case 3:
configureTriggerURL();
break;
case 4:
showSettings();
break;
default:
Serial.println("\nInvalid choice!");
break;
}
}
void triggerDoorbell() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected! Cannot trigger.");
return;
}
if (triggerURL.length() == 0) {
Serial.println("Trigger URL not configured!");
return;
}
Serial.println("Triggering doorbell...");
http.begin(triggerURL);
int httpResponseCode = http.GET();
if (httpResponseCode <= 0) {
Serial.printf("First attempt failed with code: %d\n", httpResponseCode);
delay(50); // Wait 100ms before retry
httpResponseCode = http.GET(); // Retry the request
}
if (httpResponseCode > 0) {
String response = http.getString();
Serial.printf("HTTP Response code: %d\n", httpResponseCode);
Serial.printf("Response: %s\n", response.c_str());
} else {
Serial.printf("Error code: %d\n", httpResponseCode);
}
http.end();
}
void setup() {
Serial.begin(115200);
Serial.println(WELCOME_MSG);
// Configure button pin with internal pulldown
pinMode(buttonPin, INPUT_PULLDOWN);
// Initialize preferences and load settings
preferences.begin("wifi-config", true);
activeSSID = preferences.getString("ssid", "");
activePassword = preferences.getString("password", "");
triggerURL = preferences.getString("trigger-url", "");
preferences.end();
// Connect to WiFi if credentials exist
if (activeSSID.length() > 0) {
WiFi.mode(WIFI_STA);
WiFi.begin(activeSSID.c_str(), activePassword.c_str());
Serial.printf("\nConnecting to %s ", activeSSID.c_str());
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.printf("Connected! IP: %s\n", WiFi.localIP().toString().c_str());
} else {
Serial.println("Connection failed. Use menu to reconfigure.");
}
}
// Display initial menu
handleMenu();
}
void loop() {
// Check physical button with pulldown
buttonState = digitalRead(buttonPin); // Now reading directly since we use pulldown
if (buttonState != lastButtonState && buttonState == HIGH) { // Changed to HIGH since we're using pulldown
triggerDoorbell();
delay(100); // Small debounce delay
}
lastButtonState = buttonState;
// Check for serial input
if (Serial.available()) {
handleMenu();
}
}