Skip to content

Commit 989cd9f

Browse files
authored
Merge pull request #2793 from jedgarpark/psx-controller
deeper sleep via 'esp_wifi_stop()'
2 parents 3970513 + ea790e9 commit 989cd9f

File tree

1 file changed

+79
-79
lines changed

1 file changed

+79
-79
lines changed

PlayStation_BLE/PlayStation_ESP32_BLE_Gamepad.ino

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
#include <BleGamepad.h>
1717
#include <Adafruit_NeoPixel.h>
1818

19+
#include <esp_wifi.h>
1920
#include <WiFi.h>
2021
#include <WiFiClient.h>
2122
#include <WebServer.h>
2223
#include <ESPmDNS.h>
2324
#include <Update.h>
2425

26+
bool web_ota = false;
27+
28+
int sleepSeconds = 30; // how long is it inactive before going to sleep
29+
2530
const char* host = "esp32";
2631
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
2833
WebServer server(80);
2934

3035
/*
@@ -118,7 +123,7 @@ const char* serverIndex =
118123
#define numOfButtons 12
119124
// sleep wake button definition (also update line in setup(): 'esp_sleep_enable_ext0_wakeup(GPIO_NUM_4,0);')
120125
#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;
122127

123128
BleGamepad bleGamepad("ItsyController", "Adafruit", 100); // name, manufacturer, batt level to start
124129
byte previousButtonStates[numOfButtons];
@@ -133,21 +138,7 @@ byte physicalButtons[numOfButtons] = { 1, 2, 4, 5, 7, 8, 15, 16, 13, 1
133138
// 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
134139

135140
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);
151142

152143
Adafruit_NeoPixel pixel(1, 0, NEO_GRB + NEO_KHZ800); // Itsy on-board NeoPixel
153144

@@ -172,79 +163,84 @@ void setup()
172163
pixel.begin();
173164
pixel.clear();
174165

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) {
187167

168+
// Connect to WiFi network
169+
WiFi.begin(ssid, password);
188170
Serial.println("");
189-
Serial.print("Connected to ");
190-
Serial.println(ssid);
191-
Serial.print("IP address: ");
192-
Serial.println(WiFi.localIP());
193171

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(".");
200177
}
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\nRebooting...\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);
233192
}
234193
}
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\nRebooting...\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+
}
241235
}
242236
}
243237

244238
void loop()
245239
{
246-
server.handleClient();
247-
delay(1);
240+
if (web_ota) {
241+
server.handleClient();
242+
delay(1);
243+
}
248244

249245
if (bleGamepad.isConnected())
250246
{
@@ -280,6 +276,10 @@ void loop()
280276
bleGamepad.sendReport();
281277
}
282278
if (millis() - last_button_press > sleepTime) {
279+
server.stop();
280+
delay(300);
281+
esp_wifi_stop();
282+
delay(300);
283283
esp_deep_sleep_start();
284284
}
285285
}

0 commit comments

Comments
 (0)