daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Feb 26th, 2019 21 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoJson.h>
  3. #include "bme280.h"
  4.  
  5. #include "config.h"
  6.  
  7. const char *luftdatenAPIHOST = "api.luftdaten.info";
  8. const char *luftdatenAPIURL = "/v1/push-sensor-data/";
  9. const uint16_t luftdatenAPIPort = 80;
  10.  
  11. void sendLuftdatenJson(JsonObject& json) {
  12.     WiFiClient client;
  13.     Serial.print("\nconnecting to ");
  14.     Serial.println(luftdatenAPIHOST);
  15.  
  16.     if (!client.connect(luftdatenAPIHOST, luftdatenAPIPort)) {
  17.         Serial.println("connection failed");
  18.         Serial.println("wait 3 sec...\n");
  19.         delay(3000);
  20.         return;
  21.     }
  22.  
  23.     delay(100);
  24.     client.println("POST "+ String(luftdatenAPIURL) + " HTTP/1.1");
  25.     client.println("Host: "+ String(luftdatenAPIHOST));
  26.     client.println("Content-Type: application/json");
  27.     if (!strcmp(DUST_MODEL, "PMS7003")) {
  28.         client.println("X-PIN: 1");
  29.     }
  30.     if (!strcmp(THP_MODEL, "BME280")) {
  31.         client.println("X-PIN: 11");
  32.     }
  33.     client.println("X-Sensor: esp8266-" + String(ESP.getChipId()));
  34.     client.print("Content-Length: ");
  35.     client.println(measureJson(json));
  36.     client.println();
  37.     client.println("Connection: close");
  38.     client.println();
  39.    
  40.     serializeJson(json, client);
  41.  
  42.     String line = client.readStringUntil('\r');
  43.     // TODO: Support wrong error (!= 200)
  44.  
  45.     if (DEBUG) {
  46.         Serial.print("Length:");
  47.         Serial.println(measureJson(json));
  48.         serializeJsonPretty(json, Serial);
  49.         Serial.println("\n");
  50.         Serial.println(line);
  51.     }
  52.  
  53.     client.stop();
  54.    
  55. }
  56.  
  57. void sendPMSDatatoLuftdaten(int averagePM1, int averagePM25, int averagePM10) {
  58.     if (strcmp(DUST_MODEL, "Non")) {
  59.     StaticJsonDocument<600> jsonBuffer;
  60.     JsonObject json = jsonBuffer.to<JsonObject>();
  61.     if (!strcmp(DUST_MODEL, "PMS7003")) {
  62.         json["software_version"] = "Smogomierz_" + String(SOFTWAREVERSION);
  63.         JsonArray sensordatavalues = json.createNestedArray("sensordatavalues");
  64.           JsonObject P1 = sensordatavalues.createNestedObject();
  65.             P1["value_type"] = "P1";
  66.             P1["value"] = averagePM10;
  67.           JsonObject P2 = sensordatavalues.createNestedObject();
  68.             P2["value_type"] = "P2";
  69.             P2["value"] = averagePM25;
  70.     }
  71.     sendLuftdatenJson(json);
  72.     }
  73. }
  74.  
  75. void sendBMEDatatoLuftdaten(BME280<BME280_C, BME280_ADDRESS> &bme) {
  76.     if (strcmp(THP_MODEL, "Non")) {
  77.     StaticJsonDocument<600> jsonBuffer;
  78.     JsonObject json = jsonBuffer.to<JsonObject>();
  79.     if (!strcmp(THP_MODEL, "BME280")) {
  80.         json["software_version"] = "Smogomierz_" + String(SOFTWAREVERSION);
  81.         JsonArray sensordatavalues = json.createNestedArray("sensordatavalues");
  82.           JsonObject temperature = sensordatavalues.createNestedObject();
  83.             temperature["value_type"] = "temperature";
  84.             temperature["value"] = float(bme.temperature);
  85.           JsonObject humidity = sensordatavalues.createNestedObject();
  86.             humidity["value_type"] = "humidity";
  87.             humidity["value"] = float(bme.humidity);
  88.           JsonObject pressure = sensordatavalues.createNestedObject();
  89.             pressure["value_type"] = "pressure";
  90.             pressure["value"] = float(bme.seaLevelForAltitude(MYALTITUDE));
  91.     }
  92.     sendLuftdatenJson(json);
  93.     }
  94. }
  95.  
  96. void sendDataToLuftdaten(BME280<BME280_C, BME280_ADDRESS> &bme, int averagePM1, int averagePM25, int averagePM10) {
  97.     if (!(LUFTDATEN_ON)) {
  98.         return;
  99.     }
  100.  
  101.     sendPMSDatatoLuftdaten(averagePM1, averagePM25, averagePM10);
  102.     sendBMEDatatoLuftdaten(bme);
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top