Mosquitto and ESPHome

The Mosquitto MQTT server has been installed in the basic system setup. More details on about the configuration of the server here.

Test the server: open a terminal to subscribe a topic.

pi@d-diot:~ $ mosquitto_sub -h localhost -t test

Open another terminal instance to publish the “hello world” message on the topic subscribed in the previous terminal instance.

pi@d-diot:~ $ mosquitto_pub -h localhost -t test -m "hello world"

Optional: authentication

Follow this steps to force authentication on the MQTT server. By default in the d-diot image the authentication is disabled.

Generate a password file and add the user “pi”. For this example set the password to “d-diot.com” when requested.

pi@d-diot:~ $ sudo mosquitto_passwd -c /etc/mosquitto/passwd pi

Add the user “homeassistant”. For this example set the password to “d-diot.com” when requested.

pi@d-diot:~ $ sudo mosquitto_passwd /etc/mosquitto/passwd homeassistant

Place a default.conf file in /etc/mosquitto/conf.d/ to enable authentication:

pi@d-diot:~ $ sudo nano /etc/mosquitto/conf.d/default.conf

Add the following lines:

/etc/mosquitto/conf.d/default.conf
allow_anonymous false
password_file /etc/mosquitto/passwd

Exit (CTRL+x) and save the changes (y). Restart the server to load new config.

pi@d-diot:~ $ sudo systemctl restart mosquitto

Test authentication

Run the following command:

pi@d-diot:~ $ mosquitto_pub -h localhost -t "test" -m "hello world"

It should return an error message.

Open a terminal instance and subscribe the “test” topic as user “pi”

pi@d-diot:~ $ mosquitto_sub -h localhost -t test -u "pi" -P "d-diot.com"

Open another teminal instances and publish to “test” topic as “homeassistant”.

pi@d-diot:~ $ mosquitto_pub -h localhost -t test -m "hello world from homeassistant" -u "homeassistant" -P "d-diot.com"

ESPHome has been integrated since d-diot image v.1.1 and here is the description of what it does, while the reference guide is here.

To avoid a dependencies hell with the python libraries and system packages, ESPHome is installed in python virtual environment.

Install python 2 virtual environment:

pi@d-diot:~ $ sudo pip install virtualenv

Switch to homeassistant user:

pi@d-diot:~ $ sudo -u homeassistant -H -s

ESPHome directory:

homeassistant@d-diot:/home/pi $ mkdir /home/homeassistant/ESPHome
homeassistant@d-diot:/home/pi $ cd /home/homeassistant/ESPHome

Create and activate the virtual environment:

homeassistant@d-diot:~/ESPHome $ virtualenv env
homeassistant@d-diot:~/ESPHome $ source env/bin/activate

Install ESPHome in the virtual environment:

(env) homeassistant@d-diot:~/ESPHome $ pip install esphome
(env) homeassistant@d-diot:~/ESPHome $ pip install tornado esptool
(env) homeassistant@d-diot:~/ESPHome $ deactivate

Test:

homeassistant@d-diot:~/ESPHome $ /home/homeassistant/ESPHome/env/bin/python /home/homeassistant/ESPHome/env/bin/esphome /home/homeassistant/ESPHome/config dashboard

Autostart ESPHome at boot:

homeassistant@d-diot:~/ESPHome $ exit
pi@d-diot:~ $ sudo nano /etc/rc.local
/etc/rc.local
# Launch ESPHome
su - homeassistant -c ". /home/homeassistant/ESPHome/env/bin/activate && esphome /home/homeassistant/ESPHome/config dashboard" &

See this for the integration of ESPHome dashboard in Home Assistant

First PagePrevious PageBack to overviewNext PageLast Page

  • system_administration/manual_installation/5_mosquitto_mqtt.txt
  • Last modified: 2019/06/19 19:52
  • by franzunix