CCCPaste

OTA Updater rev_12

/********* 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: OTA Updater
	- Source Code compiled for: Arduino Opta WiFi
	- Source Code created on: 2025-10-05 15:11:13

********* 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

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

/****** GLOBAL VARIABLES *****/
POTA ota; // POTA OTA controller
EasyButton button(button_PushButton_PIN_A0); // Button attached to A0

volatile bool ota_update_requested = false;

// Forward declaration (not necessary, but kept for clarity)
void onOTARequest();

void onOTARequest()
{
  // Set a flag to perform OTA in the main loop
  ota_update_requested = true;
  Serial.println("OTA update requested via button press");
}

void setup()
{
  Serial.begin(115200);
  // Wait for Serial to be ready (optional on some boards)
  // while (!Serial) { ; }
  Serial.println();
  Serial.println("POTA OTA over-the-air demo with EasyButton on A0");

  // Initialize the button with INPUT_PULLUP
  pinMode(button_PushButton_PIN_A0, INPUT_PULLUP);
  button.begin();
  button.onPressed(onOTARequest);

  // Initialize POTA with empty credentials (placeholders).
  // Users should replace the empty strings with real secrets.
  POTAError err = ota.begin("", "", "", "", "", "");
  if (err != POTAError::SUCCESS)
  {
    Serial.print("POTA begin failed: ");
    Serial.println(ota.errorToString(err));
  }
  else
  {
    Serial.println("POTA initialized (placeholders)");
    // Optional: perform an initial OTA check
    err = ota.checkAndPerformOTA();
    if (err == POTAError::NO_UPDATE_AVAILABLE)
    {
      Serial.println("No OTA update available");
    }
    else if (err != POTAError::SUCCESS)
    {
      Serial.print("OTA check error: ");
      Serial.println(ota.errorToString(err));
    }
  }
}

void loop()
{
  // Process button state
  button.read();

  // If user requested an OTA check, perform it
  if (ota_update_requested)
  {
    ota_update_requested = false;
    POTAError err = ota.checkAndPerformOTA();
    if (err == POTAError::NO_UPDATE_AVAILABLE)
    {
      Serial.println("OTA: Firmware already up to date");
    }
    else if (err != POTAError::SUCCESS)
    {
      Serial.print("OTA error: ");
      Serial.println(ota.errorToString(err));
    }
  }
}

/* END CODE */