Innenraumthermometer - Programmversion 0.2

Diese Programmversion enthält einige kleinere Optimierungen. Beispielsweise wird die Batteriespannung nun grafisch angezeigt. Ebenfalls wird die Qualität des WLAN-Signals grafisch angezeigt.

Probleme:

// Außenthermometer
// Läuft auf einem Lolin D32 
// Misst Temperatur, Luftfeuchtigkeit und Luftdruck
// Sendet die Daten an OpenSenseMap

// Bibliotheken
#include <WiFi.h>
#include <Wire.h>
#include <SPI.h>  
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"                      


// Sensor Objekt
Adafruit_BME680 bme; // I2C

uint64_t chipid; 

// WLAN SSDI und Passwort
const char* ssid       = "xxxxxxxxxxxxxx";         // Name des WLAN
const char* password   = "xxxxxxxxxxxxxxxxxxxx";   // Passwort des WLAN

// Opensensemap
//senseBox ID
#define SENSEBOX_ID "5b9c9c6a7c519100197ca81c"

//Sensor IDs
// Temperatur
#define temperatureSensorID "5b9c9c6a7c519100197ca821"
// Luftfeuchtigkeit
#define humiditySensorID "5b9c9c6a7c519100197ca820"
// Luftdruck
#define pressureSensorID "5b9c9c6a7c519100197ca81f"
// Batteriespannung
#define voltageSensorID "5b9c9c6a7c519100197ca81e"
// Taupunkt
#define dewpointSensorID "5b9c9c6a7c519100197ca81d"

// OpenSenseMap Server
char server[] = "ingress.opensensemap.org";
const int port = 80;

WiFiClient client;

float voltage;                                     // Spannung der Batterie
int rssi;                                          // Signalqualität des WiFi
float temperature;                                 // Temperatur
float humidity;                                    // Luftfeuchtigkeit
float pressure;                                    // Luftdruck
float dewpoint;                                    // Taupunkt

int wifiCounter = 0;                               // Zählt die Verbindungsversuche zum WiFi

#define uS_TO_S_FACTOR 1000000                     // Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  600                         // Time ESP32 will go to sleep (in seconds) */


void setup () {
  // Serielle Schnittstelle
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");

  // Chip ID 
  chipid = ESP.getEfuseMac();                      //The chip ID is essentially its MAC address(length: 6 bytes).
  Serial.printf("ESP32 Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes
  Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes.

  // Initialisiert Wire
  Wire.begin(25, 26);           // SDA, SCL
  
  // Verbindung zum WLAN Router aufbauen
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {                      // Es wird 5 Sekunden eine Verbingung gesucht, danach wird abgebrochen
    if (wifiCounter <= 9) {
      wifiCounter ++;
      delay(500);
      Serial.print(".");
    }
    else {
      Serial.println("Connection to wifi failed!");
      Serial.println("Going to sleep now");
      delay(100);
      esp_deep_sleep_start();
    }
  }
  Serial.println(" Connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  // BME680 initialisieren 
  bool status;
  status = bme.begin();  
  if (!status) {
      Serial.println("Could not find a valid BME680 sensor, check wiring!");
      while (1);
  }
  
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_1X);
  bme.setHumidityOversampling(BME680_OS_1X);
  bme.setPressureOversampling(BME680_OS_1X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_0);
  
  // BME 680 abfragen 
  if (! bme.performReading()) {
    Serial.println("Failed to perform reading :(");
    return;
  }
  temperature = bme.readTemperature() - 1.3;                // Der der BME680 gibt die Temeperatur um 1,3 °C höher aus als der BME280
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0;

  // Taupunkt berechnen
  float a = 17.271; 
  float b = 237.7; 
  float dewpointTmp = (a * temperature) / (b + temperature) + log(humidity/100); 
  dewpoint = (b * dewpointTmp) / (a - dewpointTmp);
  Serial.print("Dewpoint: ");
  Serial.print(dewpoint);
  Serial.println(" °C");
  
  // Batteriespannung lesen
  voltage = analogRead(35) / 4096.0 * 7.445;            // Rechnerisch müsste der Faktor 7.445 sein
  Serial.print("Battery voltage: ");
  Serial.print(voltage);
  Serial.println(" volts");

  // Signalqualität WiFi
  rssi = (WiFi.RSSI() + 100) * 2;
  Serial.print("WiFi signal: ");
  Serial.print(rssi);
  Serial.println(" %");
  
  // Daten an OpensenseMap senden
  Serial.print("Connecting to ");
  Serial.println(server);

  // Use WiFiClient class to create TCP connections
  //WiFiClient client;
  if (!client.connect(server, port)) {
    Serial.println("connection failed");
    return;
  }

  postFloatValue(temperature, 1, temperatureSensorID);
  postFloatValue(humidity, 1, humiditySensorID);
  postFloatValue(pressure, 1, pressureSensorID);
  postFloatValue(voltage, 1, voltageSensorID);
  postFloatValue(dewpoint, 1, dewpointSensorID);
   
  // Schickt den ESP32 in den Tiefschlaf
  Serial.println("Going to sleep now");
  delay(100);
  esp_deep_sleep_start();

}


void loop() {

}

//######################################### Functions ##############################################

// Sendet die Daten an Opensensemap
void postFloatValue (float measurement, int digits, String sensorId) {
  //Float zu String konvertieren
  char obs[10];
  dtostrf(measurement, 5, digits, obs);
  //Json erstellen
  String jsonValue = "{\"value\":";
  jsonValue += obs;
  jsonValue += "}";
  //Mit OSeM Server verbinden und POST Operation durchführen
  Serial.println("-------------------------------------");
  Serial.print("Connectingto OSeM Server...");
  if (client.connect(server, 80)) {
    Serial.println("connected!");
    Serial.println("-------------------------------------");
    //HTTP Header aufbauen
    client.print("POST /boxes/"); client.print(SENSEBOX_ID); client.print("/"); client.print(sensorId); client.println(" HTTP/1.1");
    client.print("Host:");
    client.println(server);
    client.println("Content-Type: application/json");
    client.println("Connection: close");
    client.print("Content-Length: "); client.println(jsonValue.length());
    client.println();
    //Daten senden
    client.println(jsonValue);
    Serial.println(jsonValue);
  } else {
    Serial.println("failed!");
    Serial.println("-------------------------------------");
  }
  //Antwort von Server im seriellen Monitor anzeigen
  //waitForServerResponse();
}

Tags: #Arduino #Lolin_D32 #ESP32