pleasedontcode

Debounced OTA rev_02

Oct 5th, 2025
118
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:32:29
  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>
  31.  
  32. #if __has_include("POTA.h")
  33. #include <POTA.h>
  34. #define POTA_AVAILABLE 1
  35. #else
  36. #define POTA_AVAILABLE 0
  37. #endif
  38.  
  39. #if __has_include("secrets.h")
  40. #include "secrets.h"
  41. #define SECRETS_AVAILABLE 1
  42. #else
  43. #define SECRETS_AVAILABLE 0
  44. #endif
  45.  
  46. #if POTA_AVAILABLE && SECRETS_AVAILABLE
  47. POTA ota;
  48. #endif
  49.  
  50. /** Pin definitions **/
  51. const uint8_t button_PushButton_PIN_A0 = A0;
  52.  
  53. // Instance of EasyButton for the debounced push-button on A0
  54. EasyButton button(button_PushButton_PIN_A0);
  55.  
  56. /** Prototypes **/
  57. void onButtonPressed();
  58. void performOTAIfConfigured();
  59.  
  60. void setup(void);
  61. void loop(void);
  62.  
  63. void setup(void)
  64. {
  65.   // Initialize serial for debugging
  66.   Serial.begin(115200);
  67.   // Small delay to ensure serial is ready
  68.   delay(100);
  69.  
  70.   Serial.println("POTA EasyButton debounced button on A0 - OTA integration test");
  71.  
  72.   // Use INPUT_PULLUP for the push-button as required by the system requirements
  73.   pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);
  74.  
  75.   // Initialize the button handling
  76.   button.begin();
  77.   button.onPressed(onButtonPressed);
  78.  
  79.   #if POTA_AVAILABLE && SECRETS_AVAILABLE
  80.   Serial.println("POTA and secrets detected. Initializing OTA may occur on button press.");
  81.   // Initialize OTA if secrets are available. The exact call depends on library usage
  82.   ota.begin(WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET);
  83.   #endif
  84. }
  85.  
  86. void loop(void)
  87. {
  88.   // Regularly update the button state (debounce handled internally by EasyButton)
  89.   button.read();
  90.  
  91.   #if POTA_AVAILABLE && SECRETS_AVAILABLE
  92.   // Optional: allow OTA background processing if the library supports it
  93.   ota.checkAndPerformOTA();
  94.   #endif
  95. }
  96.  
  97. void onButtonPressed()
  98. {
  99.   Serial.println("Button pressed (A0) - debounced.");
  100.   performOTAIfConfigured();
  101. }
  102.  
  103. void performOTAIfConfigured()
  104. {
  105.   #if POTA_AVAILABLE && SECRETS_AVAILABLE
  106.   Serial.println("Starting OTA check (POTA)...");
  107.   ota.checkAndPerformOTA();
  108.   #else
  109.   Serial.println("OTA not configured. Include POTA.h and secrets.h to enable OTA.");
  110.   #endif
  111. }
  112.  
  113. /* END CODE */
  114.  
Advertisement
Add Comment
Please, Sign In to add comment