Skip to content

Commit

Permalink
- Added Pico W (RP2040) Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushsharma82 committed Sep 21, 2023
1 parent 7f3ee75 commit cd638ed
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
20 changes: 17 additions & 3 deletions examples/Demo/Demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
Github: https://github.com/ayushsharma82/ElegantOTA
WiKi: https://docs.elegantota.pro
Works with both ESP8266 & ESP32
Works with following hardware:
- ESP8266
- ESP32
- RP2040 (with WiFi) (Example: Raspberry Pi Pico W)
Important note for RP2040 users:
- RP2040 requires LittleFS partition for the OTA updates to work. Without LittleFS partition, OTA updates will fail.
Make sure to select Tools > Flash Size > "2MB (Sketch 1MB, FS 1MB)" option.
- If using bare RP2040, it requires WiFi module like Pico W for ElegantOTA to work.
-------------------------------
Expand All @@ -27,17 +36,22 @@
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#elif defined(TARGET_RP2040)
#include <WiFi.h>
#include <WebServer.h>
#endif

#include <ElegantOTA.h>

const char* ssid = "........";
const char* password = "........";
const char* ssid = "201";
const char* password = "frenzy8284";

#if defined(ESP8266)
ESP8266WebServer server(80);
#elif defined(ESP32)
WebServer server(80);
#elif defined(TARGET_RP2040)
WebServer server(80);
#endif

unsigned long ota_progress_millis = 0;
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
],
"version": "3.0.0",
"frameworks": "arduino",
"platforms": "espressif"
"platforms": ["espressif", "raspberrypi"]
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ author=Ayush Sharma
category=Communication
maintainer=Ayush Sharma <asrocks5@gmail.com>
sentence=OTA updates made slick and simple for everyone!
paragraph=A user interface library which provides an interactive portal for your over-the-air updates for wireless microcontrollers.
paragraph=A OTA library which provides an interactive portal for your over-the-air updates for wireless microcontrollers.
url=https://github.com/ayushsharma82/ElegantOTA
architectures=esp8266,esp32
architectures=esp8266,esp32,rp2040
37 changes: 32 additions & 5 deletions src/ElegantOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username,
_authenticate = true;
}

#if defined(TARGET_RP2040)
if (!__isPicoW) {
ELEGANTOTA_DEBUG_MSG("RP2040: Not a Pico W, skipping OTA setup\n");
return;
}
#endif

#if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1
_server->on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){
if(_authenticate && !request->authenticate(_username, _password)){
Expand Down Expand Up @@ -93,7 +100,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username,
_update_error_str = str.c_str();
_update_error_str += "\n";
ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str());
}
}
#endif

return request->send((Update.hasError()) ? 400 : 200, "text/plain", (Update.hasError()) ? _update_error_str.c_str() : "OK");
Expand Down Expand Up @@ -142,9 +149,6 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username,
if (mode == OTA_MODE_FILESYSTEM) {
close_all_fs();
}
if (mode == OTA_MODE_FILESYSTEM) {
close_all_fs();
}
Update.runAsync(true);
if (!Update.begin(update_size, mode == OTA_MODE_FILESYSTEM ? U_FS : U_FLASH)) {
ELEGANTOTA_DEBUG_MSG("Failed to start update process\n");
Expand All @@ -165,6 +169,24 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username,
_update_error_str += "\n";
ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str());
}
#elif defined(TARGET_RP2040)
uint32_t update_size = 0;
// Gather FS Size
if (mode == OTA_MODE_FILESYSTEM) {
update_size = ((size_t)&_FS_end - (size_t)&_FS_start);
LittleFS.end();
} else {
FSInfo64 i;
LittleFS.begin();
LittleFS.info64(i);
update_size = i.totalBytes - i.usedBytes;
}
// Start update process
if (!Update.begin(update_size, mode == OTA_MODE_FILESYSTEM ? U_FS : U_FLASH)) {
ELEGANTOTA_DEBUG_MSG("Failed to start update process because there is not enough space\n");
_update_error_str = "Not enough space";
return _server->send(400, "text/plain", _update_error_str.c_str());
}
#endif

return _server->send((Update.hasError()) ? 400 : 200, "text/plain", (Update.hasError()) ? _update_error_str.c_str() : "OK");
Expand Down Expand Up @@ -295,7 +317,12 @@ void ElegantOTAClass::loop() {
// Check if 2 seconds have passed since _reboot_request_millis was set
if (_reboot && millis() - _reboot_request_millis > 2000) {
ELEGANTOTA_DEBUG_MSG("Rebooting...\n");
ESP.restart();
#if defined(ESP8266) || defined(ESP32)
ESP.reset();
#elif defined(TARGET_RP2040)
rp2040.reboot();
#endif
_reboot = false;
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/ElegantOTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,31 @@ _____ _ _ ___ _____ _
#define ELEGANTOTA_WEBSERVER WebServer
#endif
#define HARDWARE "ESP32"
#elif defined(TARGET_RP2040)
#include "Arduino.h"
#include "FS.h"
#include "LittleFS.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "WebServer.h"
#include "WiFiUdp.h"
#include "StreamString.h"
#include "Updater.h"
#define HARDWARE "RP2040"
#define ELEGANTOTA_WEBSERVER WebServer
// Throw an error if async mode is enabled
#if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1
#error "Async mode is not supported on RP2040. Please set ELEGANTOTA_USE_ASYNC_WEBSERVER to 0."
#endif
extern uint8_t _FS_start;
extern uint8_t _FS_end;
#endif

enum OTA_Mode {
OTA_MODE_FIRMWARE = 0,
OTA_MODE_FILESYSTEM = 1
};


class ElegantOTAClass{
public:
ElegantOTAClass();
Expand Down

0 comments on commit cd638ed

Please sign in to comment.