MQTT assistance

Yes, I will definitely do that as well. Not so important on this first one based on where it is but the one’s for the kids room and out the back will definitely need a light sensor.

Thanks again for all your assistance. Off to build some Sonoff wifi switches now

misread your post first.
but now I have to ask, if you have a Pi for HA then why do you need NodeMCU?
You can connect the DHT11 directly to the gpio pins of the Pi

cheers
tom

Normally you don’t have a raspberry pi per room.

1 Like

maybe I am not seeing the point here, but the DHT11 can be powered with 3-5.5 volts, the pi has 4 pins for power output that means you can measure at least 4 rooms without using a separate power supply.
I am saying “at least” because the gpio pin can output cca. 40-50 mA, the DHT11 takes a max. of 2.5mA, which means that one 5 V gpio pin can drive 20 DHT11 sensors. Of course these are maths on paper and in real life these numbers maybe limited, but 10 rooms is more then enough for a usual house and that is only 50% load, or you can use 2 5V pins and then it is only 25% load.
and it is the simplest you can get and ruled out anything in between that can go wrong, or uses extra power

cheers
tom

@towme But, practically speaking, how are you supposed to wire 4 DHTs to a single Pi to measure multiple rooms? Put just the DHT in each room and run wires all over the house back to the Pi? Impractical. The point of the NodeMCU is to be able to cheaply put a DHT in each room and communicate the sensor readings wirelessly to Home Assistant.

Ok, I can see the point in that, but

  1. if you have not built your house/flat with the concept of home automation, then sooner or later you will reach the limit of your cablings anyway. That is my personal experience. Either you become a battery wholesale dealer or pull in more cables.

  2. The wifi system around the house has a max of clients it can serve. The more clients you have on wifi network the bigger the latency will be. And I do not think it is a good idea to have your automations working or not working depending on the wifi network load. Just imagine you have two kids, both watching utube, mom’s on fb watching funnies, and you are trying to get your automations working, but because all your neighbours are on the 2.4 Ghz network you only have 1 free channel to use. You will end up with automations working on wednesday and not working on weekends when everyone is home.

  3. if you have a LAN in your house you can use those cables. One cable, 6 sensors (VCC, ground common)

  4. You need to power the NodeMCU as well, that is wires also, or the battery wholesale thing again

cheers
tom

No question that I see your point. And if I were to build a new house, I would do it with home automation in mind. But for 99% of users this isn’t reality.

This is why ESP8266 (NodeMCU) is seen as so revolutionary a device, because of the wireless networking and IOT applications. I think the scenarios you cite where your network is so overwhelmed with traffic and your neighbors are using all the 2.4ghz channels is really an edge case - in that case most of your network activity would need to be wired anyway (streaming, surfing, etc) or else you would be unhappy not only with your IOT devices. Especially because MQTT traffic, for example, is very light weight. The vast majority of users could put very many esp8266 devices in their houses for various functions and this would have zero effect on the wireless network.

Power is of course a consideration, but plugging an esp8266 device into the mains for constant power is not the same type of issue as communicating signals back to some central server (like a Pi) - dwellings must have multiple electrical receptacles per room by electrical code, so powering an ESP8266 almost anywhere in your house is trivial. The same can’t be said of some type of communication wire such as Ethernet, or even repurposing an old phone line (RJ11).

In short, I appreciate your perspective but I think you aren’t giving the NodeMCU enough credit for this application.

2 Likes

For the record, the use of this sensor is a one off in my bathroom which has a separate heating system (we do not centrally heat houses here in Australia) that I will automate in such a way as to to turn it on in the morning or evening should the temperature in the room be too low. The sensore will be plugged into a usb port on the wall plug which then also has a Sonoff wifi switch attached which is what will turn on the heating.

It’s too far away from my Z Wave network to use one of their sensors and wiring it back to the Pi in the loungeroom would cost a bucket-load more than simply having a wifi signal back to the router (that the pi is wired to).

Perhaps when I finally build my dream home I can wire it to make this easier but at this stage, I would think using this setup is the most efficient means of automating the task I am trying to achieve.

@jonathanp did you ever get the DHT issue figured out? I was just looking back at the comments and your comment about the output you were getting when everything was wired properly:

sensor/bathroom/temperature nan
sensor/bathroom/battery 2.94
sensor/bathroom/humidity nan

“nan” could indicate that the variables setup to hold the temp and humidity readings aren’t of the right type (i.e. the values returned are floats but the variable is initialized as int) - just a thought.

Actually I am still waiting on a replacement sensor as I thought my original one was fried when I hooked the power up to the wrong PIN.

I’ll have a look into this tonight when I get home to see if the issue you mention is the real problem. Thanks for the comment.

FYI a return value of “nan” indicates that the value in the variable is “Not A Number” which could happen for many reasons.

