Summary
With the number of IOT devices ever increasing, having an easy to use dashboard to both control and visualize the states of IOT devices is an extremely valuable piece to have in any embedded system.
Application
IOT devices are powerful but not intuitive to use for the average consumer. Creating a dashboard allows the average joe to take full advantage of these devices. For example using a dashboard an average joe can control an IOT security system installed in their house and see whether the doors are locked or potentially lock all the doors in the house at curfew so that teenagers who come home late have to sleep in the garage (true story).
Materials
To complete this lab, you will need the following materials:
· A Raspberry Pi with a bootable SD card
· A Raspberry Pi power supply
· A computer with a SD card reader and a MQTT broker installed
· The three Arduino devices made in Lab 4
References
The following links may be helpful to compelte this tutorial:
· http://aaronnelson95.com/IT441Lab4.php – Lab 4 Instructions on how to set up the Mosquitto MQTT broker on a computer and how to develop 3 Arduino devices to communicate with MQTT
· https://www.home-assistant.io/docs/installation/hassbian/installation/ – Steps on how to install Hassbian on a Raspberry Pi (as well as a link to the latest Hassbian image)
· https://www.juanmtech.com/install-home-assistant-simple-steps/ – Another helpful guide on installing Hassbian. This one includes more information about personally configuring the image and gaining access to it
· https://www.home-assistant.io/integrations/switch.mqtt/ – Adding MQTT capabilities to Home Assistant
· https://www.home-assistant.io/docs/configuration/devices/ – Saving MQTT topics as sensors in Home Assistant
· https://www.home-assistant.io/integrations/template/ – Working with templates to alter the way MQTT values are written
Procedures (TAKEN FROM AaronNeilson95.com)
1. Setup the Door Opener, Echo Sensor, and Stoplight devices from Lab 4. In addition, you should have a Mosquitto MQTT server running. Verify that these devices work together and that they publish data to the following MQTT topics. This information will be used and read by Home Assistant:
Topic | Message | Description | Published By | Subscribed By |
device/doorSwitch | Activated | Device came online | Door Opener | |
device/echoSensor | Activated | Device came Online | Echo Sensor | |
device/stoplight | Activated | Device came online | Stoplight | |
garage/doorSwitch | 0 | Magnets are disconnected | Door Opener | Echo Sensor |
1 | Magnets are connected | |||
garage/echoSwitch/color | g | Distance read is > 35cm | Echo Sensor | Stoplight |
y | Distance read is 20-35cm | |||
r | Distance read is 10-20cm | |||
b | Distance read is 0-10cm | |||
o | Received “0” from garage/doorSwitch |
2. Install the Hassbian operating system by following the instructions at https://www.home-assistant.io/docs/installation/hassbian/installation/.
a. Download the latest Hassbian image and flash the image onto an empty SD card.
b. Insert the card into a Raspberry Pi and either plug it into a monitor with a keyboard and ethernet cable or configure your Pi’s wireless network by altering a file in the boot partition (as explained in the guide).
c. Wait several minutes for the latest version of Home Assistant to be installed. When prompted, you can login to Hassbian with the default username of “pi” and password of “raspberry” (or you can SSH right into the Pi with its IP address if you are doing this without extra peripherals). When the installation is finished, you should be able to see some files populated in “/home/homeassistant/.homeassistant/”.
d. Type in “$ sudo raspi-config” to change the default password. You can also add a wifi network here and adjust other settings. If you would like to set multiple wifi networks for your device (I would highly recommend running it off of a hotspot hosted by the same computer your MQTT server is on), you can set those in the “/etc/wpa_supplicant/wpa_supplicant.conf” file.
e. Using a device on the network, open a web browser and go to “http://<YOUR_PI_IP_ADDRESS>:8123”. If Home Assistant appears, set an initial account up to log in with. If it does not appear, follow the troubleshooting steps in the Home Assistant instruction guide. Once you are in, you should see the main Home Assistant page, where you will add cards to for monitoring MQTT data.
Seeing the Home Assistant page come up is an indication that the installation worked
3. Add the MQTT Integration to Home Assistant to be able to work with your Mosquitto Server. This can either be done through the UI, or by editing the configuration yaml file for Home Assistant. Change this file with the command “$ sudo nano /home/homeassistant/.homeassistant/configuration.yaml” (we will be editing this file frequently throughout the lab). Simply insert the code below to connect to Mosquitto hosted at your Mosquitto server’s IP:
# Sets up the MQTT broker, which in this case is my computer’s Mosquitto servermqtt: # The IP of the Mosquitto server broker: 192.168.137.1 # What the Home Assistant client should check in as client_id: homeassistant discovery: true # The MQTT Topic to watch (subscribe to) discovery_prefix: garage |
Afterwards, restart home assistant with the command “$ sudo service home-assistant@homeassistant restart” (this will also be done frequently, so memorize this command!). In the browser, under the Configuration tab and then Integrations, you should now see a listing for “MQTT: configuration.yaml”. You can test that this works by going to the Developer Tools tab, selecting MQTT, and then listen to the topic “#”, which should then populate when you use your Arduino devices.
4. Now that Home Assistant is connected to the MQTT server, the topics will be setup as different sensors. In Home Assistant, once a topic receives a different value, the sensor will reflect and also contain that value. This is done by editing the configuration.yaml file again and then restarting the Home Assistant service. You will add the following for each topic you wish to watch:
sensor: # Directly returns the value of garage/doorSwitch (a 0 or 1) – platform: mqtt name: “Door Switch MQTT” state_topic: “garage/doorSwitch” |
5. Because the MQTT sends simple values, it may be beneficial to create another sensor that uses a template to assign friendly names to these topics. This will create another sensor device, and will not alter the original MQTT feed. To test the syntax of any template, you can go to the Developer Tools tab of Home Assistant and then select Template. You can examine if your code works like you intend. In addition, under the Developer Tools, you can also select the States tab to view your sensors. As you adjust MQTT values, the state column for the sensor entity will change here. Create an easy to read sensor by following this example:
sensor: # A template “adjusts” the values from a MQTT sensor to make it more readable and user friendly # This makes the Door Switch MQTT either return “Opened” or “Closed” – platform: template sensors: door_switch: friendly_name: “Door Switch” value_template: >- {% if is_state(‘sensor.door_switch_mqtt’, ‘1’) %} Opened {% else %} Closed {% endif %} |
Viewing MQTT Sensor Values in Developer Tools -> States
6. Now that we have sensors reading data from the MQTT broker, we can add cards to the Overview tab of Home Assistant to view this data easily at a glance. In this tab, click the three dots in the top right corner and select the option to configure the UI. In the bottom right, click the + button to add a card. To display the sensor data plainly, select the Sensor card. The Entity will be the name of your easy to read sensor and you can adjust other properties here. Feel free to test out other cards as well, such as the history graph. When you make changes and add cards here, they are recorded to a JSON stored in /home/homeassistant/.homeassistant/.storage/lovelace. Alternatively, you can manually insert card data in the code here.
Home Assistant Overview showing Sensor Values, histories, and other cards
7. Experiment with other sensors devices and cards. For example, I used a “switch” to listen to a MQTT topic. It also provides a button I can push to post a value into that topic as well, acting as a “manual override” mode for my garage door switch. I also created a sensor with a template to show whether or not my car is parked in the garage based on sensor readings.
Images of Final Product
Our Mosquitto Server is properly setup from Lab 4. It listens to values passed by the Arduino devices
When our devices are running and the distance from the sensor is relatively close, home assistant shows the current color of the stoplight, if the garage door is opened,
and whether or not the car is likely still at home. Also, the garage door override button lights up, showing it is opened and that we can force it to close.
I can also view a detailed history of my sensors and states in Home Assistant