Merge branch 'develop' into feature/press-button-for-offline-mode

This commit is contained in:
Phat Nguyen
2024-05-09 14:48:30 +07:00
committed by GitHub
8 changed files with 283 additions and 8 deletions

View File

@ -51,6 +51,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#include "OpenMetrics.h"
#include "WebServer.h"
#include <WebServer.h>
#include <WiFi.h>
#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
#define DISP_UPDATE_INTERVAL 2500 /** ms */
@ -92,6 +93,7 @@ static int getCO2FailCount = 0;
static AgFirmwareMode fwMode = FW_MODE_I_9PSL;
static bool ledBarButtonTest = false;
static String fwNewVersion;
static void boardInit(void);
static void failedHandler(String msg);
@ -111,6 +113,9 @@ static void factoryConfigReset(void);
static void wdgFeedUpdate(void);
static void ledBarEnabledUpdate(void);
static bool sgp41Init(void);
static void otaHandlerCallback(OtaState state, String mesasge);
static void displayExecuteOta(OtaState state, String msg,
int processing);
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, oledDisplayLedBarSchedule);
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL,
@ -162,6 +167,11 @@ void setup() {
if (ag->isOne()) {
if (ledBarButtonTest) {
stateMachine.executeLedBarPowerUpTest();
if (ag->button.getState() == PushButton::BUTTON_PRESSED) {
WiFi.begin("airgradient", "cleanair");
Serial.println("WiFi Credential reset to factory defaults");
ESP.restart();
}
} else {
ledBarEnabledUpdate();
}
@ -205,7 +215,7 @@ void setup() {
#ifdef ESP8266
// ota not supported
#else
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
// otaHandler.updateFirmwareIfOutdated(ag->deviceId());
#endif
apiClient.fetchServerConfiguration();
@ -452,6 +462,80 @@ static bool sgp41Init(void) {
return false;
}
static void otaHandlerCallback(OtaState state, String mesasge) {
switch (state) {
case OtaState::OTA_STATE_BEGIN:
displayExecuteOta(state, fwNewVersion, 0);
break;
case OtaState::OTA_STATE_FAIL:
displayExecuteOta(state, "", 0);
break;
case OtaState::OTA_STATE_PROCESSING:
displayExecuteOta(state, "", mesasge.toInt());
break;
case OtaState::OTA_STATE_SUCCESS:
displayExecuteOta(state, "", mesasge.toInt());
break;
default:
break;
}
}
static void displayExecuteOta(OtaState state, String msg, int processing) {
switch (state) {
case OtaState::OTA_STATE_BEGIN: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareVersion(msg);
} else {
Serial.println("New firmware: " + msg);
}
delay(2500);
break;
}
case OtaState::OTA_STATE_FAIL: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareFailed();
} else {
Serial.println("Error: Firmware update: failed");
}
delay(2500);
break;
}
case OtaState::OTA_STATE_PROCESSING: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareUpdating(String(processing));
} else {
Serial.println("Firmware update: " + String(processing) + String("%"));
}
break;
}
case OtaState::OTA_STATE_SUCCESS: {
int i = 6;
while(i != 0) {
i = i - 1;
Serial.println("OTA update performed, restarting ...");
int i = 6;
while (i != 0) {
i = i - 1;
if (ag->isOne()) {
oledDisplay.showNewFirmwareSuccess(String(i));
} else {
Serial.println("Rebooting... " + String(i));
}
delay(1000);
}
esp_restart();
}
break;
}
default:
break;
}
}
static void sendDataToAg() {
/** Change oledDisplay and led state */
if (ag->isOne()) {
@ -752,6 +836,20 @@ static void configUpdateHandle() {
String(configuration.getDisplayBrightness()));
}
fwNewVersion = configuration.newFirmwareVersion();
if (fwNewVersion.length()) {
int lastOta = configuration.getLastOta();
if (lastOta != 0 && lastOta < (60 * 60 * 24)) {
Serial.println("Ignore OTA cause last update is " + String(lastOta) +
String("sec"));
Serial.println("Retry again after 24h");
} else {
configuration.updateLastOta();
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
}
}
appDispHandler();
appLedHandler();
}

View File

