pleasedontcode

OTA Updater rev_12

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 Updater
  13.     - Source Code compiled for: Arduino Opta WiFi
  14.     - Source Code created on: 2025-10-05 15:11:13
  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.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  32. #include <POTA.h>         //https://github.com/pleasedontcode/POTA
  33.  
  34. /****** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t button_PushButton_PIN_A0 = A0;
  36.  
  37. /****** GLOBAL VARIABLES *****/
  38. POTA ota; // POTA OTA controller
  39. EasyButton button(button_PushButton_PIN_A0); // Button attached to A0
  40.  
  41. volatile bool ota_update_requested = false;
  42.  
  43. // Forward declaration (not necessary, but kept for clarity)
  44. void onOTARequest();
  45.  
  46. void onOTARequest()
  47. {
  48.   // Set a flag to perform OTA in the main loop
  49.   ota_update_requested = true;
  50.   Serial.println("OTA update requested via button press");
  51. }
  52.  
  53. void setup()
  54. {
  55.   Serial.begin(115200);
  56.   // Wait for Serial to be ready (optional on some boards)
  57.   // while (!Serial) { ; }
  58.   Serial.println();
  59.   Serial.println("POTA OTA over-the-air demo with EasyButton on A0");
  60.  
  61.   // Initialize the button with INPUT_PULLUP
  62.   pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);
  63.   button.begin();
  64.   button.onPressed(onOTARequest);
  65.  
  66.   // Initialize POTA with empty credentials (placeholders).
  67.   // Users should replace the empty strings with real secrets.
  68.   POTAError err = ota.begin("", "", "", "", "", "");
  69.   if (err != POTAError::SUCCESS)
  70.   {
  71.     Serial.print("POTA begin failed: ");
  72.     Serial.println(ota.errorToString(err));
  73.   }
  74.   else
  75.   {
  76.     Serial.println("POTA initialized (placeholders)");
  77.     // Optional: perform an initial OTA check
  78.     err = ota.checkAndPerformOTA();
  79.     if (err == POTAError::NO_UPDATE_AVAILABLE)
  80.     {
  81.       Serial.println("No OTA update available");
  82.     }
  83.     else if (err != POTAError::SUCCESS)
  84.     {
  85.       Serial.print("OTA check error: ");
  86.       Serial.println(ota.errorToString(err));
  87.     }
  88.   }
  89. }
  90.  
  91. void loop()
  92. {
  93.   // Process button state
  94.   button.read();
  95.  
  96.   // If user requested an OTA check, perform it
  97.   if (ota_update_requested)
  98.   {
  99.     ota_update_requested = false;
  100.     POTAError err = ota.checkAndPerformOTA();
  101.     if (err == POTAError::NO_UPDATE_AVAILABLE)
  102.     {
  103.       Serial.println("OTA: Firmware already up to date");
  104.     }
  105.     else if (err != POTAError::SUCCESS)
  106.     {
  107.       Serial.print("OTA error: ");
  108.       Serial.println(ota.errorToString(err));
  109.     }
  110.   }
  111. }
  112.  
  113. /* END CODE */
  114.  
Advertisement
Add Comment
Please, Sign In to add comment