DHT22 -> ESP8266 (ESP-01) <=Wlan=> Mosquitto <- Fhem <- MQTT <- MQTT_Device

Begonnen von Papaloewe, 28 März 2015, 19:52:13

Vorheriges Thema - Nächstes Thema

Pf@nne

FHEM auf: DS415+ (Master), Raspberry Pi 2

sbiermann

Hier ist mein Sketch: https://github.com/sbiermann/ESP8266-DHT22-MQTT
Ist wie schon gesagt der Sketch von Jörg aber radikal um alles raus geworfen was ich nicht brauche.

magic

Hallo,

ich versuche nun schon seit Stunden den Sketch von Jörg oder sbiermann zu compilen jedoch ohne Erfolg.
Ich versuche es mit Arduino IDE 1.6.7 auf Windows 10 64bit.

Welche libs brauche ich denn?

im Paket von sbiermann wird zum beispiel abgebrochen weil die mqtt.h fehlt.

Könnte mir jemand ein Archiv mit allen notwendigen libs liefern?


Arduino: 1.6.7 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Serial, 115200, 4M (3M SPIFFS)"

ESPDHT22FHEM:74: error: no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, char [13], int&)'

PubSubClient client(ethClient, mqttBroker, mqttPort);

                                                    ^

C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino:74:52: note: candidates are:

In file included from C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino:11:0:

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:99:4: note: PubSubClient::PubSubClient(const char*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)

    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:99:4: note:   candidate expects 5 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:98:4: note: PubSubClient::PubSubClient(const char*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)

    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:98:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:97:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&, Stream&)

    PubSubClient(const char*, uint16_t, Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:97:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:96:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&)

    PubSubClient(const char*, uint16_t, Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:96:4: note:   no known conversion for argument 1 from 'WiFiClient' to 'const char*'

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:95:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)

    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:95:4: note:   candidate expects 5 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:94:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)

    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:94:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:93:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&, Stream&)

    PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:93:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:92:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&)

    PubSubClient(uint8_t *, uint16_t, Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:92:4: note:   no known conversion for argument 1 from 'WiFiClient' to 'uint8_t* {aka unsigned char*}'

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:91:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&, Stream&)

    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:91:4: note:   candidate expects 5 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:90:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, void (*)(char*, uint8_t*, unsigned int), Client&)

    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:90:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:89:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)

    PubSubClient(IPAddress, uint16_t, Client& client, Stream&);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:89:4: note:   candidate expects 4 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:88:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)

    PubSubClient(IPAddress, uint16_t, Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:88:4: note:   no known conversion for argument 1 from 'WiFiClient' to 'IPAddress'

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:87:4: note: PubSubClient::PubSubClient(Client&)

    PubSubClient(Client& client);

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:87:4: note:   candidate expects 1 argument, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:86:4: note: PubSubClient::PubSubClient()

    PubSubClient();

    ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:86:4: note:   candidate expects 0 arguments, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:67:7: note: constexpr PubSubClient::PubSubClient(const PubSubClient&)

class PubSubClient {

       ^

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:67:7: note:   candidate expects 1 argument, 3 provided

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:67:7: note: constexpr PubSubClient::PubSubClient(PubSubClient&&)

C:\Users\magic\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:67:7: note:   candidate expects 1 argument, 3 provided

ESPDHT22FHEM:77: error: no matching function for call to 'DHT::DHT()'

DHT dht;

     ^

C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino:77:5: note: candidates are:

In file included from C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino:9:0:

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:40:4: note: DHT::DHT(uint8_t, uint8_t, uint8_t)

    DHT(uint8_t pin, uint8_t type, uint8_t count=6);

    ^

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:40:4: note:   candidate expects 3 arguments, 0 provided

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:38:7: note: constexpr DHT::DHT(const DHT&)

class DHT {

       ^

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:38:7: note:   candidate expects 1 argument, 0 provided

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:38:7: note: constexpr DHT::DHT(DHT&&)

C:\Users\magic\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:38:7: note:   candidate expects 1 argument, 0 provided

C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino: In function 'void setup()':

ESPDHT22FHEM:148: error: 'startSensors' was not declared in this scope

    startSensors();

                 ^

ESPDHT22FHEM:154: error: 'processDHT' was not declared in this scope

       if(processDHT()) //read DHT22

                     ^

ESPDHT22FHEM:157: error: 'processMqttItems' was not declared in this scope

         processMqttItems();//send temp and hum to MQTT Broker

                          ^

C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino: In function 'void startSensors()':

ESPDHT22FHEM:188: error: 'class DHT' has no member named 'setup'

    dht.setup(DHT22_PIN);

        ^

C:\Users\magic\Documents\Arduino\ESPDHT22FHEM\ESPDHT22FHEM.ino: In function 'boolean processDHT()':

ESPDHT22FHEM:204: error: 'class DHT' has no member named 'readSensor'

   dht.readSensor();

       ^

ESPDHT22FHEM:205: error: 'class DHT' has no member named 'getStatus'

   if (dht.getStatus() != dht.ERROR_NONE)

           ^

ESPDHT22FHEM:205: error: 'class DHT' has no member named 'ERROR_NONE'

   if (dht.getStatus() != dht.ERROR_NONE)

                              ^

ESPDHT22FHEM:208: error: 'class DHT' has no member named 'getStatusString'

     if (debugOutput) Serial.println(dht.getStatusString());

                                         ^

ESPDHT22FHEM:212: error: 'class DHT' has no member named 'getTemperature'

   temperature = dht.getTemperature();

                     ^

ESPDHT22FHEM:219: error: 'class DHT' has no member named 'getHumidity'

   humidity = dht.getHumidity();

                  ^

exit status 1
no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, char [13], int&)'

  Dieser Report hätte mehr Informationen mit
  "Ausführliche Ausgabe während der Kompilierung"
  aktiviert in Datei > Einstellungen.


Gruß

sbiermann

Hier sind meine beiden Libraries die ich nutze. Einmal für den DHT22 und für MQTT.

Mickbaer

Hallo,
die Arduino IDE 1.6.7 scheint für den ESP8266 buggy zu sein.
Versuchs mal mit der 1.6.5 das geht wesentlich besser.   :)
Gruß
Mickbaer aus Berlin

Familienpapi

Die 1.6.5 ist die letzte Version, die mit dem ESP8266 zuverlässig funktioniert.
Am besten "portable" arbeiten. So kann man mehrere Versionen für die jeweiligen Bausteine passend nebeneinander haben.
FHEM@RPi4, piVCCU3@RPi3 (nur Homematic IP), boot via USB NVME SSD, keine SDs,
FTUI 3, HMCCU, MQTT(Mosquitto), MobileAlerts, JeelinkV3c868 (LaCrosse), ZWAVE(+), TelegramBot, eigene Heizungssteuerung, Configurable Firmata
ESP8266 MQTT mit eigener Firmware / Framework

magic

Nabend,
soweit sogut :-)

