Skip to content

Commit

Permalink
Merge pull request #1 from garyservin/add-description-field
Browse files Browse the repository at this point in the history
Add description field
  • Loading branch information
garyservin authored Dec 4, 2020
2 parents 029126b + 6472ba9 commit bbfb3b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
3 changes: 2 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ char sensor[8] = "PMS7003";

/* HTTP Client */
const char* api_url = "http://rald-dev.greenbeep.com/api/v1/measurements";
char api_key[33] = ""; // Random generated one
char api_key[33] = "";
char latitude[12] = "";
char longitude[12] = "";
char description[21] = "";

/* ----------------- Hardware-specific config ---------------------- */
#define ESP_WAKEUP_PIN D0 // To reset ESP8266 after deep sleep
Expand Down
52 changes: 30 additions & 22 deletions linka-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ uint32_t g_pm10p0_ppd_value = 0; // Particles Per Deciliter pm10.0 reading
char http_data_template[] = "[{"
"\"sensor\": \"%s\","
"\"source\": \"%s\","
"\"description\": \"%s\","
"\"pm1dot0\": %d,"
"\"pm2dot5\": %d,"
"\"pm10\": %d,"
"\"longitude\": %s,"
"\"latitude\": %s,"
"\"recorded\":\"%s\""
"\"recorded\": \"%s\""
"}]";

// General
Expand Down Expand Up @@ -100,7 +101,8 @@ WiFiConnect wc;
WiFiConnectParam api_key_param("api_key", "API Key", "", 33);
WiFiConnectParam latitude_param("latitude", "Latitude", "", 12);
WiFiConnectParam longitude_param("longitude", "Longitude", "", 12);
WiFiConnectParam sensor_param("sensor", "Sensor model", "", 8);
WiFiConnectParam sensor_param("sensor", "Sensor model", "PMS7003", 8);
WiFiConnectParam description_param("description", "Desciption", "", 8);
//flag for saving data
bool shouldSaveConfig = false;
bool shouldReadConfig = true;
Expand Down Expand Up @@ -140,14 +142,13 @@ void setup()

initFS();

// Connect to WiFi
Serial.println("Connecting to WiFi");
if (initWifi())
{
Serial.println("WiFi connected");
} else {
Serial.println("WiFi FAILED");
}
// Wait before we attempt to connect to Wifi
// In the case of a power outage, wifi might take a while to be ready, we don't want
// the sensor to go to captive portal mode too quickly.
// TODO(gservin): Find a way to only wait if there's already a WiFi config stored
Serial.println("Waiting to let wifi start...");
delay(60 * 1000); // wait roughly 60 seconds
initWifi();

delay(100);

Expand Down Expand Up @@ -278,7 +279,7 @@ void updatePmsReadings()
*/
void reportToHttp()
{
char measurements[150];
char measurements[256];
char recorded[27];
char source[10];

Expand All @@ -291,10 +292,19 @@ void reportToHttp()
timeinfo->tm_min,
timeinfo->tm_sec);
sprintf(source, "%x", g_device_id);
sprintf(measurements, http_data_template, sensor, source, g_pm1p0_sp_value, g_pm2p5_sp_value, g_pm10p0_sp_value, longitude, latitude, recorded);
sprintf(measurements,
http_data_template,
sensor,
source,
description,
g_pm1p0_sp_value,
g_pm2p5_sp_value,
g_pm10p0_sp_value,
longitude,
latitude,
recorded);
Serial.println(measurements);

// Start http client
if (http.begin(client, api_url)) {

// Add headers
Expand All @@ -306,14 +316,6 @@ void reportToHttp()
if (httpCode > 0) {
// HTTP header has been sent and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);

// file found at server
//if (httpCode == HTTP_CODE_OK) {
// const String& payload = http.getString();
// Serial.println("received payload:\n<<");
// Serial.println(payload);
// Serial.println(">>");
//}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
Expand Down Expand Up @@ -448,6 +450,7 @@ bool initWifi()
wc.addParameter(&latitude_param);
wc.addParameter(&longitude_param);
wc.addParameter(&sensor_param);
wc.addParameter(&description_param);

//wc.resetSettings(); //helper to remove the stored wifi connection, comment out after first upload and re upload

Expand All @@ -459,14 +462,15 @@ bool initWifi()
*/
if (!wc.autoConnect()) { // try to connect to wifi
/* We could also use button etc. to trigger the portal on demand within main loop */
wc.startConfigurationPortal(AP_WAIT);//if not connected show the configuration portal
wc.startConfigurationPortal(AP_WAIT); //if not connected show the configuration portal
}

if (shouldReadConfig) {
strcpy(api_key, api_key_param.getValue());
strcpy(latitude, latitude_param.getValue());
strcpy(longitude, longitude_param.getValue());
strcpy(sensor, sensor_param.getValue());
strcpy(description, description_param.getValue());
}

Serial.println("Using the following parameters:");
Expand All @@ -478,6 +482,8 @@ bool initWifi()
Serial.println(longitude);
Serial.print("Sensor: ");
Serial.println(sensor);
Serial.print("Description: ");
Serial.println(description);

if (shouldSaveConfig) {
Serial.println("Saving config");
Expand All @@ -487,6 +493,7 @@ bool initWifi()
json["latitude"] = latitude;
json["longitude"] = longitude;
json["sensor"] = sensor;
json["description"] = description;

File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Expand Down Expand Up @@ -530,6 +537,7 @@ void initFS(void)
strcpy(latitude, json["latitude"]);
strcpy(longitude, json["longitude"]);
strcpy(sensor, json["sensor"]);
strcpy(description, json["description"]);
shouldReadConfig = false;

} else {
Expand Down

0 comments on commit bbfb3b8

Please sign in to comment.