DHT22 direkt am MySensors Gateway will nicht

Begonnen von Muellermann, 27 Januar 2018, 14:55:39

Vorheriges Thema - Nächstes Thema

Muellermann

Hallo zusammen,

ich habe gerade begonnen mit einen MySensors WLAN Gateway.

Bis die zusätzlichen Nodes eintreffen und eingebunden werden, habe ich direkt am Gateway (Wemos D1 Mini) einen DHT22 angeschlossen, dessen Ausgabe ich auch im seriellen Monitor vom Arduino IDE sehe.

Das Gateway habe ich in FHEM eingebunden, aber von dem DHT22 sehe ich leider absolut nichts.

Ich stehe ziemlich auf dem Schlauch und weiss nicht, was ich falsch mache, bzw. noch nicht gemacht habe.

So schaut der Code vom Gateway im Arduino IDE aus.

// ** WiFi and network configuration **
#define MY_ESP8266_SSID "XXXXXXX"
#define MY_ESP8266_PASSWORD "XXXXXXX"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
#define MY_ESP8266_HOSTNAME "MySWeMosGWShield"

// Enable UDP communication
//#define MY_USE_UDP

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 172,25,0,194

// If using static ip you need to define Gateway and Subnet address as well
//#define MY_IP_GATEWAY_ADDRESS 172,xxx,xxx,xxx
//#define MY_IP_SUBNET_ADDRESS 255,255,0,0

// The port to keep open on node server mode
#define MY_PORT 5003

// How many clients should be able to connect to this gateway (default 1)
#define MY_GATEWAY_MAX_CLIENTS 2

// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68


// ** OTA updates configuration **
// Enable OTA updates for ESP8266 based gateway
#define ESP8266_OTA

// Define password for OTA updates (recommended but optional)
//#define ESP8266_OTA_PASSWORD "sloth1501"


// ** MySensors Radio configuration **
// Enables and select radio type (if attached)
#define MY_RADIO_NRF24
#define MY_RF_24_PA_LEVEL RF24_PA_MIN
//#define MY_RADIO_RFM69

// RF24 settings
// Use custom RF24 channel (default 76)
//#define MY_RF24_CHANNEL 42
// Decrease RF24 power transmission, useful to test in case of Tx problems.
// If your problem is fixed consider adding the 5V to 3.3V voltage regulator
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
// Enables RF24 encryption (all nodes and gateway must have this enabled, and all must be personalized with the same AES key)
//#define MY_RF24_ENABLE_ENCRYPTION

// RF69 settings
// RFM69 Frequency, default = 868MHz
#define MY_RFM69_FREQUENCY RF69_868MHZ // RFM69 frequency to use (RF69_433MHZ for 433MHz, RF69_868MHZ for 868MHz or RF69_915MHZ for 915MHz)
// RFM69 Network ID. Use the same for all nodes that will talk to each other, default = 100
//#define MY_RFM69_NETWORKID     42
// Enables RFM69 encryption (all nodes and gateway must have this enabled, and all must be personalized with the same AES key)
//#define MY_RFM69_ENABLE_ENCRYPTION
// Disable this if you're not running the RFM69HW model (RFM69HW is recommended on the gateway for better coverage)
#define MY_IS_RFM69HW


// ** Mysensors additional functions **
// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE

// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  D1  // GPIO 0

// Software signing settings
//#define MY_SIGNING_SOFT
#define MY_SIGNING_SOFT_RANDOMSEED_PIN A0

// Hardware signing settings (currently unsupported?)
//#define MY_SIGNING_ATSHA204
#define MY_SIGNING_ATSHA204_PIN D0  // GPIO 16

// General signing settings
// Enable this if you want destination node to sign all messages sent to this gateway.
//#define MY_SIGNING_REQUEST_SIGNATURES
// Enable node whitelisting
//#define MY_SIGNING_NODE_WHITELISTING {{.nodeId = GATEWAY_ADDRESS,.serial = {0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01}}}

// Flash leds on rx/tx/err
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300

// Led pins used if blinking feature is enabled above
// LED_BUILTIN, the on board LED is used (D4, GPIO 2)
#define MY_DEFAULT_ERR_LED_PIN LED_BUILTIN // Error led pin
#define MY_DEFAULT_RX_LED_PIN  LED_BUILTIN // Receive led pin
#define MY_DEFAULT_TX_LED_PIN  LED_BUILTIN // Transmit led pin

// Enable debug prints to serial monitor
#define MY_DEBUG


// **************************************//
// You probably don't need to edit below //
// **************************************//

#include <EEPROM.h>
#include <SPI.h>
#if defined ESP8266_OTA
#include <ArduinoOTA.h>
#endif
#if defined(MY_USE_UDP)
#include <WiFiUDP.h>
#else
#include <ESP8266WiFi.h>
#endif

#include "DHT.h"

#define DHTPIN D4     // what pin we're connected to

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

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);





