Skip to content

Commit

Permalink
add WiFiManager and 'U8g2 to Libraries`
Browse files Browse the repository at this point in the history
  • Loading branch information
pnt325 committed Apr 4, 2024
1 parent dba385f commit f681d4b
Show file tree
Hide file tree
Showing 247 changed files with 515,027 additions and 791 deletions.
325 changes: 20 additions & 305 deletions examples/BASIC/BASIC.ino

Large diffs are not rendered by default.

462 changes: 165 additions & 297 deletions examples/OneOpenAir/OneOpenAir.ino

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions src/AgApiClient.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "AgApiClient.h"
#include "AgConfigure.h"
#include "AirGradient.h"
#ifdef ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#else
#include <HTTPClient.h>
#endif

AgApiClient::AgApiClient(Stream &debug, AgConfigure &config)
:PrintLog(debug, "ApiClient"), config(config) {
}
: PrintLog(debug, "ApiClient"), config(config) {}

AgApiClient::~AgApiClient() {}

Expand All @@ -26,7 +31,7 @@ void AgApiClient::begin(void) {
* @return true Success
* @return false Failure
*/
bool AgApiClient::fetchServerConfiguration(String deviceId) {
bool AgApiClient::fetchServerConfiguration(void) {
if (config.getConfigurationControl() ==
ConfigurationControl::ConfigurationControlLocal) {
logWarning("Ignore fetch server configuration");
Expand All @@ -37,12 +42,19 @@ bool AgApiClient::fetchServerConfiguration(String deviceId) {
return false;
}

String uri = "http://hw.airgradient.com/sensors/airgradient:" + deviceId +
"/one/config";
String uri =
"http://hw.airgradient.com/sensors/airgradient:" + ag->deviceId() +
"/one/config";

/** Init http client */
#ifdef ESP8266
HTTPClient client;
WiFiClient wifiClient;
if (client.begin(wifiClient, uri) == false) {
#else
HTTPClient client;
if (client.begin(uri) == false) {
#endif
getConfigFailed = true;
return false;
}
Expand All @@ -62,7 +74,7 @@ bool AgApiClient::fetchServerConfiguration(String deviceId) {
String respContent = client.getString();
client.end();

logInfo("Get configuration: " + respContent);
// logInfo("Get configuration: " + respContent);

/** Parse configuration and return result */
return config.parse(respContent, false);
Expand All @@ -76,7 +88,7 @@ bool AgApiClient::fetchServerConfiguration(String deviceId) {
* @return true Success
* @return false Failure
*/
bool AgApiClient::postToServer(String deviceId, String data) {
bool AgApiClient::postToServer(String data) {
if (config.isPostDataToAirGradient() == false) {
logWarning("Ignore post data to server");
return true;
Expand All @@ -87,7 +99,8 @@ bool AgApiClient::postToServer(String deviceId, String data) {
}

String uri =
"http://hw.airgradient.com/sensors/airgradient:" + deviceId + "/measures";
"http://hw.airgradient.com/sensors/airgradient:" + ag->deviceId() +
"/measures";
logInfo("Post uri: " + uri);
logInfo("Post data: " + data);

Expand Down Expand Up @@ -125,3 +138,5 @@ bool AgApiClient::isFetchConfigureFailed(void) { return getConfigFailed; }
* @return false Failure
*/
bool AgApiClient::isPostToServerFailed(void) { return postToServerFailed; }

void AgApiClient::setAirGradient(AirGradient *ag) { this->ag = ag; }
10 changes: 5 additions & 5 deletions src/AgApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
#define _AG_API_CLIENT_H_

#include "AgConfigure.h"
#include "AirGradient.h"
#include "Main/PrintLog.h"
#include <Arduino.h>

class AgConfigure;
class AirGradient;

class AgApiClient : public PrintLog {
private:
AgConfigure &config;
AirGradient *ag;

bool getConfigFailed;
bool postToServerFailed;
Expand All @@ -30,10 +29,11 @@ class AgApiClient : public PrintLog {
~AgApiClient();

void begin(void);
bool fetchServerConfiguration(String deviceId);
bool postToServer(String deviceId, String data);
bool fetchServerConfiguration(void);
bool postToServer(String data);
bool isFetchConfigureFailed(void);
bool isPostToServerFailed(void);
void setAirGradient(AirGradient *ag);
};

#endif /** _AG_API_CLIENT_H_ */
120 changes: 76 additions & 44 deletions src/AgConfigure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ const char *CONFIGURATION_CONTROL_NAME[] = {
[ConfigurationControlLocal] = "local",
[ConfigurationControlCloud] = "cloud",
[ConfigurationControlBoth] = "both"};
const char *LED_BAR_MODE_NAMES[] = {
[LedBarModeOff] = "off",
[LedBarModePm] = "pm",
[LedBarModeCO2] = "co2",
};

String AgConfigure::getLedBarModeName(LedBarMode mode) {
LedBarMode ledBarMode = mode;
if (ledBarMode == LedBarModeOff) {
return String("off");
} else if (ledBarMode == LedBarModePm) {
return String("pm");
} else if (ledBarMode == LedBarModeCO2) {
return String("co2");
} else {
return String("off");
if (mode == LedBarModeOff) {
return String(LED_BAR_MODE_NAMES[LedBarModeOff]);
} else if (mode == LedBarModePm) {
return String(LED_BAR_MODE_NAMES[LedBarModePm]);
} else if (mode == LedBarModeCO2) {
return String(LED_BAR_MODE_NAMES[LedBarModeCO2]);
}
return String("unknown");
}

void AgConfigure::saveConfig(void) {
Expand All @@ -26,13 +29,32 @@ void AgConfigure::saveConfig(void) {
for (int i = 0; i < len; i++) {
config._check += data[i];
}
#ifdef ESP8266
for (int i = 0; i < sizeof(config); i++) {
EEPROM.write(i, data[i]);
}
#else
EEPROM.writeBytes(0, &config, sizeof(config));
#endif
EEPROM.commit();
logInfo("Save Config");
}

void AgConfigure::loadConfig(void) {
bool readSuccess = false;
#ifdef ESP8266
uint8_t *data = (uint8_t *)&config;
for (int i = 0; i < sizeof(config); i++) {
data[i] = EEPROM.read(i);
}
readSuccess = true;
#else
if (EEPROM.readBytes(0, &config, sizeof(config)) != sizeof(config)) {
readSuccess = true;
}
#endif

if (readSuccess) {
logError("Load configure failed");
defaultConfig();
} else {
Expand Down Expand Up @@ -104,40 +126,50 @@ bool AgConfigure::parse(String data, bool isLocal) {
bool changed = false;

/** Get ConfigurationControl */
if (JSON.typeof_(root["configurationControl"]) == "string") {
String configurationControl = root["configurationControl"];
if (configurationControl ==
String(CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlLocal])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlLocal;
changed = true;
} else if (configurationControl ==
String(CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlCloud])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlCloud;
changed = true;
} else if (configurationControl ==
String(CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlBoth])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlBoth;
changed = true;
if (isLocal) {
if (JSON.typeof_(root["configurationControl"]) == "string") {
String configurationControl = root["configurationControl"];
if (configurationControl ==
String(CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlLocal])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlLocal;
changed = true;
} else if (configurationControl ==
String(
CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlCloud])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlCloud;
changed = true;
} else if (configurationControl ==
String(CONFIGURATION_CONTROL_NAME
[ConfigurationControl::ConfigurationControlBoth])) {
config.configurationControl =
(uint8_t)ConfigurationControl::ConfigurationControlBoth;
changed = true;
} else {

logError(String("'configurationControl' value '" +
configurationControl + "' invalid")
.c_str());
return false;
}
} else {
logError("'configurationControl' value '" + configurationControl +
"' invalid");
return false;
}
} else {
return false;
}

if ((config.configurationControl ==
(byte)ConfigurationControl::ConfigurationControlCloud)) {
logWarning("Ignore, cause ConfigurationControl is " +
String(CONFIGURATION_CONTROL_NAME[config.configurationControl]));
return false;
if ((config.configurationControl ==
(byte)ConfigurationControl::ConfigurationControlCloud)) {
logWarning("Local configure ignored");
return false;
}
} else {
if (config.configurationControl ==
(byte)ConfigurationControl::ConfigurationControlLocal) {
logWarning("Cloud configure ignored");
return false;
}
}

if (JSON.typeof_(root["country"]) == "string") {
Expand All @@ -146,7 +178,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (country != String(config.country)) {
changed = true;
snprintf(config.country, sizeof(config.country), country.c_str());
logInfo("Set country: " + country);
logInfo(String("Set country: " + country).c_str());
}

// Update temperature unit if get configuration from server
Expand Down Expand Up @@ -196,12 +228,12 @@ bool AgConfigure::parse(String data, bool isLocal) {

if (JSON.typeof_(root["ledBarMode"]) == "string") {
String mode = root["ledBarMode"];
uint8_t ledBarMode = config.useRGBLedBar;
if (mode == "co2") {
uint8_t ledBarMode = LedBarModeOff;
if (mode == String(LED_BAR_MODE_NAMES[LedBarModeCO2])) {
ledBarMode = LedBarModeCO2;
} else if (mode == "pm") {
} else if (mode == String(LED_BAR_MODE_NAMES[LedBarModePm])) {
ledBarMode = LedBarModePm;
} else if (mode == "off") {
} else if (mode == String(LED_BAR_MODE_NAMES[LedBarModeOff])) {
ledBarMode = LedBarModeOff;
} else {
ledBarMode = config.useRGBLedBar;
Expand Down
Loading

0 comments on commit f681d4b

Please sign in to comment.