iot:aussenthermometer:programmversion_0.1
Außenthermometer – Programmversion 0.1
Programmversion 0.1 des Außenthermometers basiert auf Programmversion 0.3 des Innenraumthermometers. Daher enthält diese Version bereits alle wesentlichen Funktionen und läuft stabil.
// Außenthermometer
// Läuft auf einem Lolin D32
// Misst Temperatur, Luftfeuchtigkeit und Luftdruck
// Berechnet den Taupunkt und den Hitzeindex
// Sendet die Daten an OpenSenseMap
// Bibliotheken
#include <WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Sensor Objekt
Adafruit_BME280 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"
// Hitzeindex
#define heatindexSensorID "5ba79370e624740019c40fc9"
// 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
float heatindex; // Hitzeindex
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 <= 19) {
wifiCounter ++;
delay(500);
Serial.print(".");
}
else {
Serial.println("Connection to wifi failed!");
Serial.println("Going to sleep now");
delay(100);
esp_deep_sleep_start(); // Wenn keine Verbindung zum WLAN hergestellt werden kann, geht der ESP32 in den Tiefschlaf
}
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// BME280 initialisieren
bool status;
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF );
// BME 280 abfragen
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0F;
// 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");
// Hitzeindex berechnen
float c1 = -8.784695;
float c2 = 1.61139411;
float c3 = 2.338549;
float c4 = -0.14611605;
float c5 = -1.2308094e-2;
float c6 = -1.6424828e-2;
float c7 = 2.211732e-3;
float c8 = 7.2546e-4;
float c9 = -3.582e-6;
heatindex = c1 + c2 * temperature + c3 * humidity + c4 * temperature * humidity + c5 * sq(temperature) + c6 * sq(humidity) + c7 * sq(temperature) * humidity + c8 * temperature * sq(humidity) + c9 * sq(temperature) * sq(humidity);
Serial.print("Heatindex: ");
Serial.print(heatindex);
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);
postFloatValue(heatindex, 1, heatindexSensorID);
// 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
iot/aussenthermometer/programmversion_0.1.txt · Zuletzt geändert: 23.06.2024 15:18 von Frickelpiet