Ich habe den Sketch von sbiermann erfolgreich compiliert und auf ein Nodemcu v0.9 hochgeladen.
Das Wifi funktioniert, er connected auf einen MQTT. Jedoch habe ich noch mit dem DHT-22 Probleme.

Er hängt an D4 des NodeMCU, das Pin Setting ist 2.

Ich habe nun mal ein bisschen und her experimentiert und mir ist dabei aufgefallen das der DHT22 sich nur auslesen lässt wenn ich ihn nach dem boot kurz von der 3.3vcc trenne und nicht in den deep sleep gehe. Das verwundert mich sehr. Hat da jemand eine Idee zu?

Ich habe Pin1 vom dht22 an 3.3v, Pin 2 an GPIO-2, Pin 4 an GND.

Ich verwende so ein Breakout Board:
http://www.ebay.de/itm/DHT22-AM2302-Luft-Feuchtigkeit-Feuchte-Temperatur-Sensor-Modul-Fur-Arduino-TE248-/201389650576?hash=item2ee3c23290:g:D4sAAOSwKtlWlgqk


Log:
ZitatConnecting to ChuckNorris
f 0, ....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
pm open phy_2,type:1 0 0
cnt

connected with ChuckNorris, channel 1
ip:10.0.0.200,mask:255.255.0.0,gw:10.0.0.1
.
WiFi connected
IP address:
10.0.0.200
sleep enable,type: 1
Wifi Started
WiFi connection time 3681 ms
MQTT is connected
DHT22: +++DHT start
DHT22 Error: TIMEOUT
MQTT is disconnected
state: 5 -> 0 (0)
rm 0
pm close 7 0 39618/612308
WiFi is disconnected
sleeping for 30 seconds
del if0
usl
sul 0 0
ESP8266 run complete in 20428 ms
deep sleep 9s

