Der Event hat natürlich Appetit gemacht. Somit möchte ich neben dem DHT22 auch gerne 1-wire Sensoren anschließen. Ich denke, dass ich das Skript durch Beispiele aus dem Netz ganz gut erweitert habe. Es werden jedoch keine 1-wire Sensoren gefunden. Ich weiß nicht, ob es am Skript oder an der Verkabelung liegt?
Hier das Skript:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <MQTT.h>
#include <dht.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define LED_PIN LED_BUILTIN
#define DHT221_PIN D1
#define DHT222_PIN D2
#define ONE_WIRE_BUS D0
const char* wifi_ssid = "<SSID>";
const char* wifi_key = "<Key>";
const char* mqtt_host = "<IP>";
WiFiClient wifi_client;
MQTTClient mqtt_client;
//1-wire Instanz um mit Devices zu kommunizieren
OneWire oneWire(ONE_WIRE_BUS);
//oneWire-Referenz zum Dallas-Temperatursensor
DallasTemperature sensors(&oneWire);
//Definition der DHT-Sensoren als Variable
DHT dht221_sensor(DHT221_PIN, DHT22);
DHT dht222_sensor(DHT222_PIN, DHT11);
// variable to hold device addresses
DeviceAddress Thermometer;
int deviceCount = 0;
//Adressen der 1-wire-Sensoren ermitteln
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(", ");
}
Serial.println("");
}
void setup() {
// Configure pins for LED and DHT
pinMode(LED_PIN, OUTPUT);
pinMode(DHT221_PIN, INPUT);
pinMode(DHT222_PIN, INPUT);
// Set BaudRate to 9k6
Serial.begin(9600);
// Setup DHT
dht221_sensor.begin();
dht222_sensor.begin();
//Setup 1-wire Sensoren
sensors.begin();
// locate devices on the bus
Serial.println("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
Serial.println("Printing addresses...");
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
sensors.getAddress(Thermometer, i);
printAddress(Thermometer);
}
// Setup WiFi
WiFi.begin(wifi_ssid, wifi_key);
// Setup MQTT
mqtt_client.begin(mqtt_host, wifi_client);
}
void processWiFi() {
if(WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to WiFi: ");
Serial.println(wifi_ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Good. We have WiFi now!
Serial.println("... Success!");
}
}
void processMQTT() {
if(!mqtt_client.connected()) {
// Connect to MQTT server
Serial.print("Connecting to MQTT server: ");
Serial.println(mqtt_host);
while (!mqtt_client.connect("NodeMCU", "", "")) {
delay(500);
Serial.print(".");
}
// Awesome. MQTT is ready!
Serial.println("... Success!");
}
}
void loop() {
// Put this into the loop to also show reconnecting attempts
processWiFi();
// Put this into the loop to ensure we keep an active mqtt connection
processMQTT();
// LED off
digitalWrite(LED_PIN, HIGH);
Serial.println("LED is off");
mqtt_client.publish("/led", "LED is off");
delay(30000);
// Read temperature DHT 22_1
float temperature1 = dht221_sensor.readTemperature();
if(!isnan(temperature1)) {
Serial.print("Temperature 1: ");
Serial.println(temperature1);
mqtt_client.publish("/dht221/temperature", String(temperature1));
}
// Read temperature DHT 22_2
float temperature2 = dht222_sensor.readTemperature();
if(!isnan(temperature2)) {
Serial.print("Temperature 2: ");
Serial.println(temperature2);
mqtt_client.publish("/dht222/temperature", String(temperature2));
}
// LED on
digitalWrite(LED_PIN, LOW);
Serial.println("LED is on");
mqtt_client.publish("/led", "LED is on");
delay(50);
// Read humidity DHT 22_1
float humidity1 = dht221_sensor.readHumidity();
if(!isnan(humidity1)) {
Serial.print("Humidity 1: ");
Serial.println(humidity1);
mqtt_client.publish("/dht221/humidity", String(humidity1));
}
// Read humidity DHT 22_2
float humidity2 = dht222_sensor.readHumidity();
if(!isnan(humidity2)) {
Serial.print("Humidity 2: ");
Serial.println(humidity2);
mqtt_client.publish("/dht222/humidity", String(humidity2));
}
//Read 1-wire
sensors.requestTemperatures();
uint8_t sensor1[8] = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC }; //das ist noch keine ausgelesene Addresse sondern ein Beispiel aus dem Netz
float temp1wire = sensors.getTempC(sensor1);
Serial.print("Temperatur (1wire): ");
Serial.println(temp1wire);
mqtt_client.publish("/1wire/Tempsensor1", String(temp1wire));
}
Oder liegt es an der Verkabelung? Ich habe den mittlersten Pin des DS18B20 an D0 des NodeMCU angeschlossen und einen 4K7 Widerstand auf +3,3V. Der linke Pin ist auf Masse und rechts auf +3,3V (wenn man die flache Seite anschaut).
PS: Entschudligt, dass das Skript nicht so toll formatiert dargestellt wird. Irgendwie komme ich in dem neuen Editor hier noch nicht zurecht.