A ha, that makes sense. I had assumed (incorrectly obviously) that "nan’ was something along the lines of not available.

I see the part in the sketch with reference to floats, but I’m unsure where it is intialized as int? And why would the battery give a proper reading but not the temp and humidity?

    uint16_t Lux = LightSensor.readLightLevel();
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float V = ESP.getVcc();

Where would I need to look next?

Could there be a difference in using a DHT11 vs DHT22? I know the 22 is more accurate but would they produce different output types per se? Th sketch was written for a 22 but I amended mine to say 11 everywhere it referenced it, which was only on the one line.

#define DHTTYPE DHT22

OK - so from the code we see that the variables “h” and “t” are initialized as a float (“floating-point” or decimal numbers), which is correct.

One thing I just noticed from just looking through your earlier comments: you say you are powering the DHT from VIN, which on the NodeMCU is 5V. However, the NodeMCU’s inputs use 3.3V logic and are not 5V tolerant. The DHT’s output voltage is equal to its input voltage, therefore: You need to power the DHT with 3.3V!

Here’s something to try now to rule out any problems with your code and as a check to see if the DHT or NodeMCU is actually borked:

  1. Load up the DHT example sketch (in the Arduino IDE, go to File - Examples - DHT sensor library - DHTtester).

  2. Change DHTPIN to the GPIO pin you are using for DATA (remember that on the NodeMCU the D0, D1, D2, etc numbers are NOT the GPIO pin numbers - e.g D1 is GPIO 5, D2 is GPIO 4 - you can find pinout diagrams online).

  3. Comment out (i.e. place “//” before) the #define DHTTYPE DHT22 line. Uncomment (i.e. remove the “//” before) the DHT11 line.

That’s it for configuring - upload to your NodeMCU and open the serial monitor. If working, the serial monitor should start showing humidity and temperature values. If that works, try your own code.

Let me know how it goes!

Thanks for the assistance. I know what I’ll be doing as soon as I get home tonight (don’t think the boss will be too keen to let me leave at 1PM sadly)

The reason I was using VIN for the power was I originally used a different sketch which said to power it from VIN. I’ll change that to the 3.3 pin first and then follow your steps.

Again, thanks so much for your help. I’m feeling more confident with MQTT and the whole Arduino IDE interface now. I got two Sonoff switches up and running in the meantime so I’m getting there eventually.

ok, everything is working friggin’ perfectly with the DHT tester! Wohoo!

I changed the power from VIN to a 3V pin so hopefully that was the issue. About to see if I can re-upload the other sketch and go from there!!

Ok, I’m getting rather excited now. Reloaded my sketch and we have some results.

sensor/bathroom/temperature 21.00
sensor/bathroom/battery 2.94
sensor/bathroom/humidity 50.00

HA seems to now know these details are coming in as I no longer have “unknown” on my UI but sadly I have blank field instead.

Have I got my config yaml correct? It’s loading correct off restarts and reboots

sensor 8:
  - platform: mqtt
    name: "Bathroom Temperature"
    state_topic: "sensor/bathroom/temperature"
    unit_of_measurement: " °C"
    value_template: "{{ value_json.temperature }}"
  - platform: mqtt
    name: "Bathroom Humidity"
    state_topic: "sensor/bathroom/humidity"
    unit_of_measurement: " %"
    value_template: "{{ value_json.humidity }}"

With the new sketch the format of the messages has changed. It’s not JSON anymore. Try this:

sensor 8:
  - platform: mqtt
    name: "Bathroom Temperature"
    state_topic: "sensor/bathroom/temperature"
    unit_of_measurement: " °C"
    value_template: "{{ value }}"
  - platform: mqtt
    name: "Bathroom Humidity"
    state_topic: "sensor/bathroom/humidity"
    unit_of_measurement: " %"
    value_template: "{{ value }}"

And I suggest to remove the space at the value of the unit_of_measurement. The unit is “°C” and not " °C". This will help if you want to group by unit some day.

you guys are my saviours!!!

I know have results in the HA UI and my family thinks I’m basically crazy as I’m jumping up and down because I have a temp and humidity reading from my bathroom.

Thank you soooo much for coming back again and again to teach this old dog some new tricks.

Guess the NodeMCU and DHT22 currently intransit to my place will be the beginning of my next sensor setup!!

1 Like

Ok, one last issue for me. The readings are not updating. Just looked at my UI from work and noticed the temp and humidity are not changing for hours.

Could it be the deep sleep? the sketch has it set at 1 million seconds (which is like 16 hours). If I unhook the wire on RST would that work to make it not go into deep sleep?

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

Mind you it looks like it is commented out with the // in front.

I’m only needing to update the reading every half hour or so, even though it is currently set as 5 minutes (300 seconds) which I can live with and was only planning to change once I finally got everything sorted out.

const int sleepTimeS = 300; //define deep sleep time

Any ideas?