Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: OTA Initialization
- - Source Code NOT compiled for: Arduino Opta WiFi
- - Source Code created on: 2025-10-05 15:22:15
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Detect a debounced push-button event on A0 using */
- /* EasyButton; the input uses INPUT_PULLUP, so a */
- /* pressed state is LOW and should trigger the system */
- /* logic. Integrate flash over the air. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h>
- #include <POTA.h>
- // If a secrets.h file is provided in the project, you may override these values by
- // defining the following macros in that file. If not present, defaults are used.
- #ifndef WIFI_SSID
- #define WIFI_SSID "your-ssid"
- #define WIFI_PASSWORD "your-password"
- #define DEVICE_TYPE "ARDUINO_OPTA_WIFI"
- #define FIRMWARE_VERSION "0.0.0"
- #define AUTH_TOKEN "your-auth-token"
- #define SERVER_SECRET "your-server-secret"
- #endif
- // Pointers to credentials used by POTA. These resolve to string literals defined above.
- const char* wifiSSID = WIFI_SSID;
- const char* wifiPassword = WIFI_PASSWORD;
- const char* deviceType = DEVICE_TYPE;
- const char* firmwareVersion = FIRMWARE_VERSION;
- const char* authToken = AUTH_TOKEN;
- const char* serverSecret = SERVER_SECRET;
- // Debounced push-button on A0
- static const uint8_t BUTTON_PIN_A0 = A0;
- EasyButton button(BUTTON_PIN_A0);
- // OTA controller instance
- POTA ota;
- // Callback invoked when the button is pressed
- void buttonPressed()
- {
- Serial.println("Button pressed");
- Serial.println("Checking for OTA update...");
- POTAError err = ota.checkAndPerformOTA();
- if (err == POTAError::NO_UPDATE_AVAILABLE)
- {
- Serial.println("No OTA update available");
- }
- else if (err != POTAError::SUCCESS)
- {
- Serial.print("OTA error: ");
- Serial.println(ota.errorToString(err));
- }
- else
- {
- Serial.println("OTA update finished or in progress");
- }
- }
- void setup(void)
- {
- // Initialize Serial for debug purposes
- Serial.begin(115200);
- while (!Serial) { /* wait for serial port */ }
- Serial.println();
- Serial.println(">>> POTA + EasyButton on A0 test <<<");
- // 1️⃣ Initialize the button on A0 with INPUT_PULLUP (pressed state = LOW)
- pinMode(BUTTON_PIN_A0, INPUT_PULLUP);
- button.begin();
- button.onPressed(buttonPressed);
- // 2️⃣ Initialize OTA client with credentials (from secrets.h if present)
- Serial.println("Initializing OTA...");
- POTAError otaInit = ota.begin(wifiSSID, wifiPassword, deviceType, firmwareVersion, authToken, serverSecret);
- if (otaInit != POTAError::SUCCESS)
- {
- Serial.print("OTA begin failed: ");
- Serial.println(ota.errorToString(otaInit));
- }
- else
- {
- Serial.println("OTA initialized");
- }
- // Optional: perform one-shot OTA check on startup
- POTAError initialCheck = ota.checkAndPerformOTA();
- if (initialCheck == POTAError::NO_UPDATE_AVAILABLE)
- {
- Serial.println("Firmware already up to date");
- }
- else if (initialCheck != POTAError::SUCCESS)
- {
- Serial.print("OTA error: ");
- Serial.println(ota.errorToString(initialCheck));
- }
- }
- void loop(void)
- {
- // Continuously read the status of the button to detect presses
- button.read();
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment