01/08/2020

4 Steps to send MQTT data to Home Assistant using ESP8266

By snorlaxprime

If you have been following my post on installing Home Assistant on Raspberry Pi, now it is the time to try posting MQTT message to Mosquitto within Home Assistant.

If you haven’t installed Mosquitto MQTT broker yet, you can follow the previous post on how to do it.

What we are trying to do can be seen in the following picture:

We will have a temperature sensor connected to ESP8266 and using MQTT messaging we will post the temperature reading to Mosquitto MQTT broker in Home Assistant. So as the result we will be able to monitor the temperature.

Step 1. Connecting the Temperature sensor to ESP8266

We are going to use the temperature sensor LM35 connected to the A0 of ESP8266 like in the following picture.

The temperature sensor LM35 have 3 legs, the first leg is VCC, you can connect this to the 3.3V (ESP8266 board’s output is 3.3V). The middle leg is Vout (where the temperature is read from, you can connect this to the analog input of the ESP8266 pin AD0, this is located at the top right hand side of the board as shown in picture. And the right leg should be connected to the ground. Now your circuit is complete.

Step 2. Upload the code via Arduino interface

Once you have connected the ESP8266 via USB port you can download the source code from here , then modify the following section to reflect your WiFi configuration:

// WiFi
const char* ssid = "yourssid"; // Your personal network SSID
const char* wifi_password = "wifipassword"; // Your personal network password

Then modify the following section to reflect your Home Assistant setup

// MQTT
const char* mqtt_server = "192.168.1.20"; // IP of the MQTT broker
const char* temperature_topic = "home/livingroom/temperature";
const char* mqtt_username = "yourmqttusername"; // MQTT username
const char* mqtt_password = "yourmqttpassword"; // MQTT password
const char* clientID = "client_livingroom"; // MQTT client ID

mqtt_server is where your Mosquitto broker have been installed, this would be the ip address of your Raspberry Pi where you have installed Home Assistant.

You can change the temperature_topic to your liking but make sure you also reflect this in your HomeAssistant Setup.

mqtt_username and mqtt_password is the one you have setup during the installation of Mosquitto in the previous post step no 6.

Then upload the code to your ESP8266.

Step 3. Now Configure Mosquitto Broker in Home Assistant

Login to your Home Assistant and select “Configuration->Integration” from the left menu, then click on the yellow + button on the bottom right hand corner and search for Mosquitto:

Then click on “Configure” button in the MQTT tile:

Enter the topic to listen to “Listen to a topic” section in the configuration. In this case you will enter “home/livingroom/temperature”

Click on “Start Listening”. If all goes well, one minute later the ESP8266 should have published the temperature reading and you are able to see this like the following picture:

Now that we know our MQTT broker is working ok. At this point the temperature reading will not be reflected into the main home screen. If. you want it to be shown, then follow the next step.

Step 4. Update the Home Assistant configuration.yaml

ssh to your Rapsberry Pi. Then run the following command to edit your Home Assistant configuration.yaml

sudo nano homeassistant/configuration.yaml

Add the following configuration:

sensor:
  - platform: mqtt
    name: "Living Room Temperature"
    state_topic: "home/livingroom/temperature"

Save the file using ^O and ^X to exit. Before we restart the Home Assistant so that our configuration takes affect, it is a good idea to check that there is no error in the config, to do this login to Home Assistant and click on “Developer Tools->Services”

To check whether there is any error, you will have to look at the Home Assistant log file, this is located at “Configuration->General->Log” and make sure there is no error related to the configuration.

Now you are ready to restart the Home Assistant by going into the “Developer Tools->Services”

Select “homeassistant.restart” from the list of service and click on “Call Service” to restart home assistant.

If all goes well you should see the “Living Room Temperature” sensor reading in the overview page similar to the below, your screen might look slightly different from mine:

That’s it, please comment below if you have any questions, and subscribe if you like to see the post similar to this.