Tut09: ESP8266 füttert Mosquitto (mqtt-Broker)

https://codebender.cc/sketch:316691#esp8266%20dht11%20mqtt%20-%20working.ino

-> Zeit von 60s auf 6s verringern

-> Umstellen in C°

Angepasster/verkürzter Code:

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include "DHT.h"

#define DHTPIN D2     // what pin we're connected to
#define wifi_ssid "SSID"
#define wifi_password "Passwort"

#define mqtt_server "10.16.225.X"
#define humidity_topic "feuchtigkeit"
#define temperature_topic "temperatur"

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("mqtt-Test");
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  dht.begin();
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("changeMe")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

float temp = 0.0;
float hum = 0.0;

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  delay(5000);
  float newHum = dht.readHumidity();
    float newTemp = dht.readTemperature();

      temp = newTemp;
      Serial.print("New temperature:");
      Serial.println(String(temp).c_str());
      client.publish(temperature_topic, String(temp).c_str(), true);
    

      hum = newHum;
      Serial.print("New humidity:");
      Serial.println(String(hum).c_str());
      client.publish(humidity_topic, String(hum).c_str(), true);
   }

Mosquitto dazu:

  1. apt-get install mosquitto mosquitto-clients
  2. Mosquitto beobachten: tail -f /var/log/mosquitto/mosquitto.log
  3. Auf beliebigem Client alle Themen abonnieren (subscribe):
    mosquitto_sub -h 10.0.0.253  -t "#"
  4. Mosquitto von Client füttern:
    mosquitto_pub -h 10.0.0.253 -t "ha/test" -m "Hello"
  5. EP8266 mit „Füttersketch“ flashen

Danach auf ioBroker umsteigen, lediglich das mqtt-Ziel muss im Sketch verändert werden.
Hier nochmal fuer den DS18B20 als Sensor

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include <OneWire.h>

#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS D2

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

// Update these with values suitable for your network.
const char* ssid = "SSID";
const char* password = "PWD";
const char* mqtt_server = "IP-MQTT-Server";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
float temp = 0;
int inPin = 5;

void setup_wifi() {
 delay(10);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) 
 {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
}

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect("arduinoClient_temperature_sensor")) {
 Serial.println("connected");
 } else {
 Serial.print("failed, rc=");
 Serial.print(client.state());
 Serial.println(" try again in 5 seconds");
 // Wait 5 seconds before retrying
 delay(5000);
 }
 }
}
 
void setup()
{
 Serial.begin(115200);
 setup_wifi(); 
 client.setServer(mqtt_server, 1883);
 pinMode(inPin, INPUT);
 sensors.begin();
}

void loop()
{
 if (!client.connected()) {
 reconnect();
 }
 client.loop();

long now = millis();
 if (now - lastMsg > 6000) {
 lastMsg = now;
 sensors.setResolution(12);
 sensors.requestTemperatures(); // Send the command to get temperatures
 temp = sensors.getTempCByIndex(0);
 Serial.println(temp);
 if((temp > -20) && (temp <60))
 {
 client.publish("t02", String(temp).c_str(),TRUE);
 }
 }
}
Advertisements