-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeOS-vid-cap-loc.ino
More file actions
327 lines (270 loc) · 8.43 KB
/
LifeOS-vid-cap-loc.ino
File metadata and controls
327 lines (270 loc) · 8.43 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ESP32-CAM Camera + GPS webserver (final, corrected)
// Includes IP printing for /capture and /location without breaking anything.
// Added: /stream MJPEG live stream endpoint
#include "esp_camera.h"
#include <WiFi.h>
#include <WebServer.h>
#include <TinyGPSPlus.h>
// --- WiFi credentials ---
const char* WIFI_SSID = "IQOOZ6";
const char* WIFI_PASS = "12345678";
// --- Camera model: AI-Thinker pins ---
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
WebServer server(80);
// --- GPS ---
TinyGPSPlus gps;
HardwareSerial SerialGPS(2);
const int GPS_RX_PIN = 13; // GPS TX -> ESP32
const int GPS_TX_PIN = 12; // GPS RX <- ESP32
// -------------------------------
// NEW: Print server URLs
// -------------------------------
void printServerURLs() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("======================================");
Serial.println("ESP32-CAM Web Server Running At:");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.println("PORT: 80");
Serial.print("➤ Capture URL: http://");
Serial.print(WiFi.localIP());
Serial.println("/capture");
Serial.print("➤ Location URL: http://");
Serial.print(WiFi.localIP());
Serial.println("/location");
Serial.print("➤ Stream URL: http://");
Serial.print(WiFi.localIP());
Serial.println("/stream");
Serial.println("======================================");
}
}
// -------------------------------
// Camera Init
// -------------------------------
bool initCamera() {
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
if (psramFound()) {
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
return false;
}
delay(2000);
return true;
}
// -------------------------------
// HTTP /capture
// -------------------------------
void handleCapture() {
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
server.send(500, "text/plain", "Camera capture failed");
return;
}
WiFiClient client = server.client();
if (!client) {
esp_camera_fb_return(fb);
return;
}
String header = String("HTTP/1.1 200 OK\r\n");
header += "Content-Type: image/jpeg\r\n";
header += "Content-Length: " + String(fb->len) + "\r\n";
header += "Connection: close\r\n\r\n";
client.print(header);
client.write(fb->buf, fb->len);
esp_camera_fb_return(fb);
unsigned long start = millis();
while (client.connected() && millis() - start < 100) {
delay(1);
}
client.stop();
}
// -------------------------------
// HTTP /stream (MJPEG)
// -------------------------------
void handleStream() {
WiFiClient client = server.client();
if (!client) {
server.send(500, "text/plain", "Client failed");
return;
}
// Send initial response headers for multipart MJPEG
String response = String("HTTP/1.1 200 OK\r\n");
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n";
response += "Connection: close\r\n\r\n";
client.print(response);
while (client.connected()) {
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
// If fail to get frame, break the stream
Serial.println("Camera capture failed for stream");
break;
}
// Send multipart frame header
client.print("--frame\r\n");
client.print("Content-Type: image/jpeg\r\n");
client.print("Content-Length: " + String(fb->len) + "\r\n\r\n");
// Send image
size_t written = client.write(fb->buf, fb->len);
// End of frame
client.print("\r\n");
esp_camera_fb_return(fb);
// Small delay to allow client to receive data and to avoid starving other tasks
if (!client.connected()) break;
// Yield to background tasks
yield();
delay(10);
}
// Close connection cleanly
if (client.connected()) client.stop();
}
// -------------------------------
// HTTP /location
// -------------------------------
void handleLocation() {
if (!gps.location.isValid()) {
server.send(500, "application/json", "{\"error\":\"no_fix\"}");
return;
}
unsigned long age_ms = gps.location.age();
const unsigned long STALE_MS = 10000;
if (age_ms > STALE_MS) {
String js = "{\"error\":\"stale_fix\",\"age_ms\":";
js += age_ms;
js += "}";
server.send(500, "application/json", js);
return;
}
String js = "{\"lat\":";
js += String(gps.location.lat(), 6);
js += ",\"lng\":";
js += String(gps.location.lng(), 6);
js += ",\"age_ms\":";
js += age_ms;
js += "}";
server.send(200, "application/json", js);
}
// -------------------------------
// HTTP /
// -------------------------------
void handleIndex() {
String html = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>";
html += "<h2>ESP32-CAM</h2>";
html += "<p><a href=\"/capture\">/capture (jpg)</a></p>";
html += "<p><a href=\"/location\">/location (json)</a></p>";
html += "<p><a href=\"/stream\">/stream (live MJPEG)</a></p>";
html += "<p>PSRAM: ";
html += (psramFound() ? "found" : "not found");
html += "</p></body></html>";
server.send(200, "text/html", html);
}
// -------------------------------
// WiFi Auto-Reconnect
// -------------------------------
unsigned long lastWiFiCheck = 0;
const unsigned long WIFI_CHECK_INTERVAL_MS = 5000;
void ensureWiFiConnected() {
if (WiFi.status() == WL_CONNECTED) return;
unsigned long now = millis();
if (now - lastWiFiCheck < WIFI_CHECK_INTERVAL_MS) return;
lastWiFiCheck = now;
Serial.println("WiFi disconnected — attempting reconnect...");
WiFi.disconnect(false, true);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < 3000) {
delay(50);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi reconnected!");
printServerURLs();
}
}
// -------------------------------
// Setup
// -------------------------------
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("\nStarting ESP32-CAM + GPS webserver...");
SerialGPS.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
Serial.printf("GPS UART started on RX=%d TX=%d\n", GPS_RX_PIN, GPS_TX_PIN);
if (!initCamera()) {
Serial.println("Camera init failed. Halting.");
while (true) delay(1000);
}
Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
Serial.printf("PSRAM found: %s\n", psramFound() ? "YES" : "NO");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
Serial.printf("Connecting to WiFi SSID: %s\n", WIFI_SSID);
// Wait max 10s for connection (non-blocking)
unsigned long startAttempt = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttempt < 10000) {
delay(100);
}
if (WiFi.status() == WL_CONNECTED) {
printServerURLs();
}
server.on("/", HTTP_GET, handleIndex);
server.on("/capture", HTTP_GET, handleCapture);
server.on("/location", HTTP_GET, handleLocation);
server.begin();
Serial.println("HTTP server started on port 80");
}
// -------------------------------
// Loop
// -------------------------------
void loop() {
server.handleClient();
while (SerialGPS.available()) {
gps.encode(SerialGPS.read());
}
ensureWiFiConnected();
yield();
}