Skip to content

Commit

Permalink
Move Arduino_JSON to AirGradient libries
Browse files Browse the repository at this point in the history
  • Loading branch information
pnt325 committed Apr 11, 2024
1 parent bd11979 commit 1d6a0a0
Show file tree
Hide file tree
Showing 24 changed files with 5,411 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ If you have any questions or problems, check out [our forum](https://forum.airgr
- [Sensirion Core](https://github.com/Sensirion/arduino-core/)
- [Sensirion I2C SGP41](https://github.com/Sensirion/arduino-i2c-sgp41)
- [Sensirion I2C SHT](https://github.com/Sensirion/arduino-sht)
- [WiFiManager](https://github.com/tzapu/WiFiManager)
- [Arduino_JSON](https://github.com/arduino-libraries/Arduino_JSON)

## License
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
34 changes: 13 additions & 21 deletions examples/BASIC/BASIC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ Outdoor Monitor: https://www.airgradient.com/outdoor/
Build Instructions:
https://www.airgradient.com/documentation/diy-v4/
Following libraries need to be installed:
"Arduino_JSON" by Arduino version 0.2.0
Please make sure you have esp8266 board manager installed. Tested with
version 3.1.2.
Expand All @@ -35,7 +32,6 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#include "AgSchedule.h"
#include "AgWiFiConnector.h"
#include <AirGradient.h>
#include <Arduino_JSON.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
Expand Down Expand Up @@ -166,12 +162,8 @@ void loop() {
}

static void sendDataToAg() {
JSONVar root;
root["wifi"] = WiFi.RSSI();
root["boot"] = 0;

// delay(1500);
if (apiClient.postToServer(JSON.stringify(root))) {
if (apiClient.sendPing(wifiConnector.RSSI(), 0)) {
// Ping Server succses
} else {
// Ping server failed
Expand Down Expand Up @@ -327,22 +319,22 @@ static void tempHumUpdate() {
}

static void sendDataToServer() {
JSONVar root;
root["wifi"] = WiFi.RSSI();
if (co2Ppm >= 0) {
root["rco2"] = co2Ppm;
}
if (pm25 >= 0) {
root["pm02"] = pm25;
String wifi = "\"wifi\":" + String(WiFi.RSSI());
String rco2 = "";
if(co2Ppm >= 0){
rco2 = ",\"rco2\":" + String(co2Ppm);
}
if (temp > -1001) {
root["atmp"] = ag.round2(temp);
String pm02 = "";
if(pm25) {
pm02 = ",\"pm02\":" + String(pm25);
}
if (hum >= 0) {
root["rhum"] = hum;
String rhum = "";
if(hum >= 0){
rhum = ",\"rhum\":" + String(rhum);
}
String payload = "{" + wifi + rco2 + pm02 + rhum + "}";

if (apiClient.postToServer(JSON.stringify(root)) == false) {
if (apiClient.postToServer(payload) == false) {
Serial.println("Post to server failed");
}
}
Expand Down
10 changes: 1 addition & 9 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ https://www.airgradient.com/documentation/one-v9/ Build Instructions:
AirGradient Open Air:
https://www.airgradient.com/documentation/open-air-pst-kit-1-3/
The codes needs the following libraries installed:
"Arduino_JSON" by Arduino version 0.2.0
Please make sure you have esp32 board manager installed. Tested with
version 2.0.11.
Expand Down Expand Up @@ -53,7 +50,6 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#include "OpenMetrics.h"
#include "WebServer.h"
#include <AirGradient.h>
#include <Arduino_JSON.h>
#include <WebServer.h>

#define LED_BAR_ANIMATION_PERIOD 100 /** ms */
Expand Down Expand Up @@ -416,10 +412,6 @@ static void ledBarEnabledUpdate(void) {
}

static void sendDataToAg() {
JSONVar root;
root["wifi"] = wifiConnector.RSSI();
root["boot"] = measurements.bootCount;

/** Change oledDisplay and led state */
if (ag->isOne()) {
stateMachine.displayHandle(AgStateMachineWiFiOkServerConnecting);
Expand All @@ -443,7 +435,7 @@ static void sendDataToAg() {
"task_led", 2048, NULL, 5, NULL);

delay(1500);
if (apiClient.postToServer(JSON.stringify(root))) {
if (apiClient.sendPing(wifiConnector.RSSI(), measurements.bootCount)) {
if (ag->isOne()) {
stateMachine.displayHandle(AgStateMachineWiFiOkServerConnected);
}
Expand Down
16 changes: 16 additions & 0 deletions src/AgApiClient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "AgApiClient.h"
#include "AgConfigure.h"
#include "AirGradient.h"
#include "Libraries/Arduino_JSON/src/Arduino_JSON.h"
#ifdef ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
Expand Down Expand Up @@ -143,3 +144,18 @@ bool AgApiClient::isFetchConfigureFailed(void) { return getConfigFailed; }
bool AgApiClient::isPostToServerFailed(void) { return postToServerFailed; }

void AgApiClient::setAirGradient(AirGradient *ag) { this->ag = ag; }

/**
* @brief Send the package to check the connection with cloud
*
* @param rssi WiFi RSSI
* @param bootCount Boot count
* @return true Success
* @return false Failure
*/
bool AgApiClient::sendPing(int rssi, int bootCount) {
JSONVar root;
root["wifi"] = rssi;
root["boot"] = bootCount;
return postToServer(JSON.stringify(root));
}
1 change: 1 addition & 0 deletions src/AgApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AgApiClient : public PrintLog {
bool isFetchConfigureFailed(void);
bool isPostToServerFailed(void);
void setAirGradient(AirGradient *ag);
bool sendPing(int rssi, int bootCount);
};

#endif /** _AG_API_CLIENT_H_ */
18 changes: 10 additions & 8 deletions src/AgConfigure.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AgConfigure.h"
#include "EEPROM.h"
#include "Libraries/Arduino_JSON/src/Arduino_JSON.h"

const char *CONFIGURATION_CONTROL_NAME[] = {
[ConfigurationControlLocal] = "local",
Expand All @@ -12,6 +13,15 @@ const char *LED_BAR_MODE_NAMES[] = {
[LedBarModeCO2] = "co2",
};

static bool jsonTypeInvalid(JSONVar root, String validType) {
String type = JSON.typeof_(root);
if (type == validType || type == "undefined" || type == "unknown" ||
type == "null") {
return false;
}
return true;
}

/**
* @brief Get LedBarMode Name
*
Expand Down Expand Up @@ -706,14 +716,6 @@ bool Configuration::isUpdated(void) {
return updated;
}

bool Configuration::jsonTypeInvalid(JSONVar root, String validType) {
String type = JSON.typeof_(root);
if (type == validType || type == "undefined" || type == "unknown" || type == "null") {
return false;
}
return true;
}

String Configuration::jsonTypeInvalidMessage(String name, String type) {
return "'" + name + "' type invalid, it's should '" + type + "'";
}
Expand Down
2 changes: 0 additions & 2 deletions src/AgConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "App/AppDef.h"
#include "Main/PrintLog.h"
#include <Arduino.h>
#include <Arduino_JSON.h>

class Configuration : public PrintLog {
private:
Expand Down Expand Up @@ -40,7 +39,6 @@ class Configuration : public PrintLog {
void loadConfig(void);
void defaultConfig(void);
void printConfig(void);
bool jsonTypeInvalid(JSONVar root, String validType);
String jsonTypeInvalidMessage(String name, String type);
String jsonValueInvalidMessage(String name, String value);
void jsonInvalid(void);
Expand Down
2 changes: 1 addition & 1 deletion src/AgValue.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AgValue.h"
#include "AgConfigure.h"
#include "AirGradient.h"
#include <Arduino_JSON.h>
#include "Libraries/Arduino_JSON/src/Arduino_JSON.h"

String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
void *_ag, void *_config) {
Expand Down
Loading

0 comments on commit 1d6a0a0

Please sign in to comment.