Statt mit einem Raspberry Pi kann man auch mit einem Wemos D1 Mini oder vergleichbarem die ausgelesene Temperatur eines DS18B20 Temperatursensors an eine Volkszähler Instanz schicken.
Benötigte Hardware:
- NodeMCU oder Wemos D1 Mini
- DS18B20
- 4,7 kOhm oder 10 kOhm Widerstand
- Breadboard
- Male – Female Jumper Wire oder Male -Male Jumper – Wire
Arduino Sketch:
#include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h> #include <Wire.h> #include <SPI.h> #include <OneWire.h> #include <DallasTemperature.h> #define SERIAL const String host = "IP-Adresse-des-VZ"; 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"; const unsigned char ONE_WIRE_BUS = D4; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensor(&oneWire); byte maxwait = 120; #define SEALEVELPRESSURE_HPA (1013.25) ESP8266WiFiMulti WiFiMulti; void setup() { #ifdef SERIAL Serial.begin(115200); Serial.println("BOOT"); Serial.print("Wifi..."); #endif sensor.begin(); Serial.print("Anzahl Sensoren: "); Serial.println(sensor.getDeviceCount()); WiFiMulti.addAP("SSID", "Passwort"); #ifdef SERIAL Serial.println("OK"); Serial.print("DS18B20"); #endif #ifdef SERIAL Serial.print("OK"); #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... { sensor.requestTemperatures(); float temperatur = sensor.getTempCByIndex(0); #ifdef SERIAL Serial.print("Temperature = "); Serial.println(temperatur); Serial.println(" *C"); #endif url_temp = url_start; url_temp += uuid_temp; url_temp += url_stop; url_temp += (temperatur); sendHttpData(url_temp); } #ifdef SERIAL Serial.println("SLEEP"); Serial.flush(); #endif }
Angepasst werden müssen:
- IP-Adresse
- WLAN Daten
- UUID-Temp
Anzeige