pleasedontcode

Debounced OTA rev_01

Oct 5th, 2025
64
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: Debounced OTA
  13.     - Source Code NOT compiled for: Arduino Opta WiFi
  14.     - Source Code created on: 2025-10-05 11:14:23
  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>  //https://github.com/evert-arias/EasyButton
  31. #include <POTA.h> //https://github.com/pleasedontcode/POTA
  32.  
  33. #if defined(ENABLE_OTA) && (ENABLE_OTA == 1)
  34. #include "secrets.h" // Contains WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET
  35. POTA ota;
  36. bool ota_initialized = false;
  37. #endif
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. void buttonPressed(void);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t button_PushButton_PIN_A0 = A0;
  47.  
  48. // Instance of the button.
  49. EasyButton button(button_PushButton_PIN_A0);
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // (No additional instances defined in this minimal test sketch)
  53.  
  54.  
  55. /****** FUNCTION IMPLEMENTATIONS *****/
  56.  
  57. void buttonPressed(void)
  58. {
  59.   Serial.println("Button pressed (debounced)");
  60.  
  61. #if defined(ENABLE_OTA) && (ENABLE_OTA == 1)
  62.   if (!ota_initialized)
  63.   {
  64.     POTAError err = ota.begin(WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET);
  65.     if (err == POTAError::SUCCESS)
  66.     {
  67.       ota_initialized = true;
  68.       Serial.println("OTA initialized");
  69.     }
  70.     else
  71.     {
  72.       Serial.print("OTA begin failed: ");
  73.       Serial.println(ota.errorToString(err));
  74.       return;
  75.     }
  76.   }
  77.  
  78.   if (ota_initialized)
  79.   {
  80.     POTAError err = ota.checkAndPerformOTA();
  81.     if (err == POTAError::NO_UPDATE_AVAILABLE)
  82.     {
  83.       Serial.println("OTA: No update available");
  84.     }
  85.     else if (err != POTAError::SUCCESS)
  86.     {
  87.       Serial.print("OTA error: ");
  88.       Serial.println(ota.errorToString(err));
  89.     }
  90.   }
  91. #else
  92.   Serial.println("OTA integration not enabled (ENABLE_OTA not defined).\n");
  93. #endif
  94. }
  95.  
  96. void setup(void)
  97. {
  98.   // put your setup code here, to run once:
  99.   Serial.begin(115200);
  100.   Serial.println();
  101.   Serial.println(">>> POTATest with EasyButton and optional OTA <<<");
  102.  
  103.   pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);
  104.  
  105.   // Initialize the button
  106.   button.begin();
  107.   button.onPressed(buttonPressed);
  108. }
  109.  
  110. void loop(void)
  111. {
  112.   // put your main code here, to run repeatedly:
  113.   button.read();
  114. }
  115.  
  116. /* END CODE */
  117.  
Advertisement
Add Comment
Please, Sign In to add comment