16
16
#include < BleGamepad.h>
17
17
#include < Adafruit_NeoPixel.h>
18
18
19
+ #include < esp_wifi.h>
19
20
#include < WiFi.h>
20
21
#include < WiFiClient.h>
21
22
#include < WebServer.h>
22
23
#include < ESPmDNS.h>
23
24
#include < Update.h>
24
25
26
+ bool web_ota = false ;
27
+
28
+ int sleepSeconds = 30 ; // how long is it inactive before going to sleep
29
+
25
30
const char * host = " esp32" ;
26
31
const char * ssid = " xxxxxxx" ; // your WiFi SSID here
27
- const char * password = " xxxxxxxxx " ; // your WiFi password here
32
+ const char * password = " xxxxxxxx " ; // your WiFi password here
28
33
WebServer server (80 );
29
34
30
35
/*
@@ -118,7 +123,7 @@ const char* serverIndex =
118
123
#define numOfButtons 12
119
124
// sleep wake button definition (also update line in setup(): 'esp_sleep_enable_ext0_wakeup(GPIO_NUM_4,0);')
120
125
#define BUTTON_PIN_BITMASK 0x10 // start button on RTC GPIO pin 4 which is 0x10 (2^4 in hex)
121
- RTC_DATA_ATTR int bootCount = 0 ;
126
+ // RTC_DATA_ATTR int bootCount = 0;
122
127
123
128
BleGamepad bleGamepad (" ItsyController" , " Adafruit" , 100 ); // name, manufacturer, batt level to start
124
129
byte previousButtonStates[numOfButtons];
@@ -133,21 +138,7 @@ byte physicalButtons[numOfButtons] = { 1, 2, 4, 5, 7, 8, 15, 16, 13, 1
133
138
// gampad: O/b0, X/b1, ^/b3, []]/b4, l_trig/b6, r_trig/b7, up/b14 , down/b15 , left/b12 , right/b13, select/b11, start/b10
134
139
135
140
int last_button_press = millis();
136
- int sleepTime = 30000 ; // how long is it inactive before going to sleep
137
-
138
- // void print_wakeup_reason(){
139
- // esp_sleep_wakeup_cause_t wakeup_reason;
140
- // wakeup_reason = esp_sleep_get_wakeup_cause();
141
- // switch(wakeup_reason)
142
- // {
143
- // case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
144
- // case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
145
- // case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
146
- // case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
147
- // case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
148
- // default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
149
- // }
150
- // }
141
+ int sleepTime = (sleepSeconds * 1000 );
151
142
152
143
Adafruit_NeoPixel pixel (1 , 0 , NEO_GRB + NEO_KHZ800); // Itsy on-board NeoPixel
153
144
@@ -172,79 +163,84 @@ void setup()
172
163
pixel.begin ();
173
164
pixel.clear ();
174
165
175
- // Connect to WiFi network
176
- WiFi.begin (ssid, password);
177
- Serial.println (" " );
178
-
179
- // Wait for connection for 20 seconds, then move on
180
- unsigned long startTime = millis (); // Get the current time
181
- while (!(WiFi.status () == WL_CONNECTED) && ((millis () - startTime) < 2000 )) {
182
- delay (500 );
183
- Serial.print (" ." );
184
- }
185
-
186
- if (WiFi.status () == WL_CONNECTED) {
166
+ if (web_ota) {
187
167
168
+ // Connect to WiFi network
169
+ WiFi.begin (ssid, password);
188
170
Serial.println (" " );
189
- Serial.print (" Connected to " );
190
- Serial.println (ssid);
191
- Serial.print (" IP address: " );
192
- Serial.println (WiFi.localIP ());
193
171
194
- /* use mdns for host name resolution*/
195
- if (!MDNS.begin (host)) { // http://esp32.local
196
- Serial.println (" Error setting up MDNS responder!" );
197
- while (1 ) {
198
- delay (1000 );
199
- }
172
+ // Wait for connection for 20 seconds, then move on
173
+ unsigned long startTime = millis (); // Get the current time
174
+ while (!(WiFi.status () == WL_CONNECTED) && ((millis () - startTime) < 2000 )) {
175
+ delay (500 );
176
+ Serial.print (" ." );
200
177
}
201
- Serial.println (" mDNS responder started" );
202
- /* return index page which is stored in serverIndex */
203
- server.on (" /" , HTTP_GET, []() {
204
- server.sendHeader (" Connection" , " close" );
205
- server.send (200 , " text/html" , loginIndex);
206
- });
207
- server.on (" /serverIndex" , HTTP_GET, []() {
208
- server.sendHeader (" Connection" , " close" );
209
- server.send (200 , " text/html" , serverIndex);
210
- });
211
- /* handling uploading firmware file */
212
- server.on (" /update" , HTTP_POST, []() {
213
- server.sendHeader (" Connection" , " close" );
214
- server.send (200 , " text/plain" , (Update.hasError ()) ? " FAIL" : " OK" );
215
- ESP.restart ();
216
- }, []() {
217
- HTTPUpload& upload = server.upload ();
218
- if (upload.status == UPLOAD_FILE_START) {
219
- Serial.printf (" Update: %s\n " , upload.filename .c_str ());
220
- if (!Update.begin (UPDATE_SIZE_UNKNOWN)) { // start with max available size
221
- Update.printError (Serial);
222
- }
223
- } else if (upload.status == UPLOAD_FILE_WRITE) {
224
- /* flashing firmware to ESP*/
225
- if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ) {
226
- Update.printError (Serial);
227
- }
228
- } else if (upload.status == UPLOAD_FILE_END) {
229
- if (Update.end (true )) { // true to set the size to the current progress
230
- Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
231
- } else {
232
- Update.printError (Serial);
178
+
179
+ if (WiFi.status () == WL_CONNECTED) {
180
+
181
+ Serial.println (" " );
182
+ Serial.print (" Connected to " );
183
+ Serial.println (ssid);
184
+ Serial.print (" IP address: " );
185
+ Serial.println (WiFi.localIP ());
186
+
187
+ /* use mdns for host name resolution*/
188
+ if (!MDNS.begin (host)) { // http://esp32.local
189
+ Serial.println (" Error setting up MDNS responder!" );
190
+ while (1 ) {
191
+ delay (1000 );
233
192
}
234
193
}
235
- });
236
- server.begin ();
237
- }
238
- else {
239
- Serial.println (" " );
240
- Serial.println (" WiFi connection timed out, you may need to update SSID/password. Moving on now." );
194
+ Serial.println (" mDNS responder started" );
195
+ /* return index page which is stored in serverIndex */
196
+ server.on (" /" , HTTP_GET, []() {
197
+ server.sendHeader (" Connection" , " close" );
198
+ server.send (200 , " text/html" , loginIndex);
199
+ });
200
+ server.on (" /serverIndex" , HTTP_GET, []() {
201
+ server.sendHeader (" Connection" , " close" );
202
+ server.send (200 , " text/html" , serverIndex);
203
+ });
204
+ /* handling uploading firmware file */
205
+ server.on (" /update" , HTTP_POST, []() {
206
+ server.sendHeader (" Connection" , " close" );
207
+ server.send (200 , " text/plain" , (Update.hasError ()) ? " FAIL" : " OK" );
208
+ ESP.restart ();
209
+ }, []() {
210
+ HTTPUpload& upload = server.upload ();
211
+ if (upload.status == UPLOAD_FILE_START) {
212
+ Serial.printf (" Update: %s\n " , upload.filename .c_str ());
213
+ if (!Update.begin (UPDATE_SIZE_UNKNOWN)) { // start with max available size
214
+ Update.printError (Serial);
215
+ }
216
+ } else if (upload.status == UPLOAD_FILE_WRITE) {
217
+ /* flashing firmware to ESP*/
218
+ if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ) {
219
+ Update.printError (Serial);
220
+ }
221
+ } else if (upload.status == UPLOAD_FILE_END) {
222
+ if (Update.end (true )) { // true to set the size to the current progress
223
+ Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
224
+ } else {
225
+ Update.printError (Serial);
226
+ }
227
+ }
228
+ });
229
+ server.begin ();
230
+ }
231
+ else {
232
+ Serial.println (" " );
233
+ Serial.println (" WiFi connection timed out, you may need to update SSID/password. Moving on now." );
234
+ }
241
235
}
242
236
}
243
237
244
238
void loop ()
245
239
{
246
- server.handleClient ();
247
- delay (1 );
240
+ if (web_ota) {
241
+ server.handleClient ();
242
+ delay (1 );
243
+ }
248
244
249
245
if (bleGamepad.isConnected ())
250
246
{
@@ -280,6 +276,10 @@ void loop()
280
276
bleGamepad.sendReport ();
281
277
}
282
278
if (millis () - last_button_press > sleepTime) {
279
+ server.stop ();
280
+ delay (300 );
281
+ esp_wifi_stop ();
282
+ delay (300 );
283
283
esp_deep_sleep_start ();
284
284
}
285
285
}
0 commit comments