CCCPaste

Debounced OTA rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Debounced OTA
	- Source Code NOT compiled for: Arduino Opta WiFi
	- Source Code created on: 2025-10-05 11:32:29

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Detect a debounced push-button event on A0 using */
	/* EasyButton; the input uses INPUT_PULLUP, so a */
	/* pressed state is LOW and should trigger the system */
	/* logic. Integrate flash over the air. */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 

#if __has_include("POTA.h")
#include 
#define POTA_AVAILABLE 1
#else
#define POTA_AVAILABLE 0
#endif

#if __has_include("secrets.h")
#include "secrets.h"
#define SECRETS_AVAILABLE 1
#else
#define SECRETS_AVAILABLE 0
#endif

#if POTA_AVAILABLE && SECRETS_AVAILABLE
POTA ota;
#endif

/** Pin definitions **/
const uint8_t button_PushButton_PIN_A0 = A0;

// Instance of EasyButton for the debounced push-button on A0
EasyButton button(button_PushButton_PIN_A0);

/** Prototypes **/
void onButtonPressed();
void performOTAIfConfigured();

void setup(void);
void loop(void);

void setup(void)
{
  // Initialize serial for debugging
  Serial.begin(115200);
  // Small delay to ensure serial is ready
  delay(100);

  Serial.println("POTA EasyButton debounced button on A0 - OTA integration test");

  // Use INPUT_PULLUP for the push-button as required by the system requirements
  pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);

  // Initialize the button handling
  button.begin();
  button.onPressed(onButtonPressed);

  #if POTA_AVAILABLE && SECRETS_AVAILABLE
  Serial.println("POTA and secrets detected. Initializing OTA may occur on button press.");
  // Initialize OTA if secrets are available. The exact call depends on library usage
  ota.begin(WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET);
  #endif
}

void loop(void)
{
  // Regularly update the button state (debounce handled internally by EasyButton)
  button.read();

  #if POTA_AVAILABLE && SECRETS_AVAILABLE
  // Optional: allow OTA background processing if the library supports it
  ota.checkAndPerformOTA();
  #endif
}

void onButtonPressed()
{
  Serial.println("Button pressed (A0) - debounced.");
  performOTAIfConfigured();
}

void performOTAIfConfigured()
{
  #if POTA_AVAILABLE && SECRETS_AVAILABLE
  Serial.println("Starting OTA check (POTA)...");
  ota.checkAndPerformOTA();
  #else
  Serial.println("OTA not configured. Include POTA.h and secrets.h to enable OTA.");
  #endif
}

/* END CODE */