Skip to content

Commit

Permalink
Chane from pin low level to RelayModule
Browse files Browse the repository at this point in the history
  • Loading branch information
ronny-antoon committed Nov 20, 2023
1 parent ced2f8a commit 2ea5051
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
7 changes: 4 additions & 3 deletions include/UpdateOTA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <esp_ota_ops.h> // esp_ota_set_boot_partition
#include <HTTPClient.h> // HTTPClient And WiFiClientSecure
#include <MultiPrinterLoggerInterface.hpp> // MultiPrinterLoggerInterface
#include <RelayModuleInterface.hpp> // RelayModuleInterface

#include "UpdateOTAInterface.hpp"

Expand All @@ -17,7 +18,7 @@ class UpdateOTA : public UpdateOTAInterface
/**
* @brief Constructor
*/
UpdateOTA(MultiPrinterLoggerInterface *logger = nullptr);
UpdateOTA(MultiPrinterLoggerInterface *logger = nullptr, RelayModuleInterface *relayModule = nullptr);

/**
* @brief Destructor
Expand All @@ -40,7 +41,7 @@ class UpdateOTA : public UpdateOTAInterface
* UpdateOTAError::UPDATE_PROGRESS_ERROR - If there is an error in the update progress
* UpdateOTAError::PARTITION_NOT_BOOTABLE - If the selected partition is not bootable
*/
UpdateOTAError startUpdate(const char *uRL, bool isFirmware, uint8_t pinStatus = 0) override;
UpdateOTAError startUpdate(const char *uRL, bool isFirmware) override;

/**
* @brief Get the version number from the specified URL and store it in the provided buffer
Expand Down Expand Up @@ -141,6 +142,7 @@ class UpdateOTA : public UpdateOTAInterface
*/
void toggleLed();

RelayModuleInterface *_relayModule; ///< RelayModuleInterface instance for controlling an LED during the update
MultiPrinterLoggerInterface *_logger; ///< Logger for logging messages
WiFiClientSecure *_wifiClientSecure; ///< WiFiClientSecure instance for secure communication
HTTPClient *_httpClient; ///< HTTPClient instance for handling HTTP requests
Expand All @@ -149,7 +151,6 @@ class UpdateOTA : public UpdateOTAInterface
char _buffer[BLOCK_SIZE_P]; ///< Buffer for reading/writing data blocks
const esp_partition_t *_newPartition; ///< Pointer to the new partition for firmware update
bool _isFirmware; ///< Flag indicating whether the update is for firmware
uint8_t _pinStatus; ///< Pin status for controlling an LED during the update
char ca_cert[1400] = ///< Certificate data for secure communication
"-----BEGIN CERTIFICATE-----\n"
// (Certificate data goes here)
Expand Down
3 changes: 1 addition & 2 deletions include/UpdateOTAInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class UpdateOTAInterface
* @brief Start the OTA update process with the specified URL, update type, and pin status
* @param uRL The URL of the update
* @param isFirmware Flag indicating whether the update is for firmware
* @param pinStatus The pin status for controlling an LED during the update
* @return UpdateOTAError indicating the success or failure of the OTA update process, Options:-
* UpdateOTAError::SUCCESS - If the OTA update process completed successfully
* UpdateOTAError::NO_INTERNET - If there is no internet connection
Expand All @@ -45,7 +44,7 @@ class UpdateOTAInterface
* UpdateOTAError::NO_ENOUGH_SPACE - If there is insufficient space for the update
* UpdateOTAError::UNKNOWN - If there is an unknown error during update
*/
virtual UpdateOTAError startUpdate(const char *uRL, bool isFirmware, uint8_t pinStatus) = 0;
virtual UpdateOTAError startUpdate(const char *uRL, bool isFirmware) = 0;