Code:
Zitat/*************************************************/
/* Includes                                      */
/*************************************************/
#include <stdio.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include "Client.h"

#include <DHT.h>
#include <MQTT.h>
#include <PubSubClient.h>

#include <Wire.h>

// Include API-Headers
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}

/*************************************************/
/* Debugging                                     */
/*************************************************/
const bool debugOutput = true;  // set to true for serial OUTPUT

/*************************************************/
/* Definitions                                   */
/*************************************************/
// in seconds, time between measurements, sketch goes into deep sleep between readings
// should be at least 30secs, to thingspeak only allows update every 20s and script excekution is about 20s
#define REPEAT_INTERVAL 30

#define SERIAL_SPEED 115200

#define DHT22_PIN 2       // Pin 12 as 1-wire bus for DHT22

/*************************************************/
/* Settings for WLAN                             */
/*************************************************/
const char* ssid = "ChuckNorris";
const char* password = "xxXxxx";

/*************************************************/
/* Static IP                                     */
/*************************************************/
IPAddress ip(10,0,0,200);
IPAddress gateway(10,0,0,1);
IPAddress subnet(255,255,0,0);

/*************************************************/
/* Settings for MQTT                             */
/*************************************************/
char mqttBroker[] = "10.0.0.5";
int  mqttPort = 1883;

#define MQTTDeviceID "01"
#define MQTTClientID "ESP8266." MQTTDeviceID
#define MQTT_USER ""
#define MQTT_PASSWORD ""

#define TOPIC_ClientID "ESP8266/" MQTTDeviceID
#define TOPIC_LastWill  TOPIC_ClientID "/connected"
#define TOPIC_HumidityState  TOPIC_ClientID "/humidity/value"
#define TOPIC_TemperatureState  TOPIC_ClientID "/temperature/value"

// Create an WiFiClient object, here called "ethClient":
WiFiClient ethClient;

//Create MQTT client object
PubSubClient client(ethClient, mqttBroker, mqttPort);

// Create an DHT object, here called "dht":
DHT dht;
float temperature;
float humidity;
char charBuffer[32];

int wifiConnectCounter = 0;

// Pin14: VCC for Sensors
const int GPIO_Power_Sensors = 14;


// ------------------------
void setup() {
   Serial.begin(115200);   

   if (debugOutput) Serial.println("ESP8266 starts...");

   // power on sensors
   pinMode(GPIO_Power_Sensors, OUTPUT);
   digitalWrite(GPIO_Power_Sensors, HIGH);
   //disable internal LED
   pinMode(2, OUTPUT);
   digitalWrite(2, HIGH );
   long start = millis();
   
   if (debugOutput) Serial.println("Wifi wait for connection");

   system_deep_sleep_set_option(1); // alt. 2 to not recalibrate WiFi after wake up, 1 is more stable needs more power
   wifi_set_sleep_type (LIGHT_SLEEP_T);

   if (debugOutput) Serial.setDebugOutput(true); // enable WIFI Debug output
   if (debugOutput) Serial.println();
   if (debugOutput) Serial.println();
   if (debugOutput) Serial.print("Connecting to ");
   if (debugOutput) Serial.println(ssid);

   // Connect to WiFi network
   WiFi.mode(WIFI_STA);
   yield();

   WiFi.disconnect();
   yield();

   WiFi.begin(ssid, password);
   WiFi.config(ip, gateway, subnet);

   // Wait for connection
   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     wifiConnectCounter++;
     if (debugOutput)  Serial.print(".");
     if (wifiConnectCounter > 100) {
       if (debugOutput)  Serial.println("No WiFi connection, try again in " + String(REPEAT_INTERVAL) + " seconds");
       system_deep_sleep(REPEAT_INTERVAL * 1000 * 1000); // after deep sleep automtic reset
       delay(200); // give some time to activate sleep state
     }
   }
   if (debugOutput) Serial.println("");
   if (debugOutput) Serial.println("WiFi connected");
   if (debugOutput) Serial.println("IP address: ");
   if (debugOutput) Serial.println(WiFi.localIP());
   wifi_set_sleep_type (LIGHT_SLEEP_T);

   if (debugOutput) Serial.println("Wifi Started");
   client.disconnect();
   //wait at least 3 seconds...
   long wait = millis()-start;
   if (debugOutput) Serial.println("WiFi connection time "+String(wait)+" ms");
   if( wait < 3000)
    delay(3000-wait);
   //starting the sensor
   startSensors();
   // connect to MQTT Broker
   while (!client.connected()) {
   if (client.connect((char *)TOPIC_ClientID,(char *)TOPIC_LastWill,1,1,(char *)"0")) 
    {
      if (debugOutput) Serial.println("MQTT is connected");
      if(processDHT()) //read DHT22
      {
        yield();
        processMqttItems();//send temp and hum to MQTT Broker
        delay(500);
      }
    }
    else
    {
      if (debugOutput) Serial.println("MQTT is not connected... retrying");
      delay(200);
    }
  }
    // disconnect from MQTT
    client.disconnect();
    if (debugOutput) Serial.println("MQTT is disconnected");
    yield();

    // disconnect from WiFi
    WiFi.disconnect();
    if (debugOutput) Serial.println("WiFi is disconnected");
    yield();


  // all done enable deep_sleep
   long sleep_dur = REPEAT_INTERVAL * 1000 * 1000 - millis() * 1000;
   if (debugOutput) Serial.println("sleeping for " + String(REPEAT_INTERVAL) + " seconds");
   system_deep_sleep(sleep_dur);
   if (debugOutput) Serial.println("ESP8266 run complete in "+String(millis())+" ms");
   delay(1000);//waiting a little bit, deep sleep needs some time to start
}