// Set the gateway type to ESP8266
#define MY_GATEWAY_ESP8266

// Disable serial and allow TX and RX to be used as GPIO when debug is disabled
#ifndef MY_DEBUG
#define MY_DISABLED_SERIAL
#endif

// Enable signing related debug prints to serial monitor if debug and signing are both enabled
#if ((defined MY_DEBUG) && ( defined MY_SIGNING_SOFT || defined MY_SIGNING_ATSHA204))
#define MY_DEBUG_VERBOSE_SIGNING
#endif

// Define RFM69 pins to match PCB traces
#if defined MY_RADIO_RFM69
#define MY_RF69_IRQ_PIN D2  // GPIO 4
#define MY_RF69_IRQ_NUM D2  // GPIO 4
#define MY_RF69_SPI_CS  D8  // GPIO 15
#endif

#define ARDUINO_ARCH_ESP8266

#include <MySensors.h>


void setup() {
  // Executed once at boot


 
#if defined ESP8266_OTA
#if defined MY_DEBUG
  Serial.println("");
  Serial.println("ESP8266 OTA enabled");
#endif

#if defined ESP8266_OTA_PASSWORD
#if defined MY_DEBUG
  Serial.println("OTA Password set");
#endif
  ArduinoOTA.setPassword(ESP8266_OTA_PASSWORD);
#endif

  ArduinoOTA.setHostname(MY_ESP8266_HOSTNAME);
  ArduinoOTA.onStart([]() {
    Serial.println("ArduinoOTA start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nArduinoOTA end");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
#endif

#if defined MY_DEBUG
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Not connected to WiFi");
  } else {
    Serial.println("");
    Serial.println("-- STA (client) informations --");
    Serial.print("Hostname: ");
    Serial.println(WiFi.hostname());
    Serial.print("Connected to: ");
    Serial.print(WiFi.SSID());
    Serial.print(" (");
    Serial.print(WiFi.BSSIDstr());
    Serial.print("), ");
    Serial.print(WiFi.RSSI());
    Serial.println("dBm");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway address: ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("Subnet mask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("MAC STA address: ");
    Serial.println(WiFi.macAddress());
    Serial.println("-- AP (server) informations --");
    Serial.print("Soft IP address: ");
    Serial.println(WiFi.softAPIP());
    Serial.print("MAC AP address: ");
    Serial.println(WiFi.softAPmacAddress());
    Serial.println("----");
    Serial.println("");
  }
#endif
}


void presentation() {
  // Present locally attached sensors here

}

  void loop() {

delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Impossible de lire la sonde DHT!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");

   
  // Send locally attech sensors data here
#if defined ESP8266_OTA
  ArduinoOTA.handle();
#endif
}


In FHEM habe ich das Gateway so definiert:

define gateway MYSENSORS 172.xxx.xxx.xxx
attr gateway first-sensorid 10
attr gateway requestAck 1
attr gateway room mySensors
attr gateway stateFormat connection


Kann mir da jemand von euch evtl. auf die Sprünge helfen?

Herzlichen Dank und viele Grüße, sowie ein schönes Wochenende

  Andreas

/Edit:  Das Gateway an sich ist in FHEM sichtbar und auch ansprechbar, nur der DHT22 halt nicht :(

PeMue

Hallo Andreas,

ich habe mir den Sketch nicht genauer angeschaut, aber so wie ich es gesehen habe, wird der DHT22 über die serielle Schnittstelle ausgegeben. Dein Gateway ist aber für WLAN konfiguriert. Häng mal den WeMos an eine serielle Schnittstelle und schaue mit einem Terminal, was ankommt. Wenn da was ankommt, musst Du "nur" noch die Messwerte auf WLAN umbiegen bzw. in einem für ein FHEM Modul geeigneten Format ausgeben  ;D

Gruß PeMue
RPi3Bv1.2 rpiaddon 1.66 6.0 1xHM-CC-RT-DN 1.4 1xHM-TC-IT-WM 1.1 2xHB-UW-Sen-THPL-O 0.15 1x-I 0.14OTAU  1xCUNO2 1.67 2xEM1000WZ 2xUniroll 1xASH2200 3xHMS100T(F) 1xRFXtrx 90 1xWT440H 3xTFA30.3150 5xFA21
RPi1Bv2 LCDCSM 1.63 5.8 2xMAX HKT 1xMAX RT V200KW1 Heizung Wasser

Muellermann

Hallo PeMue,

auf der seriellen Schnittstelle kommen die gewünschten Werte an.
Das 'nur' noch aufs WLAN umbiegen macht mir etwas Angst. Ich habe noch keine Ahnung wie.

Das Gateway geht übrigens nach einer Sekunde von 'connected' auf 'Startup Complete'

Ich dachte bisher, dass FHEM über den Port 5003 auf die serielle Ausgabe vom Wemos zugreift.

PeMue

Zitat von: Muellermann am 27 Januar 2018, 15:27:24
Ich dachte bisher, dass FHEM über den Port 5003 auf die serielle Ausgabe vom Wemos zugreift.
Nein, das Gateway leitet nur die Ausgaben, die normalerweise über seriell/USB kommen, auf WLAN um. Die Sensorik musst Du getrennt behandeln, ich weiß nur im Moment nicht, wie.

Gruß Peter
RPi3Bv1.2 rpiaddon 1.66 6.0 1xHM-CC-RT-DN 1.4 1xHM-TC-IT-WM 1.1 2xHB-UW-Sen-THPL-O 0.15 1x-I 0.14OTAU  1xCUNO2 1.67 2xEM1000WZ 2xUniroll 1xASH2200 3xHMS100T(F) 1xRFXtrx 90 1xWT440H 3xTFA30.3150 5xFA21
RPi1Bv2 LCDCSM 1.63 5.8 2xMAX HKT 1xMAX RT V200KW1 Heizung Wasser

Markus.

#4
Also für mich sieht der Sketch so aus, als wenn nur Debug Messages verarbeitet werden.
Die Nachrichten ans Gateway sollten eigentlich so aussehen.
Z.B Luftfeuchtigkeit.
send(msgHum.set(humidity, 1));
Irgendwie fehlt mir in dem Loop des Sketchs genau dieser Teil.

Ich würde auch grundsätzlich bei sowas mit dem einfachsten Gatewaysketch aus den Beispielen anfangen, ohne OTA und Verschlüsselung.

Sowas wie hier das sollte erstmal gehen, richtige Libs vorrausgesetzt:

// Enable debug prints to serial monitor
#define MY_DEBUG

// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
#define MY_BAUD_RATE 9600

// Enables and select radio type (if attached)
#define MY_RADIO_NRF24
#define MY_RF_24_PA_LEVEL RF24_PA_MIN

#define MY_GATEWAY_ESP8266

#define MY_ESP8266_SSID "SSID"
#define MY_ESP8266_PASSWORD "Passwort"

// The port to keep open on node server mode
#define MY_PORT 5003

// How many clients should be able to connect to this gateway (default 1)
#define MY_GATEWAY_MAX_CLIENTS 2

#if defined(MY_USE_UDP)
#include <WiFiUdp.h>
#endif

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <MySensors.h>
#include <DHT.h>

#define DHT_DATA_PIN 4

// Set this offset if the sensor has a permanent small offset to the real temperatures
#define SENSOR_TEMP_OFFSET 0

// Sleep time between sensor updates (in milliseconds)
// Must be >1000ms for DHT22 and >2000ms for DHT11
static const uint64_t UPDATE_INTERVAL = 2000;

// Force sending an update of the temperature after n sensor reads, so a controller showing the
// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
// the value didn't change since;
// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
static const uint8_t FORCE_UPDATE_N_READS = 10;

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1


float lastTemp;
float lastHum;
uint8_t nNoUpdatesTemp;
uint8_t nNoUpdatesHum;
bool metric = true;

MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
DHT dht;


void presentation() 
{
  // Send the sketch version information to the gateway
  sendSketchInfo("TemperatureAndHumidity", "1.1");

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID_HUM, S_HUM);
  present(CHILD_ID_TEMP, S_TEMP);

  metric = getControllerConfig().isMetric;
}


void setup()
{
  dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
    Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  }
  // Sleep for the time of the minimum sampling period to give the sensor time to power up
  // (otherwise, timeout errors might occure for the first reading)
  sleep(dht.getMinimumSamplingPeriod());
}


void loop()     

  // Force reading sensor, so it works also after sleep()
  dht.readSensor(true);

  // Get temperature from DHT library
  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
    Serial.println("Failed reading temperature from DHT!");
  } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
    // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
    lastTemp = temperature;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    // Reset no updates counter
    nNoUpdatesTemp = 0;
    temperature += SENSOR_TEMP_OFFSET;
    send(msgTemp.set(temperature, 1));

    #ifdef MY_DEBUG
    Serial.print("T: ");
    Serial.println(temperature);
    #endif
  } else {
    // Increase no update counter if the temperature stayed the same
    nNoUpdatesTemp++;
  }

  // Get humidity from DHT library
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
    // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
    lastHum = humidity;
    // Reset no updates counter
    nNoUpdatesHum = 0;
    send(msgHum.set(humidity, 1));

    #ifdef MY_DEBUG
    Serial.print("H: ");
    Serial.println(humidity);
    #endif
  } else {
    // Increase no update counter if the humidity stayed the same
    nNoUpdatesHum++;
  }

  // Sleep for a while to save energy
  sleep(UPDATE_INTERVAL);
}

Da es ja ein Gateway ist würde ich das noch mit dem Sleep ím Loop anders machen.

Gruß

Markus

Muellermann

Auch hier nochmal vielen Dank.

Nun läuft es. Manchmal braucht man helt einen Schubs in die richtige Richtung :)