-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from HowardsPlayPen/PR
Changes to allow optional integration of PsychicHttp
- Loading branch information
Showing
4 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
----------------------- | ||
ElegantOTA - Async Demo Example | ||
----------------------- | ||
NOTE: Make sure you have enabled Async Mode in ElegantOTA before compiling this example! | ||
Guide: https://docs.elegantota.pro/async-mode/ | ||
Skill Level: Beginner | ||
This example provides with a bare minimal app with ElegantOTA functionality which works | ||
with AsyncWebServer. | ||
Github: https://github.com/ayushsharma82/ElegantOTA | ||
WiKi: https://docs.elegantota.pro | ||
Works with both ESP8266 & ESP32 | ||
------------------------------- | ||
Upgrade to ElegantOTA Pro: https://elegantota.pro | ||
*/ | ||
|
||
#if defined(ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#include <ESPAsyncTCP.h> | ||
#elif defined(ESP32) | ||
#include <WiFi.h> | ||
#endif | ||
|
||
#include <ElegantOTA.h> | ||
#include <PsychicHttp.h> | ||
|
||
PsychicWebSocketHandler websocketHandler; | ||
PsychicEventSource eventSource; | ||
PsychicHttpServer server; | ||
|
||
|
||
const char* ssid = "........"; | ||
const char* password = "........"; | ||
|
||
unsigned long ota_progress_millis = 0; | ||
|
||
void onOTAStart() { | ||
// Log when OTA has started | ||
Serial.println("OTA update started!"); | ||
// <Add your own code here> | ||
} | ||
|
||
void onOTAProgress(size_t current, size_t final) { | ||
// Log every 1 second | ||
if (millis() - ota_progress_millis > 1000) { | ||
ota_progress_millis = millis(); | ||
Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final); | ||
} | ||
} | ||
|
||
void onOTAEnd(bool success) { | ||
// Log when OTA has finished | ||
if (success) { | ||
Serial.println("OTA update finished successfully!"); | ||
} else { | ||
Serial.println("There was an error during OTA update!"); | ||
} | ||
// <Add your own code here> | ||
} | ||
|
||
void setup(void) { | ||
Serial.begin(115200); | ||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(ssid, password); | ||
Serial.println(""); | ||
|
||
// Wait for connection | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
Serial.println(""); | ||
Serial.print("Connected to "); | ||
Serial.println(ssid); | ||
Serial.print("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
server.listen(80); // NOTE: for PsychicHttp you MUST call listen() before registering any urls using .on() | ||
|
||
// Set Authentication Credentials | ||
ElegantOTA.setAuth("test", "test"); | ||
|
||
//setup server config stuff here | ||
server.config.max_uri_handlers = 20; //maximum number of uri handlers (.on() calls) | ||
|
||
server.onOpen([](PsychicClient *client) { | ||
Serial.printf("[http] connection #%u connected from %s\n", client->socket(), client->remoteIP().toString()); | ||
}); | ||
|
||
//example callback everytime a connection is closed | ||
server.onClose([](PsychicClient *client) { | ||
Serial.printf("[http] connection #%u closed from %s\n", client->socket(), client->remoteIP().toString()); | ||
}); | ||
|
||
ElegantOTA.begin(&server); // Start ElegantOTA | ||
// ElegantOTA callbacks | ||
ElegantOTA.onStart(onOTAStart); | ||
ElegantOTA.onProgress(onOTAProgress); | ||
ElegantOTA.onEnd(onOTAEnd); | ||
|
||
Serial.println("HTTP server started"); | ||
} | ||
|
||
void loop(void) { | ||
ElegantOTA.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:esp32cam] | ||
platform = espressif32 | ||
board = esp32cam | ||
framework = arduino | ||
monitor_speed = 115200 | ||
lib_ldf_mode = deep | ||
; NOTE the below build flag activates the code for using PsychicHttp | ||
build_flags = -DELEGANTOTA_USE_PSYCHIC=1 | ||
lib_deps = | ||
; I found that the packaged up version was not sufficient / recent enough | ||
https://github.com/hoeken/PsychicHttp.git | ||
; if you were pulling ElegantOTA from this temporay fork | ||
;https://github.com/HowardsPlayPen/ElegantOTA.git | ||
; To pull mainline from the main repository | ||
https://github.com/ayushsharma82/ElegantOTA.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters