Skip to content

Commit

Permalink
Merge pull request #173 from airgradienthq/develop
Browse files Browse the repository at this point in the history
Next version 3.1.3
  • Loading branch information
pnt325 authored Jun 6, 2024
2 parents 7a1a033 + 6bdd4cb commit 8a83e40
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 69 deletions.
4 changes: 2 additions & 2 deletions examples/BASIC/BASIC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#define LED_BAR_COUNT_INIT_VALUE (-1) /** */
#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
#define DISP_UPDATE_INTERVAL 5000 /** ms */
#define SERVER_CONFIG_UPDATE_INTERVAL 30000 /** ms */
#define SERVER_CONFIG_SYNC_INTERVAL 60000 /** ms */
#define SERVER_SYNC_INTERVAL 60000 /** ms */
#define SENSOR_CO2_CALIB_COUNTDOWN_MAX 5 /** sec */
#define SENSOR_TVOC_UPDATE_INTERVAL 1000 /** ms */
Expand Down Expand Up @@ -82,7 +82,7 @@ bool hasSensorPMS = true;
bool hasSensorSHT = true;
int pmFailCount = 0;
int getCO2FailCount = 0;
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL,
AgSchedule configSchedule(SERVER_CONFIG_SYNC_INTERVAL,
updateServerConfiguration);
AgSchedule serverSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
AgSchedule dispSchedule(DISP_UPDATE_INTERVAL, dispHandler);
Expand Down
124 changes: 76 additions & 48 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License

#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
#define DISP_UPDATE_INTERVAL 2500 /** ms */
#define SERVER_CONFIG_UPDATE_INTERVAL 15000 /** ms */
#define SERVER_CONFIG_SYNC_INTERVAL 60000 /** ms */
#define SERVER_SYNC_INTERVAL 60000 /** ms */
#define MQTT_SYNC_INTERVAL 60000 /** ms */
#define SENSOR_CO2_CALIB_COUNTDOWN_MAX 5 /** sec */
Expand All @@ -64,6 +64,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#define SENSOR_PM_UPDATE_INTERVAL 2000 /** ms */
#define SENSOR_TEMP_HUM_UPDATE_INTERVAL 2000 /** ms */
#define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */
#define FIRMWARE_CHECK_FOR_UPDATE_MS (60*60*1000) /** ms */

/** I2C define */
#define I2C_SDA_PIN 7
Expand Down Expand Up @@ -113,19 +114,21 @@ static void factoryConfigReset(void);
static void wdgFeedUpdate(void);
static void ledBarEnabledUpdate(void);
static bool sgp41Init(void);
static void firmwareCheckForUpdate(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,
AgSchedule configSchedule(SERVER_CONFIG_SYNC_INTERVAL,
configurationUpdateSchedule);
AgSchedule agApiPostSchedule(SERVER_SYNC_INTERVAL, sendDataToServer);
AgSchedule co2Schedule(SENSOR_CO2_UPDATE_INTERVAL, co2Update);
AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, updatePm);
AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate);
AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, updateTvoc);
AgSchedule watchdogFeedSchedule(60000, wdgFeedUpdate);
AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, firmwareCheckForUpdate);

