Verschiedenes > ESP8266

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

<< < (35/35)

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.

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:

--- Zitat ---Connecting 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

--- Ende Zitat ---

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
}

--- Ende Zitat ---

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

Navigation

[0] Themen-Index

[*] Vorherige Sete

Zur normalen Ansicht wechseln