@ -1,12 +1,11 @@
#ifndef _OTA_HANDLER_H_
#define _OTA_HANDLER_H_
#include <Arduino.h>
#include <esp_err.h>
#include <esp_http_client.h>
#include <esp_ota_ops.h>
#define OTA_BUF_SIZE 512
#define OTA_BUF_SIZE 1024
#define URL_BUF_SIZE 256
enum OtaUpdateOutcome {
@ -16,10 +15,19 @@ enum OtaUpdateOutcome {
UDPATE_SKIPPED
};
enum OtaState {
OTA_STATE_BEGIN,
OTA_STATE_FAIL,
OTA_STATE_PROCESSING,
OTA_STATE_SUCCESS
};
typedef void(*OtaHandlerCallback_t)(OtaState state,
String message);
class OtaHandler {
public:
void updateFirmwareIfOutdated(String deviceId) {
String url = "http://hw.airgradient.com/sensors/airgradient:" + deviceId +
"/generic/os/firmware.bin";
url += "?current_firmware=";
@ -30,15 +38,26 @@ public:
esp_http_client_config_t config = {};
config.url = urlAsChar;
esp_err_t ret = attemptToPerformOta(&config);
OtaUpdateOutcome ret = attemptToPerformOta(&config);
Serial.println(ret);
if (ret == OtaUpdateOutcome::UPDATE_PERFORMED) {
Serial.println("OTA update performed, restarting ...");
esp_restart();
if (this->callback) {
this->callback(OtaState::OTA_STATE_SUCCESS, "");
}
} else {
if(this->callback) {
this->callback(OtaState::OTA_STATE_FAIL, "");
}
}
}
void setHandlerCallback(OtaHandlerCallback_t callback) {
this->callback = callback;
}
private:
OtaHandlerCallback_t callback;
OtaUpdateOutcome attemptToPerformOta(const esp_http_client_config_t *config) {
esp_http_client_handle_t client = esp_http_client_init(config);
if (client == NULL) {
@ -94,6 +113,16 @@ private:
}
int binary_file_len = 0;
int totalSize = esp_http_client_get_content_length(client);
Serial.println("File size: " + String(totalSize) + String(" bytes"));
// Show display start update new firmware.
if (this->callback) {
this->callback(OtaState::OTA_STATE_BEGIN, "");
}
// Download file and write new firmware to OTA partition
uint32_t lastUpdate = millis();
while (1) {
int data_read =
esp_http_client_read(client, upgrade_data_buf, OTA_BUF_SIZE);
@ -103,16 +132,33 @@ private:
}
if (data_read < 0) {
Serial.println("Data read error");
if (this->callback) {
this->callback(OtaState::OTA_STATE_FAIL, "");
}
break;
}
if (data_read > 0) {
ota_write_err = esp_ota_write(
update_handle, (const void *)upgrade_data_buf, data_read);
if (ota_write_err != ESP_OK) {
if (this->callback) {
this->callback(OtaState::OTA_STATE_FAIL, "");
}
break;
}
binary_file_len += data_read;
// Serial.printf("Written image length %d\n", binary_file_len);
int percent = (binary_file_len * 100) / totalSize;
uint32_t ms = (uint32_t)(millis() - lastUpdate);
if (ms >= 250) {
// sm.executeOTA(StateMachine::OtaState::OTA_STATE_PROCESSING, "",
// percent);
if (this->callback) {
this->callback(OtaState::OTA_STATE_PROCESSING,
String(percent));
}
lastUpdate = millis();
}
}
}
free(upgrade_data_buf);

View File

@ -6,6 +6,7 @@
#else
#include "EEPROM.h"
#endif
#include <time.h>
#define EEPROM_CONFIG_SIZE 1024
#define CONFIG_FILE_NAME "/cfg.json"
@ -613,6 +614,18 @@ bool Configuration::parse(String data, bool isLocal) {
}
}
if (JSON.typeof_(root["targetFirmware"]) == "string") {
String newVer = root["targetFirmware"];
String curVer = String(GIT_VERSION);
if (curVer != newVer) {
logInfo("Detected new firwmare version: " + newVer);
otaNewFirmwareVersion = newVer;
udpated = true;
} else {
otaNewFirmwareVersion = String("");
}
}
if (changed) {
udpated = true;
saveConfig();
@ -1166,3 +1179,52 @@ bool Configuration::isDisplayBrightnessChanged(void) {
displayBrightnessChanged = false;
return changed;
}
/**
* @brief Get number of sec from last OTA
*
* @return int < 0 is invalid, 0 mean there is no OTA trigger.
*/
int Configuration::getLastOta(void) {
struct tm timeInfo;
if (getLocalTime(&timeInfo) == false) {
logError("Get localtime failed");
return -1;
}
int curYear = timeInfo.tm_year + 1900;
if (curYear < 2024) {
logError("Current year " + String(curYear) + String(" invalid"));
return -1;
}
time_t curTime = mktime(&timeInfo);
logInfo("Last ota time: " + String(config.lastOta));
if (config.lastOta == 0) {
return 0;
}
int sec = curTime - config.lastOta;
logInfo("Last ota secconds: " + String(sec));
return sec;
}
void Configuration::updateLastOta(void) {
struct tm timeInfo;
if (getLocalTime(&timeInfo) == false) {
logError("updateLastOta: Get localtime failed");
return;
}
int curYear = timeInfo.tm_year + 1900;
if (curYear < 2024) {
logError("updateLastOta: lolcal time invalid");
return;
}
config.lastOta = mktime(&timeInfo);
logInfo("Last OTA: " + String(config.lastOta));
saveConfig();
}
String Configuration::newFirmwareVersion(void) {
String newFw = otaNewFirmwareVersion;
otaNewFirmwareVersion = String("");
return newFw;
}

View File

@ -16,6 +16,7 @@ private:
bool _tvocLearningOffsetChanged;
bool ledBarBrightnessChanged = false;
bool displayBrightnessChanged = false;
String otaNewFirmwareVersion;
AirGradient* ag;
@ -74,6 +75,9 @@ public:
int getLedBarBrightness(void);
bool isDisplayBrightnessChanged(void);
int getDisplayBrightness(void);
int getLastOta(void);
void updateLastOta(void);
String newFirmwareVersion(void);
bool isOfflineMode(void);
void setOfflineMode(bool offline);
};

View File

@ -49,6 +49,16 @@ void OledDisplay::showTempHum(bool hasStatus) {
}
}
void OledDisplay::setCentralText(int y, String text) {
setCentralText(y, text.c_str());
}
void OledDisplay::setCentralText(int y, const char *text) {
int x = (DISP()->getWidth() - DISP()->getStrWidth(text)) / 2;
DISP()->drawStr(x, y, text);
}
/**
* @brief Construct a new Ag Oled Display:: Ag Oled Display object
*
@ -291,3 +301,42 @@ void OledDisplay::showDashboard(const char *status) {
void OledDisplay::setBrightness(int percent) {
DISP()->setContrast((127 * percent) / 100);
}
void OledDisplay::showNewFirmwareVersion(String version) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "New version");
setCentralText(60, version.c_str());
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareUpdating(String percent) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(50, String("Updating... ") + percent + String("%"));
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareSuccess(String count) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Success");
setCentralText(60, String("Rebooting... ") + count);
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareFailed(void) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Failed");
setCentralText(60, String("Retry after 24h"));
} while (DISP()->nextPage());
}

View File

@ -16,6 +16,9 @@ private:
Measurements &value;
void showTempHum(bool hasStatus);
void setCentralText(int y, String text);
void setCentralText(int y, const char *text);
public:
OledDisplay(Configuration &config, Measurements &value,
Stream &log);
@ -32,6 +35,10 @@ public:
void showDashboard(void);
void showDashboard(const char *status);
void setBrightness(int percent);
void showNewFirmwareVersion(String version);
void showNewFirmwareUpdating(String percent);
void showNewFirmwareSuccess(String count);
void showNewFirmwareFailed(void);
};
#endif /** _AG_OLED_DISPLAY_H_ */

View File

@ -1,5 +1,6 @@
#include "AgWiFiConnector.h"
#include "Libraries/WiFiManager/WiFiManager.h"
#include <time.h>
#define WIFI_CONNECT_COUNTDOWN_MAX 180
#define WIFI_HOTSPOT_PASSWORD_DEFAULT "cleanair"
@ -158,6 +159,11 @@ bool WifiConnector::connect(void) {
config.setPostToAirGradient(result != "T");
}
hasPortalConfig = false;
/** Configure internet time */
const char *ntp_server = "pool.ntp.org";
configTime(0, 0, ntp_server);
logInfo("Set internet time server: " + String(ntp_server));
}
#else
_wifiProcess();

View File

@ -61,6 +61,9 @@ enum AgStateMachineState {
AgStateMachineLedBarTest,
AgStateMachineLedBarPowerUpTest,
/** OTA perform, show display status */
AgStateMachineOtaPerform,
/** LED: Show working state.
* Display: Show dashboard */
AgStateMachineNormal,