MQTT assistance

I unhooked the DHT and got this

0J2%1"=2I=5"!Q9M=Ij
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!

Now my DB was and still is connecting to my wifi network but not getting the attempting MQTT connection is of concern I think

This is fine. The code just publishes MQTT messages if there are valid measurements:

if (isnan(h) || isnan(t)) {
  Serial.println("ERROR: Failed to read from DHT sensor!");
  return;
} else {
  publishData(t, h);
}

Oh, a connection should be possible. Just don’t expect messages of sensor readings.

If I unplug and then plug the db back in I keep getting that first line of crazy stuff then the warning message

+4
ERROR: Failed to read from DHT sensor!
ERROR: Failed to read from DHT sensor!

Should I re-upload the sketch to the db?

Slowly. Crazy stuff on boot-up is fine. I just attached a DHT22. This is the output if everything works fine:

wemos-mqtt-dht22-sketch-working

Okay, my measurements are a bit crazy. :stuck_out_tongue: But it’s more or less a proof of concept.

Okay, I wired up a DHT11, too and changed the define:

#define DHTPIN D6
#define DHTTYPE DHT11

The output is

⸮Hh⸮⸮⸮յ⸮0x2d
csum 0x2d
vdb886

INFO: Connecting to Greendale
.......
INFO: WiFi connected
INFO: IP address: 
192.168.130.56
INFO: Attempting MQTT connection...INFO: connected
{
  "temperature": "23.00",
  "humidity": "35.00"
}
INFO: Closing the MQTT connection
INFO: Closing the Wifi connection

And this is a picture of the setup:

I make the same change to the sketch and get an error stating

D6' was not declared in this scope

Wow. The sketch just doesn’t work out of the box. The serial output looks fine but the MQTT server doesn’t receive the messages.

$ mosquitto_sub -v -h localhost -t '#' -u username -P password

Does not produce any output. The log of mosquitto shows a timeout because the device is too slow:

1505030238: New connection from 192.168.130.56 on port 1883.
1505030238: New client connected from 192.168.130.56 as office_dht22 (c1, k15, u'sensors').
1505030260: Client office_dht22 has exceeded timeout, disconnecting.

Please tell me the full name of your development board / product you are using. You must select the right board at “Tools → Board → Wemos D1 mini (just for example)”.

This is the board.

NodeMCU ESP8266 WiFi Lua Development Board + Support CP2102 V1.0 IoT Module

changes to sketch verified correctly when I remembered to change the board type (yet another mistake by me, sorry)

I’ll try to upload to the db and rewire accordingly

ok, did the upload successfully and now nothing from the Serial Monitor

Did you wire up the sensor properly? If the grid points to you and the pins to the bottom:

  • Pin 1: 3.3V
  • Pin 2: D6 + a 4.7kOhm Pull-Up (-> 3.3V)
  • Pin 3: not connected
  • Pin 4: GND

I skipped the pull-up but it’s not recommended: http://www.micropik.com/PDF/dht11.pdf

it’s a 3 pin dht11 sensor so pin 1 is the data and it’s got the 4.7k ohm resistor attached and then on to D6

Pin 2 is to power and pin 3 is to GND

Does it looks like this one?

$_12

This is the one I have

I submitted a patch for another issue: https://github.com/mertenats/Open-Home-Automation/pull/19

Do you have a multimeter? Can you verify the circuit? Just use the continuity tester and verify the connection between the pin header and the sensor pins.

sadly I do not have one available currently

To summarize: If your sensor is properly wired and not destroyed you must see all log messages (mentioned above). As long as it doesn’t work please keep the wires as short as possible. If you can see proper measurements in the serial output you can care about the next step (MQTT). For me (with! the mentioned patch) it’s working now:

$ mosquitto_sub -v -h localhost -t '#' -u user -P password
office/sensor1 {"temperature":"24.00","humidity":"35.00"}
office/sensor1 {"temperature":"24.00","humidity":"35.00"}
office/sensor1 {"temperature":"24.00","humidity":"35.00"}

This is my setup:

configuration.yaml :

sensor: 
  - platform: mqtt
    state_topic: "sensor/livingroom/lux"
    name: "Lux Livingroom"
    qos: 0
    unit_of_measurement: "Lux"

  - platform: mqtt
    state_topic: "sensor/livingroom/humidity" 
    name: "Humidity Livingroom"
    qos: 0 
    unit_of_measurement: " %"

  - platform: mqtt
    state_topic: "sensor/livingroom/temperature"
    name: "Temperature Livingroom" 
    qos: 0
    unit_of_measurement: " ºC"

  - platform: mqtt
    state_topic: "sensor/livingroom/battery"
    name: "Battery Sensor Livingroom"
    qos: 0
    unit_of_measurement: " V"

(Maybe it’s not necessary with “double” -platform statement? But it works.)

Hardware:
ESP8266(Wemos D1 mini), DHT22, BH1750 light sensor and chargeable battery with the following .ino file:

#include <DHT.h> // Temp_hum sensor
#include <BH1750.h> // Lux Sensor
#include <Wire.h>
#include <ESP8266WiFi.h> //ESP WIFI
#include <PubSubClient.h> //MQTT
ADC_MODE(ADC_VCC);

const char *ssid =  "yourSSID"; 
const char *pass =  "yourPWD";
const int sleepTimeS = 300; //define deep sleep time

#define DHTTYPE DHT22
#define DHTPIN 2
#define MQTT_VERSION MQTT_VERSION_3_1_1
#define mqtt_server "yourMQTTServer"
#define mqtt_user "MQTTUser"
#define mqtt_password "MQTTPWD"
#define temperature_topic "sensor/livingroom/temperature" 
#define humidity_topic "sensor/livingroom/humidity"
#define battery_topic "sensor/livingroom/battery"
#define lux_topic "sensor/livingroom/lux"

BH1750 LightSensor;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
 
void setup() {
  Serial.begin(115200);
  Wire.begin();
  LightSensor.begin();
  dht.begin();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  client.setServer(mqtt_server, 1883);
}

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("ESP8266Client", mqtt_user, mqtt_password)) {
      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 loop() {
 if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
    uint16_t Lux = LightSensor.readLightLevel();
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float V = ESP.getVcc();
     
     Serial.print("Temperature: ");
     Serial.print(t);
     Serial.print(" degrees Celcius, Humidity: ");
     Serial.print(h);
     Serial.print("%.  "); 
     Serial.print("Voltage: ");
     Serial.print(V/1000);
     Serial.println(" V");
     Serial.print(Lux);
     Serial.println(" Lux");
     client.publish(lux_topic, String(Lux).c_str(), true);
     client.publish(temperature_topic, String(t).c_str(), true);
     client.publish(humidity_topic, String(h).c_str(), true);
     client.publish(battery_topic, String(V/1000).c_str(), true);

// Sleep
  Serial.println("ESP8266 in sleep mode");
  ESP.deepSleep(sleepTimeS * 1000000);
    }

This works like a charm…

1 Like