pleasedontcode

OTA Button rev_11

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