mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-28 16:07:16 +02:00
Integrate ota
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
|||||||
[submodule "src/Libraries/airgradient-client"]
|
[submodule "src/Libraries/airgradient-client"]
|
||||||
path = src/Libraries/airgradient-client
|
path = src/Libraries/airgradient-client
|
||||||
url = git@github.com:airgradienthq/airgradient-client.git
|
url = git@github.com:airgradienthq/airgradient-client.git
|
||||||
|
[submodule "src/Libraries/airgradient-ota"]
|
||||||
|
path = src/Libraries/airgradient-ota
|
||||||
|
url = git@github.com:airgradienthq/airgradient-ota.git
|
||||||
|
@ -37,21 +37,25 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "EEPROM.h"
|
#include "EEPROM.h"
|
||||||
#include "ESPmDNS.h"
|
#include "ESPmDNS.h"
|
||||||
#include "Libraries/airgradient-client/src/cellularModule.h"
|
#include "Libraries/airgradient-ota/src/airgradientOta.h"
|
||||||
#include "LocalServer.h"
|
#include "LocalServer.h"
|
||||||
#include "MqttClient.h"
|
#include "MqttClient.h"
|
||||||
#include "OpenMetrics.h"
|
#include "OpenMetrics.h"
|
||||||
#include "OtaHandler.h"
|
|
||||||
#include "WebServer.h"
|
#include "WebServer.h"
|
||||||
#include "esp32c3/rom/rtc.h"
|
#include "esp32c3/rom/rtc.h"
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
#include <WebServer.h>
|
#include <WebServer.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "Libraries/airgradient-client/src/agSerial.h"
|
#include "Libraries/airgradient-client/src/agSerial.h"
|
||||||
|
#include "Libraries/airgradient-client/src/cellularModule.h"
|
||||||
#include "Libraries/airgradient-client/src/cellularModuleA7672xx.h"
|
#include "Libraries/airgradient-client/src/cellularModuleA7672xx.h"
|
||||||
#include "Libraries/airgradient-client/src/airgradientCellularClient.h"
|
#include "Libraries/airgradient-client/src/airgradientCellularClient.h"
|
||||||
#include "Libraries/airgradient-client/src/airgradientWifiClient.h"
|
#include "Libraries/airgradient-client/src/airgradientWifiClient.h"
|
||||||
|
#include "Libraries/airgradient-ota/src/airgradientOtaWifi.h"
|
||||||
|
#include "Libraries/airgradient-ota/src/airgradientOtaCellular.h"
|
||||||
|
#include "esp_system.h"
|
||||||
#include "freertos/projdefs.h"
|
#include "freertos/projdefs.h"
|
||||||
|
|
||||||
#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
|
#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
|
||||||
@ -94,7 +98,6 @@ static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine,
|
|||||||
configuration);
|
configuration);
|
||||||
static OpenMetrics openMetrics(measurements, configuration, wifiConnector,
|
static OpenMetrics openMetrics(measurements, configuration, wifiConnector,
|
||||||
apiClient);
|
apiClient);
|
||||||
static OtaHandler otaHandler;
|
|
||||||
static LocalServer localServer(Serial, openMetrics, measurements, configuration,
|
static LocalServer localServer(Serial, openMetrics, measurements, configuration,
|
||||||
wifiConnector);
|
wifiConnector);
|
||||||
static AgSerial *agSerial;
|
static AgSerial *agSerial;
|
||||||
@ -137,8 +140,8 @@ static void wdgFeedUpdate(void);
|
|||||||
static void ledBarEnabledUpdate(void);
|
static void ledBarEnabledUpdate(void);
|
||||||
static bool sgp41Init(void);
|
static bool sgp41Init(void);
|
||||||
static void checkForFirmwareUpdate(void);
|
static void checkForFirmwareUpdate(void);
|
||||||
static void otaHandlerCallback(OtaHandler::OtaState state, String mesasge);
|
static void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg);
|
||||||
static void displayExecuteOta(OtaHandler::OtaState state, String msg, int processing);
|
static void displayExecuteOta(AirgradientOTA::OtaResult result, String msg, int processing);
|
||||||
static int calculateMaxPeriod(int updateInterval);
|
static int calculateMaxPeriod(int updateInterval);
|
||||||
static void setMeasurementMaxPeriod();
|
static void setMeasurementMaxPeriod();
|
||||||
static void newMeasurementCycle();
|
static void newMeasurementCycle();
|
||||||
@ -337,9 +340,6 @@ void loop() {
|
|||||||
|
|
||||||
/** check that local configuration changed then do some action */
|
/** check that local configuration changed then do some action */
|
||||||
configUpdateHandle();
|
configUpdateHandle();
|
||||||
|
|
||||||
/** Firmware check for update handle */
|
|
||||||
// checkForUpdateSchedule.run(); //! Temporary until ota cellular
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void co2Update(void) {
|
static void co2Update(void) {
|
||||||
@ -520,47 +520,48 @@ static bool sgp41Init(void) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void checkForFirmwareUpdate(void) {
|
void checkForFirmwareUpdate(void) {
|
||||||
Serial.println();
|
AirgradientOTA *agOta;
|
||||||
Serial.print("checkForFirmwareUpdate: ");
|
if (networkOption == UseWifi) {
|
||||||
|
agOta = new AirgradientOTAWifi;
|
||||||
if (configuration.isOfflineMode() || configuration.isCloudConnectionDisabled()) {
|
} else {
|
||||||
Serial.println("mode is offline or cloud connection disabled, ignored");
|
agOta = new AirgradientOTACellular(cell);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (!wifiConnector.isConnected()) {
|
// TODO: This might be too long and new measurement cycle queue will be big
|
||||||
// Serial.println("wifi not connected, ignored");
|
Serial.println("Check for firmware update");
|
||||||
// return;
|
agOta->setHandlerCallback(otaHandlerCallback);
|
||||||
// }
|
agOta->updateIfAvailable(ag->getDeviceId(), GIT_VERSION);
|
||||||
|
// agOta->updateIfAvailable("aabbccddeeff", GIT_VERSION);
|
||||||
Serial.println("perform");
|
delete agOta;
|
||||||
otaHandler.setHandlerCallback(otaHandlerCallback);
|
|
||||||
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void otaHandlerCallback(OtaHandler::OtaState state, String message) {
|
void otaHandlerCallback(AirgradientOTA::OtaResult result, const char *msg) {
|
||||||
Serial.println("OTA message: " + message);
|
switch (result) {
|
||||||
switch (state) {
|
case AirgradientOTA::Starting:
|
||||||
case OtaHandler::OTA_STATE_BEGIN:
|
displayExecuteOta(result, fwNewVersion, 0);
|
||||||
displayExecuteOta(state, fwNewVersion, 0);
|
|
||||||
break;
|
break;
|
||||||
case OtaHandler::OTA_STATE_FAIL:
|
case AirgradientOTA::InProgress:
|
||||||
displayExecuteOta(state, "", 0);
|
displayExecuteOta(result, "", std::stoi(msg));
|
||||||
break;
|
break;
|
||||||
case OtaHandler::OTA_STATE_PROCESSING:
|
case AirgradientOTA::Failed:
|
||||||
case OtaHandler::OTA_STATE_SUCCESS:
|
case AirgradientOTA::Skipped:
|
||||||
displayExecuteOta(state, "", message.toInt());
|
case AirgradientOTA::AlreadyUpToDate:
|
||||||
|
displayExecuteOta(result, "", 0);
|
||||||
|
break;
|
||||||
|
case AirgradientOTA::Success:
|
||||||
|
displayExecuteOta(result, "", 0);
|
||||||
|
esp_restart();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void displayExecuteOta(OtaHandler::OtaState state, String msg, int processing) {
|
static void displayExecuteOta(AirgradientOTA::OtaResult result, String msg, int processing) {
|
||||||
switch (state) {
|
switch (result) {
|
||||||
case OtaHandler::OTA_STATE_BEGIN: {
|
case AirgradientOTA::Starting:
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
oledDisplay.showFirmwareUpdateVersion(msg);
|
oledDisplay.showFirmwareUpdateVersion(msg);
|
||||||
} else {
|
} else {
|
||||||
@ -568,49 +569,40 @@ static void displayExecuteOta(OtaHandler::OtaState state, String msg, int proces
|
|||||||
}
|
}
|
||||||
delay(2500);
|
delay(2500);
|
||||||
break;
|
break;
|
||||||
}
|
case AirgradientOTA::Failed:
|
||||||
case OtaHandler::OTA_STATE_FAIL: {
|
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
oledDisplay.showFirmwareUpdateFailed();
|
oledDisplay.showFirmwareUpdateFailed();
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Error: Firmware update: failed");
|
Serial.println("Error: Firmware update: failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(2500);
|
delay(2500);
|
||||||
break;
|
break;
|
||||||
}
|
case AirgradientOTA::Skipped:
|
||||||
case OtaHandler::OTA_STATE_SKIP: {
|
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
oledDisplay.showFirmwareUpdateSkipped();
|
oledDisplay.showFirmwareUpdateSkipped();
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Firmware update: Skipped");
|
Serial.println("Firmware update: Skipped");
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(2500);
|
delay(2500);
|
||||||
break;
|
break;
|
||||||
}
|
case AirgradientOTA::AlreadyUpToDate:
|
||||||
case OtaHandler::OTA_STATE_UP_TO_DATE: {
|
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
oledDisplay.showFirmwareUpdateUpToDate();
|
oledDisplay.showFirmwareUpdateUpToDate();
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Firmware update: up to date");
|
Serial.println("Firmware update: up to date");
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(2500);
|
delay(2500);
|
||||||
break;
|
break;
|
||||||
}
|
case AirgradientOTA::InProgress:
|
||||||
case OtaHandler::OTA_STATE_PROCESSING: {
|
|
||||||
if (ag->isOne()) {
|
if (ag->isOne()) {
|
||||||
oledDisplay.showFirmwareUpdateProgress(processing);
|
oledDisplay.showFirmwareUpdateProgress(processing);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Firmware update: " + String(processing) + String("%"));
|
Serial.println("Firmware update: " + String(processing) + String("%"));
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
case AirgradientOTA::Success: {
|
||||||
case OtaHandler::OTA_STATE_SUCCESS: {
|
|
||||||
int i = 6;
|
int i = 6;
|
||||||
while(i != 0) {
|
while (i != 0) {
|
||||||
i = i - 1;
|
i = i - 1;
|
||||||
Serial.println("OTA update performed, restarting ...");
|
Serial.println("OTA update performed, restarting ...");
|
||||||
int i = 6;
|
int i = 6;
|
||||||
@ -625,7 +617,6 @@ static void displayExecuteOta(OtaHandler::OtaState state, String msg, int proces
|
|||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
oledDisplay.setBrightness(0);
|
oledDisplay.setBrightness(0);
|
||||||
esp_restart();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -966,8 +957,8 @@ void initializeNetwork() {
|
|||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
// ota not supported
|
// ota not supported
|
||||||
#else
|
#else
|
||||||
// checkForFirmwareUpdate(); // FIX: Temporary until ota cellular
|
checkForFirmwareUpdate();
|
||||||
// checkForUpdateSchedule.update();
|
checkForUpdateSchedule.update();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string config = agClient->httpFetchConfig(ag->getDeviceId());
|
std::string config = agClient->httpFetchConfig(ag->getDeviceId());
|
||||||
@ -999,11 +990,6 @@ static void configurationUpdateSchedule(void) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (wifiConnector.isConnected() == false) {
|
|
||||||
// Serial.println(" WiFi not connected, skipping fetch configuration from AG server");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
std::string config = agClient->httpFetchConfig(ag->getDeviceId());
|
std::string config = agClient->httpFetchConfig(ag->getDeviceId());
|
||||||
if (agClient->isLastFetchConfigSucceed() && configuration.parse(config.c_str(), false)) {
|
if (agClient->isLastFetchConfigSucceed() && configuration.parse(config.c_str(), false)) {
|
||||||
configUpdateHandle();
|
configUpdateHandle();
|
||||||
@ -1443,6 +1429,7 @@ void networkingTask(void *args) {
|
|||||||
// Run scheduler
|
// Run scheduler
|
||||||
configSchedule.run();
|
configSchedule.run();
|
||||||
transmissionSchedule.run();
|
transmissionSchedule.run();
|
||||||
|
checkForUpdateSchedule.run();
|
||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
1
src/Libraries/airgradient-ota
Submodule
1
src/Libraries/airgradient-ota
Submodule
Submodule src/Libraries/airgradient-ota added at 71a25be14f
Reference in New Issue
Block a user