-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathESP32WebcamBluetoothWifi.ino
284 lines (242 loc) · 7.48 KB
/
ESP32WebcamBluetoothWifi.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
#include <ArduinoWebsockets.h>
#include "esp_http_server.h"
#include <WiFi.h>
#include "esp_camera.h"
#include "camera_index.h"
#include <Preferences.h>
#include "BluetoothSerial.h"
#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
String ssids_array[50];
String network_string;
String connected_string;
const char* pref_ssid = "";
const char* pref_pass = "";
String client_wifi_ssid;
String client_wifi_password;
const char* bluetooth_name = "robot01";
long start_wifi_millis;
long wifi_timeout = 10000;
bool bluetooth_disconnect = false;
enum wifi_setup_stages { NONE, SCAN_START, SCAN_COMPLETE, SSID_ENTERED, WAIT_PASS, PASS_ENTERED, WAIT_CONNECT, LOGIN_FAILED };
enum wifi_setup_stages wifi_stage = NONE;
camera_fb_t * fb = NULL;
using namespace websockets;
WebsocketsServer socket_server;
BluetoothSerial SerialBT;
Preferences preferences;
httpd_handle_t camera_httpd = NULL;
void setup()
{
Serial.begin(115200);
Serial.println("Booting...");
preferences.begin("wifi_access", false);
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;
//init with high specs to pre-allocate larger buffers
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;
}
#if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
#endif
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_QVGA);
if (!init_wifi()) { // Connect to Wi-Fi fails
SerialBT.register_callback(callback);
} else {
SerialBT.register_callback(callback_show_ip);
}
SerialBT.begin(bluetooth_name);
app_httpserver_init();
socket_server.listen(82);
}
bool init_wifi()
{
String temp_pref_ssid = preferences.getString("pref_ssid");
String temp_pref_pass = preferences.getString("pref_pass");
pref_ssid = temp_pref_ssid.c_str();
pref_pass = temp_pref_pass.c_str();
Serial.println(pref_ssid);
Serial.println(pref_pass);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
start_wifi_millis = millis();
WiFi.begin(pref_ssid, pref_pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (millis() - start_wifi_millis > wifi_timeout) {
WiFi.disconnect(true, true);
return false;
}
}
return true;
}
void scan_wifi_networks()
{
WiFi.mode(WIFI_STA);
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
if (n == 0) {
SerialBT.println("no networks found");
} else {
SerialBT.println();
SerialBT.print(n);
SerialBT.println(" networks found");
delay(1000);
for (int i = 0; i < n; ++i) {
ssids_array[i + 1] = WiFi.SSID(i);
Serial.print(i + 1);
Serial.print(": ");
Serial.println(ssids_array[i + 1]);
network_string = i + 1;
network_string = network_string + ": " + WiFi.SSID(i) + " (Strength:" + WiFi.RSSI(i) + ")";
SerialBT.println(network_string);
}
wifi_stage = SCAN_COMPLETE;
}
}
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
if (event == ESP_SPP_SRV_OPEN_EVT) {
wifi_stage = SCAN_START;
}
if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == SCAN_COMPLETE) { // data from phone is SSID
int client_wifi_ssid_id = SerialBT.readString().toInt();
client_wifi_ssid = ssids_array[client_wifi_ssid_id];
wifi_stage = SSID_ENTERED;
}
if (event == ESP_SPP_DATA_IND_EVT && wifi_stage == WAIT_PASS) { // data from phone is password
client_wifi_password = SerialBT.readString();
client_wifi_password.trim();
wifi_stage = PASS_ENTERED;
}
}
void callback_show_ip(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
if (event == ESP_SPP_SRV_OPEN_EVT) {
SerialBT.print("ESP32 IP: ");
SerialBT.println(WiFi.localIP());
bluetooth_disconnect = true;
}
}
static esp_err_t main_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
return httpd_resp_send(req, (const char *)main_html, main_html_len);
}
httpd_uri_t main_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = main_handler,
.user_ctx = NULL
};
void app_httpserver_init ()
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
if (httpd_start(&camera_httpd, &config) == ESP_OK)
{
Serial.println("httpd_start");
httpd_register_uri_handler(camera_httpd, &main_uri);
}
}
void disconnect_bluetooth()
{
delay(1000);
Serial.println("BT stopping");
SerialBT.println("Bluetooth disconnecting...");
delay(1000);
SerialBT.flush();
SerialBT.disconnect();
SerialBT.end();
Serial.println("BT stopped");
delay(1000);
bluetooth_disconnect = false;
}
void loop()
{
if (bluetooth_disconnect)
{
disconnect_bluetooth();
}
switch (wifi_stage)
{
case SCAN_START:
SerialBT.println("Scanning Wi-Fi networks");
Serial.println("Scanning Wi-Fi networks");
scan_wifi_networks();
SerialBT.println("Please enter the number for your Wi-Fi");
wifi_stage = SCAN_COMPLETE;
break;
case SSID_ENTERED:
SerialBT.println("Please enter your Wi-Fi password");
Serial.println("Please enter your Wi-Fi password");
wifi_stage = WAIT_PASS;
break;
case PASS_ENTERED:
SerialBT.println("Please wait for Wi-Fi connection...");
Serial.println("Please wait for Wi_Fi connection...");
wifi_stage = WAIT_CONNECT;
preferences.putString("pref_ssid", client_wifi_ssid);
preferences.putString("pref_pass", client_wifi_password);
if (init_wifi()) { // Connected to WiFi
connected_string = "ESP32 IP: ";
connected_string = connected_string + WiFi.localIP().toString();
SerialBT.println(connected_string);
Serial.println(connected_string);
bluetooth_disconnect = true;
} else { // try again
wifi_stage = LOGIN_FAILED;
}
break;
case LOGIN_FAILED:
SerialBT.println("Wi-Fi connection failed");
Serial.println("Wi-Fi connection failed");
delay(2000);
wifi_stage = SCAN_START;
break;
}
if (socket_server.poll()) {
disconnect_bluetooth();
auto client = socket_server.accept();
while (client.available()) {
fb = esp_camera_fb_get();
client.sendBinary((const char *)fb->buf, fb->len);
esp_camera_fb_return(fb);
fb = NULL;
}
}
}