void setup() {
/** Serial for print debug message */
Expand Down Expand Up @@ -166,16 +169,6 @@ void setup() {
/** Connecting wifi */
bool connectToWifi = false;
if (ag->isOne()) {
if (ledBarButtonTest) {
if (ag->button.getState() == PushButton::BUTTON_PRESSED) {
WiFi.begin("airgradient", "cleanair");
Serial.println("WiFi Credential reset to factory defaults");
ESP.restart();
}
} else {
ledBarEnabledUpdate();
}

/** Show message confirm offline mode, should me perform if LED bar button
* test pressed */
if (ledBarButtonTest == false) {
Expand Down Expand Up @@ -219,10 +212,8 @@ void setup() {
#ifdef ESP8266
// ota not supported
#else
otaHandler.updateFirmwareIfOutdated(ag->deviceId());

/** Update first OTA */
measurements.otaBootCount = 0;
firmwareCheckForUpdate();
checkForUpdateSchedule.update();
#endif

apiClient.fetchServerConfiguration();
Expand All @@ -241,8 +232,12 @@ void setup() {
ledBarEnabledUpdate();
}
} else {
oledDisplay.showRebooting();
delay(2500);
if (wifiConnector.isConfigurePorttalTimeout()) {
oledDisplay.showRebooting();
delay(2500);
oledDisplay.setText("", "", "");
ESP.restart();
}
}
}
}
Expand Down Expand Up @@ -312,6 +307,9 @@ void loop() {

/** check that local configura changed then do some action */
configUpdateHandle();

/** Firmware check for update handle */
checkForUpdateSchedule.run();
}

static void co2Update(void) {
Expand Down Expand Up @@ -420,6 +418,7 @@ static void factoryConfigReset(void) {
}

/** Reset WIFI */
WiFi.enableSTA(true); // Incase offline mode
WiFi.disconnect(true, true);

/** Reset local config */
Expand All @@ -431,6 +430,7 @@ static void factoryConfigReset(void) {
Serial.println("Factory reset successful");
}
delay(3000);
oledDisplay.setText("","","");
ESP.restart();
}
}
Expand Down Expand Up @@ -488,7 +488,22 @@ static bool sgp41Init(void) {
return false;
}

static void firmwareCheckForUpdate(void) {
Serial.println();
Serial.println("firmwareCheckForUpdate:");

if (wifiConnector.isConnected()) {
Serial.println("firmwareCheckForUpdate: Perform");
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
} else {
Serial.println("firmwareCheckForUpdate: Ignored");
}
Serial.println();
}

static void otaHandlerCallback(OtaState state, String mesasge) {
Serial.println("OTA message: " + mesasge);
switch (state) {
case OtaState::OTA_STATE_BEGIN:
displayExecuteOta(state, fwNewVersion, 0);
Expand All @@ -511,7 +526,7 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
switch (state) {
case OtaState::OTA_STATE_BEGIN: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareVersion(msg);
oledDisplay.showFirmwareUpdateVersion(msg);
} else {
Serial.println("New firmware: " + msg);
}
Expand All @@ -520,17 +535,37 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
}
case OtaState::OTA_STATE_FAIL: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareFailed();
oledDisplay.showFirmwareUpdateFailed();
} else {
Serial.println("Error: Firmware update: failed");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_SKIP: {
if (ag->isOne()) {
oledDisplay.showFirmwareUpdateSkipped();
} else {
Serial.println("Firmware update: Skipped");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_UP_TO_DATE: {
if (ag->isOne()) {
oledDisplay.showFirmwareUpdateUpToDate();
} else {
Serial.println("Firmware update: up to date");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_PROCESSING: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareUpdating(String(processing));
oledDisplay.showFirmwareUpdateProgress(processing);
} else {
Serial.println("Firmware update: " + String(processing) + String("%"));
}
Expand All @@ -546,13 +581,14 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
while (i != 0) {
i = i - 1;
if (ag->isOne()) {
oledDisplay.showNewFirmwareSuccess(String(i));
oledDisplay.showFirmwareUpdateSuccess(i);
} else {
Serial.println("Rebooting... " + String(i));
}

delay(1000);
}
oledDisplay.setBrightness(0);
esp_restart();
}
break;
Expand Down Expand Up @@ -641,6 +677,24 @@ static void oneIndoorInit(void) {
}
}

/** Check for button to reset WiFi connecto to "airgraident" after test LED
* bar */
if (ledBarButtonTest) {
if (ag->button.getState() == ag->button.BUTTON_PRESSED) {
WiFi.begin("airgradient", "cleanair");
oledDisplay.setText("Configure WiFi", "connect to", "\'airgradient\'");
delay(2500);
oledDisplay.setText("Rebooting...", "","");
delay(2500);
oledDisplay.setText("","","");
ESP.restart();
}
}
ledBarEnabledUpdate();

/** Show message init sensor */
oledDisplay.setText("Sensor", "initializing...", "");

/** Init sensor SGP41 */
if (sgp41Init() == false) {
dispSensorNotFound("SGP41");
Expand Down Expand Up @@ -881,32 +935,6 @@ static void configUpdateHandle() {
stateMachine.executeLedBarTest();
}

fwNewVersion = configuration.newFirmwareVersion();
if (fwNewVersion.length()) {
bool doOta = false;
if (measurements.otaBootCount == 0) {
doOta = true;
Serial.println("First OTA");
} else {
/** Only check for update each 1h*/
const float otaBootCount = 60.0f / (SERVER_SYNC_INTERVAL / 60000.0f);
if ((measurements.bootCount - measurements.otaBootCount) >= (int)otaBootCount) {
doOta = true;
} else {
Serial.println(
"OTA ignore, try again next " +
String(30 - (measurements.bootCount - measurements.otaBootCount)) +
String(" boots"));
}
}

if (doOta) {
measurements.otaBootCount = measurements.bootCount;
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
}
}

appDispHandler();
appLedHandler();
}
Expand Down
24 changes: 19 additions & 5 deletions examples/OneOpenAir/OtaHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum OtaUpdateOutcome {
enum OtaState {
OTA_STATE_BEGIN,
OTA_STATE_FAIL,
OTA_STATE_SKIP,
OTA_STATE_UP_TO_DATE,
OTA_STATE_PROCESSING,
OTA_STATE_SUCCESS
};
Expand All @@ -40,13 +42,22 @@ class OtaHandler {
config.url = urlAsChar;
OtaUpdateOutcome ret = attemptToPerformOta(&config);
Serial.println(ret);
if (ret == OtaUpdateOutcome::UPDATE_PERFORMED) {
if (this->callback) {
if (this->callback) {
switch (ret) {
case OtaUpdateOutcome::UPDATE_PERFORMED:
this->callback(OtaState::OTA_STATE_SUCCESS, "");
}
} else {
if(this->callback) {
break;
case OtaUpdateOutcome::UDPATE_SKIPPED:
this->callback(OtaState::OTA_STATE_SKIP, "");
break;
case OtaUpdateOutcome::ALREADY_UP_TO_DATE:
this->callback(OtaState::OTA_STATE_UP_TO_DATE, "");
break;
case OtaUpdateOutcome::UPDATE_FAILED:
this->callback(OtaState::OTA_STATE_FAIL, "");
break;
default:
break;
}
}
}
Expand Down Expand Up @@ -127,6 +138,9 @@ class OtaHandler {
int data_read =
esp_http_client_read(client, upgrade_data_buf, OTA_BUF_SIZE);
if (data_read == 0) {
if (this->callback) {
this->callback(OtaState::OTA_STATE_PROCESSING, String(100));
}
Serial.println("Connection closed, all data received");
break;
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AirGradient Air Quality Sensor
version=3.1.0-beta.1
version=3.1.3
author=AirGradient <support@airgradient.com>
maintainer=AirGradient <support@airgradient.com>
sentence=ESP32-C3 / ESP8266 library for air quality monitor measuring PM, CO2, Temperature, TVOC and Humidity with OLED display.
Expand Down
Loading

0 comments on commit 8a83e40

Please # to comment.