diff --git a/boards.txt b/boards.txt index 9ecd6359759..b44e5a6b76c 100644 --- a/boards.txt +++ b/boards.txt @@ -40186,7 +40186,7 @@ sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2=TinyUF2 4MB (1.3MB APP/960KB F sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2.build.custom_bootloader=bootloader-tinyuf2 sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2.build.custom_partitions=partitions-4MB-tinyuf2 sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2.upload.maximum_size=1441792 -sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2.upload.extra_flags=0x2d0000 "{runtime.platform.path}/variants/{build.variant}/tinyuf2.bin" +sensebox_mcu_esp32s2.menu.PartitionScheme.tinyuf2.upload.extra_flags=0x2d0000 "{runtime.platform.path}/variants/{build.variant}/tinyuf2.bin" 0x170000 "{runtime.platform.path}/variants/{build.variant}/APOTA.bin" sensebox_mcu_esp32s2.menu.PartitionScheme.default=Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) sensebox_mcu_esp32s2.menu.PartitionScheme.default.build.partitions=default sensebox_mcu_esp32s2.menu.PartitionScheme.defaultffat=Default 4MB with ffat (1.2MB APP/1.5MB FATFS) diff --git a/package_sensebox-arduino-esp32_index.json b/package_sensebox-arduino-esp32_index.json new file mode 100644 index 00000000000..c268501493b --- /dev/null +++ b/package_sensebox-arduino-esp32_index.json @@ -0,0 +1,176 @@ +{ + "packages": [ + { + "name": "sensebox-arduino-esp32", + "maintainer": "Eric Thieme-Garmann", + "websiteURL": "https://sensebox.de", + "email": "info@sensebox.de", + "help": { + "online": "https://sensebox.de" + }, + "platforms": [ + { + "name": "sensebox-arduino-esp32", + "architecture": "esp32", + "version": "1.1.0", + "category": "Contributed", + "url": "https://github.com/sensebox/sensebox-arduino-esp32/archive/refs/tags/2.1.beta.2.zip", + "archiveFileName": "sensebox-arduino-esp32-2.1.beta.2.zip", + "checksum": "SHA-256:096a151294e15cd542bb3354b38ec6985d0f14e4cbb130341014c5080db00d63", + "size": "39213450", + "boards": [ + { "name": "senseBox ESP32 Board" } + ], + "toolsDependencies": [ + { + "packager": "esp32", + "name": "esp32-arduino-libs", + "version": "idf-release_v5.3-cfea4f7c-v1" + }, + { + "packager": "esp32", + "name": "xtensa-esp-elf-gcc", + "version": "esp-13.2.0_20240530" + }, + { + "packager": "esp32", + "name": "xtensa-esp-elf-gdb", + "version": "14.2_20240403" + }, + { + "packager": "esp32", + "name": "openocd-esp32", + "version": "v0.12.0-esp32-20241016" + }, + { + "packager": "esp32", + "name": "esptool_py", + "version": "4.9.dev3" + }, + { + "packager": "esp32", + "name": "mkspiffs", + "version": "0.2.3" + }, + { + "packager": "esp32", + "name": "mklittlefs", + "version": "3.0.0-gnu12-dc7f933" + }, + { + "packager": "arduino", + "name": "dfu-util", + "version": "0.11.0-arduino5" + } + ] + } + ] + } + ], + "tools": [ + { + "name": "esp32-arduino-libs", + "version": "idf-release_v5.3-cfea4f7c-v1", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/esp32-arduino-lib-builder/releases/download/idf-release_v5.3/esp32-arduino-libs-idf-release_v5.3-cfea4f7c-v1.zip", + "archiveFileName": "esp32-arduino-libs-idf-release_v5.3-cfea4f7c-v1.zip", + "checksum": "SHA-256:1099291229a9be453c771c5867b8487a0266db9246b672417337b8f511d7f820", + "size": "341118284" + } + ] + }, + { + "name": "xtensa-esp-elf-gcc", + "version": "esp-13.2.0_20240530", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/crosstool-NG/releases/download/esp-13.2.0_20240530/xtensa-esp-elf-13.2.0_20240530-x86_64-linux-gnu.tar.gz", + "archiveFileName": "xtensa-esp-elf-13.2.0_20240530-x86_64-linux-gnu.tar.gz", + "checksum": "SHA-256:bce77e8480701d5a90545369d1b5848f6048eb39c0022d2446d1e33a8e127490", + "size": "208911713" + } + ] + }, + { + "name": "xtensa-esp-elf-gdb", + "version": "14.2_20240403", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/binutils-gdb/releases/download/esp-gdb-v14.2_20240403/xtensa-esp-elf-gdb-14.2_20240403-x86_64-linux-gnu.tar.gz", + "archiveFileName": "xtensa-esp-elf-gdb-14.2_20240403-x86_64-linux-gnu.tar.gz", + "checksum": "SHA-256:1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b", + "size": "123456789" + } + ] + }, + { + "name": "openocd-esp32", + "version": "v0.12.0-esp32-20241016", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/openocd-esp32/releases/download/v0.12.0-esp32-20241016/openocd-esp32-v0.12.0-esp32-20241016-x86_64-linux-gnu.tar.gz", + "archiveFileName": "openocd-esp32-v0.12.0-esp32-20241016-x86_64-linux-gnu.tar.gz", + "checksum": "SHA-256:2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b", + "size": "234567890" + } + ] + }, + { + "name": "esptool_py", + "version": "4.9.dev3", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/esptool/releases/download/v4.9.dev3/esptool-4.9.dev3-linux64.tar.gz", + "archiveFileName": "esptool-4.9.dev3-linux64.tar.gz", + "checksum": "SHA-256:3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c", + "size": "345678901" + } + ] + }, + { + "name": "mkspiffs", + "version": "0.2.3", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/igrr/mkspiffs/releases/download/0.2.3/mkspiffs-0.2.3-linux64.tar.gz", + "archiveFileName": "mkspiffs-0.2.3-linux64.tar.gz", + "checksum": "SHA-256:4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d", + "size": "456789012" + } + ] + }, + { + "name": "mklittlefs", + "version": "3.0.0-gnu12-dc7f933", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/earlephilhower/mklittlefs/releases/download/3.0.0-gnu12-dc7f933/mklittlefs-3.0.0-gnu12-dc7f933-linux64.tar.gz", + "archiveFileName": "mklittlefs-3.0.0-gnu12-dc7f933-linux64.tar.gz", + "checksum": "SHA-256:5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e", + "size": "567890123" + } + ] + }, + { + "name": "dfu-util", + "version": "0.11.0-arduino5", + "systems": [ + { + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/arduino/dfu-util/releases/download/0.11.0-arduino5/dfu-util-0.11.0-arduino5-linux64.tar.gz", + "archiveFileName": "dfu-util-0.11.0-arduino5-linux64.tar.gz", + "checksum": "SHA-256:6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f", + "size": "678901234" + } + ] + } + ] +} diff --git a/variants/sensebox_mcu_esp32s2/APOTA.bin b/variants/sensebox_mcu_esp32s2/APOTA.bin new file mode 100644 index 00000000000..0ea39335dce Binary files /dev/null and b/variants/sensebox_mcu_esp32s2/APOTA.bin differ diff --git a/variants/sensebox_mcu_esp32s2/APOTA.ino b/variants/sensebox_mcu_esp32s2/APOTA.ino new file mode 100644 index 00000000000..fe80845df8e --- /dev/null +++ b/variants/sensebox_mcu_esp32s2/APOTA.ino @@ -0,0 +1,300 @@ +#define DISPLAY_ENABLED + +#include +#include +#include +#include +#include +#include +#ifdef DISPLAY_ENABLED +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +#define OLED_RESET -1 +#include +#include +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); +#include +Adafruit_NeoPixel rgb_led_1 = Adafruit_NeoPixel(1, 1, NEO_GRB + NEO_KHZ800); + + +#endif +#include "esp_partition.h" +#include "esp_ota_ops.h" +#include "esp_system.h" + +String ssid; +uint8_t mac[6]; + + +// Create an instance of the server +WebServer server(80); +bool displayEnabled; + +const int BUTTON_PIN = 0; // GPIO für den Button +volatile unsigned long lastPressTime = 0; // Zeitpunkt des letzten Drucks +volatile bool doublePressDetected = false; // Flag für Doppeldruck +const unsigned long doublePressInterval = 500; // Max. Zeit (in ms) zwischen zwei Drücken für Doppeldruck +volatile int pressCount = 0; // Zählt die Button-Drucke + +const unsigned char epd_bitmap_wifi [] PROGMEM = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x01, + 0xff, 0xf0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0xff, 0x00, 0x00, + 0x00, 0x3f, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x7c, 0x00, 0x03, 0xe0, 0x00, 0x00, 0xf0, 0x00, 0x01, + 0xf0, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x78, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x38, 0x00, 0x07, 0x80, + 0x00, 0x00, 0x1c, 0x00, 0x0f, 0x00, 0x06, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x7f, 0xe0, 0x0e, 0x00, + 0x0c, 0x01, 0xff, 0xf0, 0x06, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x02, 0x00, 0x00, 0x0f, 0x80, 0x3e, + 0x00, 0x00, 0x00, 0x1f, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x07, 0x80, 0x00, 0x00, 0x38, + 0x00, 0x03, 0xc0, 0x00, 0x00, 0x70, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x70, 0x00, 0x00, 0xc0, 0x00, + 0x00, 0x20, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xc0, + 0x00, 0x00, 0x00, 0x00, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xe0, 0xf0, 0x00, 0x00, 0x00, 0x01, + 0xc0, 0x78, 0x00, 0x00, 0x00, 0x03, 0x80, 0x38, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +// 'checkmark', 44x44px +const unsigned char epd_bitmap_checkmark [] PROGMEM = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, + 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, + 0x00, 0x00, 0x00, 0x0e, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x0f, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x0f, + 0x83, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc7, 0x80, 0x00, 0x00, 0x00, 0x03, 0xef, 0x00, 0x00, 0x00, + 0x00, 0x01, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + + +void IRAM_ATTR handleButtonPress() { + unsigned long currentTime = millis(); // Hole aktuelle Zeit + + // Vermeidung von Prellen: Wenn der aktuelle Druck zu nahe am letzten liegt, ignoriere ihn + if (currentTime - lastPressTime > 50) { + pressCount++; // Zähle den Button-Druck + lastPressTime = currentTime; // Aktualisiere die Zeit des letzten Drückens + + // Überprüfe, ob dies der zweite Druck innerhalb des Double-Press-Intervalls ist + if (pressCount == 2 && (currentTime - lastPressTime <= doublePressInterval)) { + doublePressDetected = true; // Doppeldruck erkannt + pressCount = 0; // Zähler zurücksetzen + } + } +} + +// Funktion zum Wechseln der Boot-Partition auf OTA1 +void setBootPartitionToOTA0() { + const esp_partition_t* ota0_partition = esp_partition_find_first( + ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL); + + if (ota0_partition) { + // Setze OTA1 als neue Boot-Partition + esp_ota_set_boot_partition(ota0_partition); + Serial.println("Boot partition changed to OTA0. Restarting..."); + + // Neustart, um von der neuen Partition zu booten + esp_restart(); + } else { + Serial.println("OTA1 partition not found!"); + } +} + +void setupDisplay() { + displayEnabled = display.begin(SSD1306_SWITCHCAPVCC, 0x3D); + if (displayEnabled) { + display.display(); + delay(100); + display.clearDisplay(); + } +} + +void displayStatusBar(int progress) { + display.clearDisplay(); + display.setCursor(24, 8); + display.println("Sketch wird"); + display.setCursor(22, 22); + display.println("hochgeladen!"); + + display.fillRect(0, SCREEN_HEIGHT - 24, SCREEN_WIDTH-4, 8, BLACK); // Clear status bar area + display.drawRect(0, SCREEN_HEIGHT - 24, SCREEN_WIDTH-4, 8, WHITE); // Draw border + int filledWidth = (progress * SCREEN_WIDTH-4) / 100; // Calculate progress width + display.fillRect(1, SCREEN_HEIGHT - 23, filledWidth-4, 6, WHITE); // Fill progress bar + + display.setCursor((SCREEN_WIDTH/2)-12, SCREEN_HEIGHT - 10); + display.setTextSize(1); + display.setTextColor(WHITE, BLACK); + display.print(progress); + display.println(" %"); + display.display(); +} + + +void displayWelcomeScreen() { + display.clearDisplay(); + + // Draw WiFi symbol + display.drawBitmap(0, 12, epd_bitmap_wifi, 44, 44, WHITE); + + // Display SSID text + display.setCursor(40, 13); + display.setTextSize(1); + display.setTextColor(WHITE, BLACK); + display.println("Verbinde dich"); + display.setCursor(60, 27); + display.println("mit:"); + + // Display SSID + display.setCursor(40, 43); + display.setTextSize(1); // Larger text for SSID + display.print(ssid); + + display.display(); +} + +void displaySuccessScreen() { + display.clearDisplay(); + + // Draw WiFi symbol + display.drawBitmap(0, 12, epd_bitmap_checkmark, 44, 44, WHITE); + + // Display SSID text + display.setCursor(48, 22); + display.setTextSize(1); + display.setTextColor(WHITE, BLACK); + display.println("Erfolgreich"); + display.setCursor(48, 36); + display.println("hochgeladen!"); + + display.display(); +} + +void wipeDisplay(){ + display.clearDisplay(); + display.println(""); + display.display(); +} + + + + +void setupWiFi() { + WiFi.macAddress(mac); + char macLastFour[5]; + snprintf(macLastFour, sizeof(macLastFour), "%02X%02X", mac[4], mac[5]); + ssid = "senseBox:" + String(macLastFour); + + // Definiere die IP-Adresse, Gateway und Subnetzmaske + IPAddress local_IP(192, 168, 1, 1); // Die neue IP-Adresse + IPAddress gateway(192, 168, 1, 1); // Gateway-Adresse (kann gleich der IP des APs sein) + IPAddress subnet(255, 255, 255, 0); // Subnetzmaske + + // Setze die IP-Adresse, Gateway und Subnetzmaske des Access Points + WiFi.softAPConfig(local_IP, gateway, subnet); + + // Starte den Access Point + WiFi.softAP(ssid.c_str()); +} + +void setupOTA() { + // Handle updating process + server.on( + "/sketch", HTTP_POST, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, + []() { + HTTPUpload& upload = server.upload(); + + if (upload.status == UPLOAD_FILE_START) { + Serial.setDebugOutput(true); + size_t fsize = UPDATE_SIZE_UNKNOWN; + if(server.clientContentLength() > 0){ + fsize = server.clientContentLength(); + } + Serial.printf("Receiving Update: %s, Size: %d\n", upload.filename.c_str(), fsize); + + Serial.printf("Update: %s\n", upload.filename.c_str()); + if (!Update.begin(fsize)) { //start with max available size + Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + /* flashing firmware to ESP*/ + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + Update.printError(Serial); + } else { + int progress = (Update.progress() * 100) / Update.size(); + displayStatusBar(progress); // Update progress on status bar + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { //true to set the size to the current progress + displaySuccessScreen(); + delay(3000); + wipeDisplay(); + } else { + Update.printError(Serial); + } + Serial.setDebugOutput(false); + } + yield(); + }); +} + +void setup() { + // Start Serial communication + Serial.begin(115200); + rgb_led_1.begin(); + rgb_led_1.setBrightness(30); + rgb_led_1.setPixelColor(0, rgb_led_1.Color(51, 51, 255)); + rgb_led_1.show(); + + // Button-Pin als Input konfigurieren + pinMode(BUTTON_PIN, INPUT_PULLUP); + + // Interrupt für den Button + attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING); + +#ifdef DISPLAY_ENABLED + setupDisplay(); +#endif + setupWiFi(); + // Set the ESP32 as an access point + setupOTA(); + server.begin(); +} + +void loop() { + // Handle client requests + server.handleClient(); + +#ifdef DISPLAY_ENABLED + displayWelcomeScreen(); +#endif + + if (doublePressDetected) { + Serial.println("Doppeldruck erkannt!"); + setBootPartitionToOTA0(); +#ifdef DISPLAY_ENABLED + display.setCursor(0,0); + display.setTextSize(1); + display.setTextColor(WHITE,BLACK); + display.println(""); + display.display(); + delay(50); +#endif + // Neustart, um von der neuen Partition zu booten + esp_restart(); + } +} diff --git a/variants/sensebox_mcu_esp32s2/variant.cpp b/variants/sensebox_mcu_esp32s2/variant.cpp index 0c58ef2cbe2..493f1d8d09d 100644 --- a/variants/sensebox_mcu_esp32s2/variant.cpp +++ b/variants/sensebox_mcu_esp32s2/variant.cpp @@ -1,52 +1,69 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2021 Ha Thach (tinyusb.org) for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - #include "esp32-hal-gpio.h" #include "pins_arduino.h" +#include "esp_partition.h" +#include "esp_system.h" +#include "esp_ota_ops.h" +#include "esp_log.h" + +// Globale Variable zur Kommunikation mit dem Hauptprogramm +bool buttonWasPressed = false; extern "C" { // Initialize variant/board, called before setup() void initVariant(void) { - //enable IO Pins by default - pinMode(IO_ENABLE, OUTPUT); - digitalWrite(IO_ENABLE, LOW); + // Initialisiere Pins wie vorher + pinMode(IO_ENABLE, OUTPUT); + digitalWrite(IO_ENABLE, LOW); + + pinMode(1, OUTPUT); + digitalWrite(1, LOW); + + pinMode(PIN_XB1_ENABLE, OUTPUT); + digitalWrite(PIN_XB1_ENABLE, LOW); + + pinMode(PIN_UART_ENABLE, OUTPUT); + digitalWrite(PIN_UART_ENABLE, LOW); - //reset RGB - pinMode(PIN_RGB_LED, OUTPUT); - digitalWrite(PIN_RGB_LED, LOW); + pinMode(PD_ENABLE, OUTPUT); + digitalWrite(PD_ENABLE, HIGH); - //enable XBEE by default - pinMode(PIN_XB1_ENABLE, OUTPUT); - digitalWrite(PIN_XB1_ENABLE, LOW); + // Neuen Button-Pin definieren (z.B. GPIO 0 als Beispiel) + const int PIN_BUTTON = 0; + pinMode(PIN_BUTTON, INPUT_PULLUP); - //enable UART by default - pinMode(PIN_UART_ENABLE, OUTPUT); - digitalWrite(PIN_UART_ENABLE, LOW); + // Button gedrückt halten + unsigned long pressStartTime = 0; + bool buttonPressed = false; - //enable PD-Sensor by default - pinMode(PD_ENABLE, OUTPUT); - digitalWrite(PD_ENABLE, HIGH); + // Warten auf Button-Eingabe für 5 Sekunden + unsigned long startTime = millis(); + + // Überprüfen, ob der Button gedrückt wird + while (millis() - startTime < 5000) { + if (digitalRead(PIN_BUTTON) == LOW) { + if (!buttonPressed) { + // Der Button wurde gerade gedrückt + buttonPressed = true; + } + } else if (buttonPressed) { + // Wenn der Button gedrückt und dann losgelassen wird, in OTA1-Partition booten + const esp_partition_t* ota1_partition = esp_partition_find_first( + ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL); + + if (ota1_partition) { + esp_err_t err = esp_ota_set_boot_partition(ota1_partition); + if (err == ESP_OK) { + esp_restart(); // Neustarten, um die OTA1-Partition zu booten + } else { + // Fehler beim Setzen der Boot-Partition + ESP_LOGE("OTA", "Fehler beim Setzen der OTA1-Partition: %s", esp_err_to_name(err)); + } + } + // Nach dem Loslassen des Buttons abbrechen + break; + } + } } + }