pleasedontcode

OTA Initialization rev_13

Oct 5th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: OTA Initialization
  13.     - Source Code NOT compiled for: Arduino Opta WiFi
  14.     - Source Code created on: 2025-10-05 15:22:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Detect a debounced push-button event on A0 using */
  21.     /* EasyButton; the input uses INPUT_PULLUP, so a */
  22.     /* pressed state is LOW and should trigger the system */
  23.     /* logic. Integrate flash over the air. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h>
  29. #include <POTA.h>
  30.  
  31. // If a secrets.h file is provided in the project, you may override these values by
  32. // defining the following macros in that file. If not present, defaults are used.
  33. #ifndef WIFI_SSID
  34. #define WIFI_SSID "your-ssid"
  35. #define WIFI_PASSWORD "your-password"
  36. #define DEVICE_TYPE "ARDUINO_OPTA_WIFI"
  37. #define FIRMWARE_VERSION "0.0.0"
  38. #define AUTH_TOKEN "your-auth-token"
  39. #define SERVER_SECRET "your-server-secret"
  40. #endif
  41.  
  42. // Pointers to credentials used by POTA. These resolve to string literals defined above.
  43. const char* wifiSSID = WIFI_SSID;
  44. const char* wifiPassword = WIFI_PASSWORD;
  45. const char* deviceType = DEVICE_TYPE;
  46. const char* firmwareVersion = FIRMWARE_VERSION;
  47. const char* authToken = AUTH_TOKEN;
  48. const char* serverSecret = SERVER_SECRET;
  49.  
  50. // Debounced push-button on A0
  51. static const uint8_t BUTTON_PIN_A0 = A0;
  52. EasyButton button(BUTTON_PIN_A0);
  53.  
  54. // OTA controller instance
  55. POTA ota;
  56.  
  57. // Callback invoked when the button is pressed
  58. void buttonPressed()
  59. {
  60.   Serial.println("Button pressed");
  61.   Serial.println("Checking for OTA update...");
  62.   POTAError err = ota.checkAndPerformOTA();
  63.   if (err == POTAError::NO_UPDATE_AVAILABLE)
  64.   {
  65.     Serial.println("No OTA update available");
  66.   }
  67.   else if (err != POTAError::SUCCESS)
  68.   {
  69.     Serial.print("OTA error: ");
  70.     Serial.println(ota.errorToString(err));
  71.   }
  72.   else
  73.   {
  74.     Serial.println("OTA update finished or in progress");
  75.   }
  76. }
  77.  
  78. void setup(void)
  79. {
  80.   // Initialize Serial for debug purposes
  81.   Serial.begin(115200);
  82.   while (!Serial) { /* wait for serial port */ }
  83.   Serial.println();
  84.   Serial.println(">>> POTA + EasyButton on A0 test <<<");
  85.  
  86.   // 1️⃣ Initialize the button on A0 with INPUT_PULLUP (pressed state = LOW)
  87.   pinMode(BUTTON_PIN_A0, INPUT_PULLUP);
  88.   button.begin();
  89.   button.onPressed(buttonPressed);
  90.  
  91.   // 2️⃣ Initialize OTA client with credentials (from secrets.h if present)
  92.   Serial.println("Initializing OTA...");
  93.   POTAError otaInit = ota.begin(wifiSSID, wifiPassword, deviceType, firmwareVersion, authToken, serverSecret);
  94.   if (otaInit != POTAError::SUCCESS)
  95.   {
  96.     Serial.print("OTA begin failed: ");
  97.     Serial.println(ota.errorToString(otaInit));
  98.   }
  99.   else
  100.   {
  101.     Serial.println("OTA initialized");
  102.   }
  103.  
  104.   // Optional: perform one-shot OTA check on startup
  105.   POTAError initialCheck = ota.checkAndPerformOTA();
  106.   if (initialCheck == POTAError::NO_UPDATE_AVAILABLE)
  107.   {
  108.     Serial.println("Firmware already up to date");
  109.   }
  110.   else if (initialCheck != POTAError::SUCCESS)
  111.   {
  112.     Serial.print("OTA error: ");
  113.     Serial.println(ota.errorToString(initialCheck));
  114.   }
  115. }
  116.  
  117. void loop(void)
  118. {
  119.   // Continuously read the status of the button to detect presses
  120.   button.read();
  121. }
  122.  
  123. /* END CODE */
  124.  
Advertisement
Add Comment
Please, Sign In to add comment