void startSensors(void) {
   // Setup DHT22
   dht.setup(DHT22_PIN);
}

// ------------------------
// publish mit retain
boolean myPublish(char* topic, char* payload)
{
  client.publish(topic,(uint8_t*)payload,strlen(payload),true);
}

// ------------------------
// process DHT22
boolean processDHT()
{
  if (debugOutput) Serial.println("DHT22: +++DHT start");
 
  dht.readSensor();
  if (dht.getStatus() != dht.ERROR_NONE)
  {
    if (debugOutput) Serial.print("DHT22 Error: ");
    if (debugOutput) Serial.println(dht.getStatusString());
    return false;
  }

  temperature = dht.getTemperature();
  if (isnan(temperature))
  {
    if (debugOutput) Serial.println("DHT22: Failed reading temperature from DHT");
  }
  if (debugOutput) Serial.print("DHT22 Deg: "); Serial.println(temperature);
 
  humidity = dht.getHumidity();
  if (isnan(humidity)) {
    if (debugOutput) Serial.println("DHT22: Failed reading humidity from DHT");
  }
  if (debugOutput) Serial.print("DHT22 Hum: "); Serial.println(humidity);
  if (debugOutput) Serial.println("DHT22: ---DHT done");
  return true;
}

// ---------------------------
// processes the items to be published
void processMqttItems()
{
  String strBuffer;
  strBuffer =  String(humidity);
  strBuffer.toCharArray(charBuffer,10);
  myPublish((char *)TOPIC_HumidityState,charBuffer); 
  if (debugOutput) Serial.println("Action: update hum"); 
  yield();

  strBuffer =  String(temperature);
  strBuffer.toCharArray(charBuffer,10);
  myPublish((char *)TOPIC_TemperatureState,charBuffer); 
  if (debugOutput) Serial.println("Action: update temp"); 

}
// -------------------------------

void loop() {
  //not needed
}

Gruß magic

wingfighter

Hallo Magic

Wenn Du das Modul aus dem Link verwendest und Pin 1 links ist, muss an 1 die Masse als GND, an 2 (MItte) +3,3V und an PIN 3 rechts kommt die Verbindung zum GPIO in Deine Fall GPIO2 hin.
Wenn Du ein Modul mit vier Anschlüssen verwendest, also ohne das Board, dann  muss zwischen VCC (+3,3V) und der Datenleitung noch ein ca. 5-10kOhm-Widerstand geschaltet werden.

Den Quelltext habe ich mir auch mal angeguckt.
Die DHT-Library, die Du verwendest hatte ich bei meinen Versuchen auch im Test. Das sieht mir nach dieser aus https://github.com/markruys/arduino-DHT
Da kamen bei meinen Versuchen allerdings keine plausiblen Werte raus.

Versuch mal diese hier: https://github.com/adafruit/DHT-sensor-library
Mit der habe ich gute Erfahrungen gemacht.

Gruß Wingfighter