CCCPaste

Debounced OTA rev_01

/********* 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:14:23

********* 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   //https://github.com/evert-arias/EasyButton
#include  //https://github.com/pleasedontcode/POTA

#if defined(ENABLE_OTA) && (ENABLE_OTA == 1)
#include "secrets.h" // Contains WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET
POTA ota;
bool ota_initialized = false;
#endif

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

void buttonPressed(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_A0 = A0;

// Instance of the button.
EasyButton button(button_PushButton_PIN_A0);

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// (No additional instances defined in this minimal test sketch)


/****** FUNCTION IMPLEMENTATIONS *****/

void buttonPressed(void)
{
  Serial.println("Button pressed (debounced)");

#if defined(ENABLE_OTA) && (ENABLE_OTA == 1)
  if (!ota_initialized)
  {
    POTAError err = ota.begin(WIFI_SSID, WIFI_PASSWORD, DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET);
    if (err == POTAError::SUCCESS)
    {
      ota_initialized = true;
      Serial.println("OTA initialized");
    }
    else
    {
      Serial.print("OTA begin failed: ");
      Serial.println(ota.errorToString(err));
      return;
    }
  }

  if (ota_initialized)
  {
    POTAError err = ota.checkAndPerformOTA();
    if (err == POTAError::NO_UPDATE_AVAILABLE)
    {
      Serial.println("OTA: No update available");
    }
    else if (err != POTAError::SUCCESS)
    {
      Serial.print("OTA error: ");
      Serial.println(ota.errorToString(err));
    }
  }
#else
  Serial.println("OTA integration not enabled (ENABLE_OTA not defined).\n");
#endif
}

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.println(">>> POTATest with EasyButton and optional OTA <<<");

  pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);

  // Initialize the button
  button.begin();
  button.onPressed(buttonPressed);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  button.read();
}

/* END CODE */