DHT11/DHT22, NodeMCU & Volkszähler

Im Beitrag „Raspberry Pi, DS18B20 & Volkszähler“ habe ich euch gezeigt wie ihr die gemessene Temperatur an den Volkszähler, welcher auf einem Raspberry Pi läuft, gesendet. In diesem Beitrag zeige ich euch wie ihr die Temperatur und Luftfeuchte mit einem DHT22 messt und ebenfalls an den Volkszähler sendet. Auch dieses mal greife ich auf den Quellcode von Florian Knodt zurück.

Benötigte Hardware:

  • NodeMCU
  • DHT22
  • 4,7 kOhm oder 10 kOhm Widerstand
  • Breadboard
  • Male – Female Jumper Wire oder Male -Male Jumper – Wire

Aufbau:

DHT11/DHT22, NodeMCU & Volkszähler

Sketch:

#include <Arduino.h>

#define SERIAL

const String host = "IP-Adresse";
const unsigned int port = 80;

const String url_start = "/middleware.php/data/";
const String url_stop = ".json?operation=add&value=";

const String uuid_temp = "UUID-Temp";
const String uuid_humid = "UUID-Humid";

byte maxwait = 120;

#define DHTPIN 2
#define DHTTYPE DHT22

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

ESP8266WiFiMulti WiFiMulti;

DHT_Unified dht(DHTPIN, DHTTYPE);

void setup() {
 #ifdef SERIAL
 Serial.begin(115200);
 Serial.println("BOOT");

 Serial.print("Wifi...");
 #endif

 WiFiMulti.addAP("Meins2-2,4GHz", "95259760514407391113");

 #ifdef SERIAL
 Serial.println("OK");
 Serial.print("DHT...");
 #endif

 dht.begin();

 #ifdef SERIAL
 Serial.print("OK");
 #endif

 sensor_t sensor;
 #ifdef SERIAL
 dht.temperature().getSensor(&sensor);
 Serial.println("------------------------------------");
 Serial.println("Temperature");
 Serial.print ("Sensor: "); Serial.println(sensor.name);
 Serial.print ("Driver Ver: "); Serial.println(sensor.version);
 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" °C");
 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" °C");
 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" °C");
 #endif
 dht.humidity().getSensor(&sensor);
 #ifdef SERIAL
 Serial.println("------------------------------------");
 Serial.println("Humidity");
 Serial.print ("Sensor: "); Serial.println(sensor.name);
 Serial.print ("Driver Ver: "); Serial.println(sensor.version);
 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
 Serial.println("------------------------------------");
 #endif
}

bool waitWifi() {
 while((WiFiMulti.run() != WL_CONNECTED) & maxwait > 0) {
 #ifdef SERIAL
 Serial.println("Wait Wifi");
 #endif
 delay(1000);
 maxwait--;
 }

 if(WiFiMulti.run() == WL_CONNECTED) return true;
 return false;
}

void sendHttpData(String url) {
 HTTPClient http;

 if(waitWifi()) {

 #ifdef SERIAL
 Serial.print("GET: "); Serial.println(url);
 #endif
 http.begin(host, port, url); //HTTP
 int httpCode = http.GET();
 #ifdef SERIAL
 if(httpCode) {
 if(httpCode == 200) {
 String payload = http.getString();
 Serial.println(payload);
 }else{
 Serial.print("HTTP "); Serial.println(httpCode);
 }
 } else {
 Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
 }
 #endif
 }else{
 #ifdef SERIAL
 Serial.print("No WiFi available\n");
 #endif
 }
}

void loop() {

 String url_temp = "";

 #ifndef SERIAL
 digitalWrite(1, HIGH); //LED off
 #endif

 delay(2000); //If we've just started the power might be somewhat distorted - lets wait a bit to get things setteled...

 sensors_event_t event;
 
 dht.temperature().getEvent(&event);
 if (isnan(event.temperature)) {
 #ifdef SERIAL
 Serial.println("Error reading temperature!");
 #endif
 } else{
 #ifdef SERIAL
 Serial.print("Temperature: ");
 Serial.print(event.temperature);
 Serial.println(" °C");
 #endif
 url_temp = url_start;
 url_temp += uuid_temp;
 url_temp += url_stop;
 url_temp += event.temperature;

 sendHttpData(url_temp);
 }

 dht.humidity().getEvent(&event);
 if (isnan(event.relative_humidity)) {
 #ifdef SERIAL
 Serial.println("Error reading humidity!");
 #endif
 }
 else {
 #ifdef SERIAL
 Serial.print("Humidity: ");
 Serial.print(event.relative_humidity);
 Serial.println("%");
 #endif
 url_temp = url_start;
 url_temp += uuid_humid;
 url_temp += url_stop;
 url_temp += event.relative_humidity;

 sendHttpData(url_temp);
 }
 #ifdef SERIAL
 Serial.println("SLEEP");
 Serial.flush();
 #endif

 
}

Angepasst werden müssen:

  • IP-Adresse
  • UUID-Temp
  • UUID-Humid

Quelle: BitBastelei/Adlerweb

Anzeige


wallpaper-1019588
Tag des Adventskalenders
wallpaper-1019588
Das Literary Passion … Die Idee hinter dem Etablissement
wallpaper-1019588
CD-REVIEW: DOTE – Centre Court [EP]
wallpaper-1019588
GLAZ – Displayschutz für dein Smartphone
wallpaper-1019588
Marina Hoermanseder und die Austrias next Topmodels in Fürstenfeld
wallpaper-1019588
Skitour: Über den Rumpler auf den Hohen Dachstein
wallpaper-1019588
Top Ten Thursday #39
wallpaper-1019588
In Sicherheit