NODE MCU ESP12E V2 and 3x DS18B20 Sensors Send Data to HomeAssistant
by MartinS455 in Circuits > Arduino
345 Views, 2 Favorites, 0 Comments
NODE MCU ESP12E V2 and 3x DS18B20 Sensors Send Data to HomeAssistant
ESP with DS18b20 data logger.
Material
- NODE MCU ESP12-F (or something like that)
- DS18B20 sensors (how much do you need?, I have 3pcs.)
- resistor 4k7
- microUSB to USB cable, PC, USB 5V charger
- ArduinoIDE
...and a little patience and skill :)
Wiring
- the resistor (4k7) is connected between red and yellow cables
- yellow data cable is connected to D2 (pin)
- red is for 3.3V
- black is GND
Code
This is the code that is compiled according to the needs of my project. I drew from official sources but also from other similar projects. Please note, this may not be 100% suitable for everyone.
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 #define TEMPERATURE_PRECISION 9 #define wifi_ssid "your wifi" #define wifi_password "youf wifi pass" #define mqtt_server "IP MQTT server" #define mqtt_user "MQTT user" #define mqtt_password "MQTT pass" #define temperature_topic1 "sensor/temperature1" #define temperature_topic2 "sensor/temperature2" #define temperature_topic3 "sensor/temperature3" #define ONE_WIRE_BUS 2 #define TEMPERATURE_PRECISION 9 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //address to DS18B20 sensors DeviceAddress Sensor1 = { 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; DeviceAddress Sensor2 = { 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; DeviceAddress Sensor3 = { 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); delay(10); sensors.begin(); setup_wifi(); client.setServer(mqtt_server, 1883); Serial.println(); Serial.println(); Serial.print("Locating DS18B20 sensors..."); Serial.print("I found "); Serial.print(sensors.getDeviceCount(), DEC); Serial.println(" devices."); Serial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON"); else Serial.println("OFF"); if (!sensors.getAddress(Sensor1, 0)) Serial.println("Unable to find Sensor 1"); if (!sensors.getAddress(Sensor2, 1)) Serial.println("Unable to find Sensor 2"); if (!sensors.getAddress(Sensor3, 2)) Serial.println("Unable to find Sensor 3"); sensors.setResolution(Sensor1, TEMPERATURE_PRECISION); sensors.setResolution(Sensor2, TEMPERATURE_PRECISION); sensors.setResolution(Sensor3, TEMPERATURE_PRECISION); Serial.print("Sensor 1 Adress = "); printAddress(Sensor1); Serial.print(" read temperature: "); Serial.print(sensors.getTempC(Sensor1)); Serial.print(" ºC, Resolution: "); Serial.print(sensors.getResolution(Sensor1), DEC); Serial.println(); Serial.print("Sensor 2 Adress = "); printAddress(Sensor2); Serial.print(" read temperature: "); Serial.print(sensors.getTempC(Sensor2)); Serial.print(" ºC, Resolution: "); Serial.print(sensors.getResolution(Sensor2), DEC); Serial.println(); Serial.print("Sensor 3 Adress = "); printAddress(Sensor3); Serial.print(" read temperature: "); Serial.print(sensors.getTempC(Sensor3)); Serial.print(" ºC, Resolution: "); Serial.print(sensors.getResolution(Sensor3), DEC); Serial.println(); } void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } void printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); if(tempC == DEVICE_DISCONNECTED_C) { Serial.println("!!! Sensor is not connected !!!"); return; } Serial.print("Temp C: "); Serial.println(tempC); } void setup_wifi() { delay(10); 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("Establishing connection to MQTT server ..."); 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 60 seconds"); delay(60000); } } } void printData(DeviceAddress deviceAddress) { printTemperature(deviceAddress); } bool checkBound(float newValue, float prevValue, float maxDiff) { return !isnan(newValue) && (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); } long lastMsg = 0; float temp1 = 0.0; float temp2 = 0.0; float temp3 = 0.0; float diff = 0.1; void loop() { if (!client.connected()) { reconnect(); } client.loop(); delay(60000); sensors.requestTemperatures(); Serial.println("********************************************************"); Serial.println("Scaned temperature Sensor1:"); printData(Sensor1); Serial.println("Scaned temperature Sensor2:"); printData(Sensor2); Serial.println("Scaned temperature Sensor3:"); printData(Sensor3); Serial.println("********************************************************"); long now = millis(); if (now - lastMsg > 2000) { lastMsg = now; float newTemp1 = sensors.getTempC(Sensor1); float newTemp2 = sensors.getTempC(Sensor2); float newTemp3 = sensors.getTempC(Sensor3); if (checkBound(newTemp1, temp1, diff)) { temp1 = newTemp1; Serial.print("Send temperature Sensor1: "); Serial.println(String(temp1).c_str()); client.publish(temperature_topic1, String(temp1).c_str(), true); } if (checkBound(newTemp2, temp2, diff)) { temp2 = newTemp2; Serial.print("Send temperature Sensor2: "); Serial.println(String(temp2).c_str()); client.publish(temperature_topic2, String(temp2).c_str(), true); } if (checkBound(newTemp3, temp3, diff)) { temp3 = newTemp3; Serial.print("Send temperature Sensor3: "); Serial.println(String(temp3).c_str()); client.publish(temperature_topic3, String(temp3).c_str(), true); } } }
HA Setup
- install MQTT broker for HA, for example https://learn.adafruit.com/set-up-home-assistant-...
- configure MQTT broker, paste to config this for three sensors:
mqtt: broker: *ip address MQTT server" username: *user* password: *pass* sensor 1: platform: mqtt name: "Temperature1" state_topic: "sensor/temperature1" qos: 0 unit_of_measurement: "ºC" sensor 2: platform: mqtt name: "Temperature2" state_topic: "sensor/temperature2" qos: 0 unit_of_measurement: "ºC" sensor 3: platform: mqtt name: "Temperature3" state_topic: "sensor/temperature3" qos: 0 unit_of_measurement: "ºC"
Finish
Maybe you will:
- http://mqtt-explorer.com/
- https://datasheets.maximintegrated.com/en/ds/DS18B...
- in Arduino IDE is fine example to get HEX address DS18B20 sensor
good luck :)