I just made several temp/humidity sensors using nodemcu’s and DHT11 sensors.
My recommendation is if you are going to use it for heating control then you may want to buy the (reportedly) more accurate DHT22. I think the accuracy of the DHT11 is around 2.5 degrees F. And I can see my sensors jumping around 2 degrees or so between readings.
I used a thread found here on the forum as the base example for my sensor and tweaked it a little to my liking.
Here is the sketch. But be aware that I’ve had troubles just copy and pasting from the thread into the Arduino-IDE.
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN D3 // what digital pin we're connected to on NodeMCU (D3)
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (+ on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Duo connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (- on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor unless your sensor already has it
// installed
DHT dht(DHTPIN, DHTTYPE);
//add your specifics to the below lines
#define wifi_ssid "XXXXXXX"
#define wifi_password "YYYYYYYY"
#define mqtt_server "111.111.1.111"
#define mqtt_user "ZZZZZZ"
#define mqtt_password "AAAAAAAA"
// change below to be unique for each sensor
#define clientName "DHTsensorXX"
#define humidity_topic "sensorXX/humidity"
#define temperature_celsius_topic "sensorXX/temperature_celsius"
#define temperature_fahrenheit_topic "sensorXX/temperature_fahrenheit"
#define heat_index_fahrenheit_topic "sensorXX/heat_index_fahrenheit"
//
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection…");
// Generate client name based on MAC address and last 8 bits of microsecond counter
// String clientName;
// clientName += "esp8266-";
// uint8_t mac[6];
// WiFi.macAddress(mac);
// clientName += macToStr(mac);
// clientName += "-";
// clientName += String(micros() & 0xff, 16);
Serial.print("Connecting to ");
Serial.print(mqtt_server);
Serial.print(" as ");
Serial.println(clientName);
// Attempt to connect
// If you do not want to use a username and password, change next line to
//if (client.connect((char*) clientName.c_str())) {
//if (client.connect("homeassistant","homeassistant","05081985"))
//if (client.connect("ESP8266Client", mqtt_user, mqtt_password))
if (client.connect(clientName, 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();
// Wait a few seconds between measurements.
delay(5000);
// 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("Failed to read from DHT sensor!");
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");
// Serial.print("Temperature in Celsius:");
// Serial.println(String(t).c_str());
// client.publish(temperature_celsius_topic, String(t).c_str(), true);
Serial.print("Temperature in Fahrenheit:");
Serial.println(String(f).c_str());
client.publish(temperature_fahrenheit_topic, String(f).c_str(), true);
// delay(1000);
Serial.print("Humidity:");
Serial.println(String(h).c_str());
client.publish(humidity_topic, String(h).c_str(), true);
// delay(1000);
Serial.print("Heat Index in Fahrenheit:");
Serial.println(String(hif).c_str());
client.publish(heat_index_fahrenheit_topic, String(hif).c_str(), true);
// delay(1000);
}
I have 39 radiators in the house, and have the ability to enable any single one of these independently. Radiators are grouped into zones which represent a room or combined space
Each radiator has a Thermostatic Release Valve which has the actuator and a temperature sensor (tuned to account for its physical closeness to the heat source.
Additionally I have a few room stats to get a measure in larger rooms or rooms where a window curtain may be interfering with the heat source flow and measurements
Originally I used a Honeywell Evohome solution but right now I am almost completely moved to a Zwave solution using the Eurotronic Spirit TRV, which are battery driven, and can be purchased for 38€ a head.
For scheduling there is an awsome Home assistant app daemon solution called Heaty which is very customisable.
Hopefully this info might help you get an inexpensive yet reliable solution.
(10 years ago I tried what your suggestions to roll your own - the current tech is less expensive and more reliable)
I am trying to keep things as simple as possible, and believe the below should work, but it doesn’t. the switch does not switch on or off dependant on temperature. Apologies for how it is displayed, haven’t worked out how to put it in properly.
Thanks
So i have now got the Switch and Climate part talking with Hassio, but cant get the sensors working. I have changed the above to DHT11 as it was set to DHT22. I get the icons in Hassio just not the readings. Any assistance always great fully received.
I may well change over to DHT22 or go for another system for sensors, advice always appreciated.
To properly format the code for display when you are done entering it in the reply window just highlight all the code and hit the </> symbol at the top of the window.
Hi
What do you mean by hardware/code.? I am using Sonoff Basic with Tasmota, and a rasperry pi3b+.
My configuration.yaml file has the following to control it. Both the switch and the climate parts are working, I just dont get the current temperature showing on the Home Assistant overview screen.
sorry. i don’t know how the tasmota software sends the dht11 signal over mqtt.
my nodemcu just sends an mqtt topic to HA that contains the measured values and i just create a sensor to read them out. based on code i posted above here are the sensors:
Apologies for asking the same question again, but i just cant get the temperatures into HA. I have tried all mentioned on this thread and others i have found on the forum. I have now purchased a DHT22 and after changing the names to that of my sonoff, and configuring the Tasmota to SI7021 ( I beleive this is the same as DHT22 which is not an option in the dropdown). IT still willnot work.
So just to confirm, do I need to addd any other info to other YAML files or elsewhere, or do i need to re flash the sonoff with an edited Tasmota. I have read about NodeMCU, is this an easier or a suitable replacement, if so where do i get the flash from.
Again apologies for going on, its just i have been trying to suss this for nearly a month now and am going over and over the same things.
I really did think this was going to be more straight forward, and I am considering buying Xiaomi or similar sensors but thought i may run into other issues.
Thanks in advance for any help.
I have no experience with sonoffs using DHT11/DHT22 sensors (i just use them as wifi light switches but not using tasmota) but if you want to use the nodemcu all you have to do is put the code in my first post into the ArduinoIDE, modify the code for your specific requirements (SSID, password, topic, sensor type, etc) then flash that code onto the nodemcu (using proper settings for the board). connect up the DHT sensor according to the comments in the code.
then create sensors by entering my code from the second entry into the HA configuration file and modifying that code to match the topics in the nodemcu.
This will give you a temperature and humidity sensor and will display it on the frontend and is usable in automations.
Hi again
Good news, i have now got the temperatures showing, not sure exactly what i did, but it did put a smile on my face. It may have just been a matter of giving it a few minutes.
However I now need the generic _thermostat to work. It displays on the Hassio overview ok, and i change the temperature setting on it, but this doesnt get back to the switch and switch it on. I guess this is either the generic thermostat not receiving the current temp, or it not sending the information correctly, or both.
I have looked at nodemcu, but this just gave me another headache and figured that what i have should be working.
The relevant part of my configuration.yaml is below. thanks
Just a couple of observations i have made. When manually turning the switch on, the generic thermostat changes from idle to heat, but does not switch off when it hits the target temp.
Not sure if this has any relevance , but when using the Hassio Configurator, the text for switch and sensor is green, for the thermostat it stays black. I have looked around for an answer as i am unsure of the importance of the colours.
Thanks
Hey guys, basically I’m doing something similiar but DIY and I have a floor heating system.
You should keep in mind that those electrothermic actuators are like binary switches. They are either open or closed (with some minutes transition time).
This is OK for a slow control system due to the floor itself being heated, but I’d not consider this solution for radiators (just my opinion)
I could think of very short activation cycles due the heat radiation if the sensor is put near to the radiator.
Regarding NO or NC you should think about what is the common state of the actuator. For my floor heating I use NC, so if not powered the valve is closed means no heating. This is probably the best solution if not living in arctic regions where the valve needs to be open >50% of the time.
Its working. I am very happy, turned out that the name of my switch was different. now i can start some testing before i decide whether to go with this idea.
Turning on and off radiators will always produce temperature oscillation in a room, both because of the on/off action itself, overshoot and also the time that the sensor takes to register the heat increase. The larger the thermal mass of the radiator the more pronounced the swings will likely be.
A much better approach is to reduce the flow in the radiator to a point at which it’s temperature becomes stable providing just enough heat to maintain the room set point. e.g 33% on. This assumes the flow temperature remains fairly constant of course.