Mosquitto, ESPHome and Platformio

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

Create the config directory

homeassistant@d-diot:~/ESPHome $ mkdir config

Test:

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

If the previous command fails due to this bug, install voluptuous==0.11.5

homeassistant@d-diot:~/ESPHome $ source env/bin/activate
(env) homeassistant@d-diot:~/ESPHome $ pip install voluptuous==0.11.5
(env) homeassistant@d-diot:~/ESPHome $ deactivate

Autostart ESPHome at boot for d-diot image v.1.1:

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" &

Due to this bug for the d-diot image v.1.2 use this line instead of the previous one:

/etc/rc.local
# Launch ESPHome
su - homeassistant -c ". /home/homeassistant/ESPHome/env/bin/activate && export LC_ALL=C && esphome /home/homeassistant/ESPHome/config dashboard" &

See this for the integration of ESPHome dashboard in Home Assistant

PlatformIO is one of the dependencies of ESPHome, so it is already installed. The instruction to upload a firmware into a MySensors node using PlatformIO are here.

Below some configuration steps of the d-diot image v.1.2 and above to facilitate the use of PlatformIO. The configuration steps for MYSBootloader support in the d-diot image v.1.1 are here.

Configure the USB port auto detection in PlatformIO:

pi@d-diot:~ $ curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
pi@d-diot:~ $ sudo usermod -a -G plugdev homeassistant

Reboot your system:

pi@d-diot:~ $ sudo reboot

Switch to Home Assistant user:

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

Create a folder to store your PlatformIO projects:

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

Download and compile an example project to force the download of the atmelavr toolchain

homeassistant@d-diot:~/pio $ git clone https://github.com/d-diot/d-diot-personalize-node.git
homeassistant@d-diot:~/pio $ cd /home/homeassistant/pio/d-diot-personalize-node
homeassistant@d-diot:~/pio/d-diot-personalize-node $ source /home/homeassistant/ESPHome/env/bin/activate
(env) homeassistant@d-diot:~/pio/d-diot-personalize-node $ platformio run -e uno
(env) homeassistant@d-diot:~/pio/d-diot-personalize-node $ deactivate

Enter in the PlatformIO boards directory:

homeassistant@d-diot:/home/pi $ cd /home/homeassistant/.platformio/platforms/atmelavr/boards

Create a custom board file for the ATMega328P 16 MHz - 5V devices with MYSBootloader:

homeassistant@d-diot:~/.platformio/platforms/atmelavr/boards $ nano MySpro16MHzatmega328.json

and insert the following lines:

MySpro16MHzatmega328.json
{
  "build": {
    "core": "arduino",
    "extra_flags": "-DARDUINO_AVR_PRO",
    "f_cpu": "16000000L",
    "mcu": "atmega328p",
    "variant": "eightanaloginputs"
  },
  "frameworks": [
    "arduino"
  ],
  "fuses": {
    "efuse": "0xFE",
    "hfuse": "0xDA",
    "lfuse": "0xFF"
  },
  "name": "MYSBootloader ATmega328P (5V, 16 MHz)",
  "upload": {
    "maximum_ram_size": 2048,
    "maximum_size": 30720,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 57600
  },
  "url": "http://www.mysensors.org",
  "vendor": "MySensors"
}

Save and exit (Ctrl + x, Y).

Create a custom board file for the ATMega328P 8 MHz - 3.3V devices with MYSBootloader:

homeassistant@d-diot:~/.platformio/platforms/atmelavr/boards $ nano MySpro8MHzatmega328.json

and insert the following lines:

MySpro8MHzatmega328.json
{
  "build": {
    "core": "arduino",
    "extra_flags": "-DARDUINO_AVR_PRO",
    "f_cpu": "8000000L",
    "mcu": "atmega328p",
    "variant": "eightanaloginputs"
  },
  "frameworks": [
    "arduino"
  ],
  "fuses": {
    "efuse": "0xFE",
    "hfuse": "0xD2",
    "lfuse": "0xE2"
  },
  "name": "MYSBootloader ATmega328P (3.3V, 8 MHz)",
  "upload": {
    "maximum_ram_size": 2048,
    "maximum_size": 30720,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 57600
  },
  "url": "http://www.mysensors.org",
  "vendor": "MySensors"
}

Save and exit (Ctrl + x, Y).

Create a custom board file for the ATMega328P 1 MHz devices (battery powered, BOD = 1.8V) with MYSBootloader:

homeassistant@d-diot:~/.platformio/platforms/atmelavr/boards $ nano MySpro1MHzatmega328.json

and insert the following lines:

MySpro1MHzatmega328.json
{
  "build": {
    "core": "arduino",
    "extra_flags": "-DARDUINO_AVR_PRO",
    "f_cpu": "1000000L",
    "mcu": "atmega328p",
    "variant": "eightanaloginputs"
  },
  "frameworks": [
    "arduino"
  ],
  "fuses": {
    "efuse": "0xFE",
    "hfuse": "0xD2",
    "lfuse": "0x62"
  },
  "name": "MYSBootloader ATmega328P (1 MHz)",
  "upload": {
    "maximum_ram_size": 2048,
    "maximum_size": 30720,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 9600
  },
  "url": "http://www.mysensors.org",
  "vendor": "MySensors"
}

Save and exit (Ctrl + x, Y).

Clean the build of the example

homeassistant@d-diot:~/pio $ cd /home/homeassistant/pio/d-diot-personalize-node
homeassistant@d-diot:~/pio/d-diot-personalize-node $ source /home/homeassistant/ESPHome/env/bin/activate
(env) homeassistant@d-diot:~/pio/d-diot-personalize-node $ platformio run --target clean
(env) homeassistant@d-diot:~/pio/d-diot-personalize-node $ deactivate

Remove the d-diot-personalize-node folder via samba

  • system_administration/manual_installation/5_mosquitto_mqtt.txt
  • Last modified: 2019/10/22 18:34
  • by franzunix