/**
* @brief Get the version number from the specified URL and store it in the provided buffer
Expand Down
7 changes: 6 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "UpdateOTA",
"version": "4.1.0",
"version": "4.5.0",
"description": "The UpdateOTA Library streamlines the implementation of Over-The-Air firmware updates for ESP8266-based projects. It introduces an abstract class, *UpdateOTAInterface*, defining methods for starting updates, retrieving version numbers, and handling update errors. The library is designed to work seamlessly with ESP8266, Arduino, and other related libraries.",
"keywords": "UpdateOTA, OTA,over the air, update http, updater,arduino ota,firmware update,upload spiffs, ESP32, ronny, antoon, MetaHouse",
"repository": {
Expand All @@ -26,6 +26,11 @@
"owner": "ronny-antoon",
"name": "MultiPrinterLogger",
"version": "4.1.0"
},
{
"owner": "ronny-antoon",
"name": "RelayModule",
"version": "4.1.0"
}
]
}
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ monitor_raw = yes
board_build.partitions = partition.csv
lib_deps =
ronny-antoon/MultiPrinterLogger@^4.1.0
ronny-antoon/RelayModule@^4.1.0
google/googletest@^1.12.1
test_build_src = yes
build_type = test
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ The UpdateOTA Library streamlines the implementation of Over-The-Air firmware up

The UpdateOTA Library depends on the following libraries:
- MultiPrinterLogger @ 4.1.0
- RelayModule @ 4.1.0
- HTTPClient
- WiFi
- WiFiClientSecure
- esp_ota_ops.h
- esp_partition.h

Ensure that these dependencies are correctly configured in your project.

## Installation
Expand All @@ -51,7 +53,7 @@ To integrate the *UpdateOTA* library into your PlatformIO project, follow these

2. Add the following line to the `lib_deps` option under the `[env:]` section:
```cpp
ronny-antoon/UpdateOTA@^4.1.0
ronny-antoon/UpdateOTA@^4.5.0
```

3. Build your project, and PlatformIO will automatically handle library installation.
Expand Down
25 changes: 12 additions & 13 deletions src/UpdateOTA.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "UpdateOTA.hpp"

UpdateOTA::UpdateOTA(MultiPrinterLoggerInterface *logger) : _logger(logger)
UpdateOTA::UpdateOTA(MultiPrinterLoggerInterface *logger, RelayModuleInterface *relayModule)
: _logger(logger),
_relayModule(relayModule)
{
Log_Debug(_logger, "UpdateOTA constructor");
// Initialize member variables
_newPartition = nullptr;
_isFirmware = false;
_pinStatus = 0;
_uRL = nullptr;
_httpCode = 0;
_wifiClientSecure = nullptr;
Expand All @@ -30,12 +31,11 @@ UpdateOTA::~UpdateOTA()
}
}

UpdateOTAError UpdateOTA::startUpdate(const char *uRL, bool isFirmware, uint8_t pinStatus)
UpdateOTAError UpdateOTA::startUpdate(const char *uRL, bool isFirmware)
{
// Set member variables based on input parameters
_uRL = uRL;
_isFirmware = isFirmware;
_pinStatus = pinStatus;

// Check if the device is connected to the internet
if (WiFi.status() != WL_CONNECTED)
Expand Down Expand Up @@ -182,11 +182,9 @@ UpdateOTAError UpdateOTA::processGetRequest()
UpdateOTAError UpdateOTA::updateFirmware()
{
// Update the firmware
if (_pinStatus != 0)
{
pinMode(_pinStatus, OUTPUT); // Set the pin to output mode.
digitalWrite(_pinStatus, HIGH); // Turn on the LED.
}
if (_relayModule != nullptr)
_relayModule->turnOn();

size_t written = 0; // Variable to keep track of the number of bytes written.
size_t toWrite = 0; // Variable to keep track of the number of bytes to write.
uint32_t _streamLength = _httpClient->getSize();
Expand All @@ -212,8 +210,9 @@ UpdateOTAError UpdateOTA::updateFirmware()

printProgress(written, _streamLength); // Print the progress.
_httpClient->end(); // Close the input stream.
if (_pinStatus != 0)
digitalWrite(_pinStatus, LOW); // Turn off the LED.

if (_relayModule != nullptr)
_relayModule->turnOff();

if (_streamLength != written)
return UpdateOTAError::UPDATE_PROGRESS_ERROR; // Check if the number of bytes written is equal to the stream length. If not return an error.
Expand Down Expand Up @@ -291,6 +290,6 @@ void UpdateOTA::printProgress(size_t written, size_t total)
void UpdateOTA::toggleLed()
{
// Toggle the LED
if (_pinStatus != 0)
digitalWrite(_pinStatus, !digitalRead(_pinStatus));
if (_relayModule != nullptr)
_relayModule->toggle();
}

0 comments on commit 2ea5051

Please sign in to comment.