Adding devices that are commercially available is even easier. It doesn't matter where these lines go in the file, but we'll place it just below the discovery. Copy and paste the following code into your Arduino IDE and upload to your ESP32. Weekly product releases, special offers, and more. If you havent installed Mosquitto MQTT broker yet, you can follow the previous post on how to do it. When the message "on" is received, it will turn on the LED connected to IO pin 13, and when "off" is received, turn the LED off. So we can add our own devices, and that's cool I guess, but that was basically working in the MQTT tutorial. Luckily, Home Assistant has an amazing feature that helps you to automate this process: MQTT discovery. For more context on how this discovery function fits in the whole program, I'm including my full script below. which will be used for running and deploying apps. To add the Configurator, click on the Configurator add-on, and click install. We'll need the name of the app and our login credentials when we add the plugs to Home Assistant.
Head over to Supervisor > Add-on store and search for Mosquitto MQTT. only during a certain time, or only if a switch is currently on or off), and actions which Home Assistant will do when the automation is triggered. Create a new.toit file in the project folder and name it sht31-mqtt.toit. The first device we'll add is the switch component, just like when we added the MQTT component, it doesn't matter where we add the switch to the configuration file, but to keep things organized, we'll add the following lines below the mqtt initialization. is one of the largest open-source platforms for home automation and is very popular for managing things through IoT. The Host should be your Home Assistant servers IP address on the network and the port should be 1883 which we defined while setting up the MQTT broker. To create an automation, select configuration from the left menu bar, and click automation (or go to http://hassio.local:8123/config/automation) and click the "Add Automation" button from the bottom right. When done press the save button in the lower right, and go back to configuration -> general and check the config and restart the server. Also, you need to change the last line to your phones name which we noted in the previous step. 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. When you push the button this time, you should notice a delay before the plug turns on or off. When the server comes back online, you can navigate back over to the overview or unused entities page and you should see your devices added under switches. The parameter repeat: 1 defines the time interval in minutes after which the notification will appear again. 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: Then modify the following section to reflect your Home Assistant setup. MQTT Discovery is essentially a way to tell your MQTT broker, Mosquitto running inside of Home Assistant in this case, which topics it needs to listen to. Now, to test the mobile notification service, go to Developers Tools > Services and start typing notification. I break my disovery messages into functions, so I can group them together while not pollution the heart of the Arduino program. Go back to the configurator tab and open the configuration.yaml file again. Once the network has been setup, insert the SD card and USB stick, if used, into the Pi. The next example will show that you can not only add your own devices, but devices you can buy and get working out of the box. Similarly to the switch component, we told Home Assistant that we would like to add a light component. Later on we'll be using this add-on to modify the configuration.yaml and automations.yaml files. It makes using new devices as simple as powering it on and leaving it alone. In my case, my discovery topics are: If you were to register each of these sensors manually for each MQTT device, you would do more configuration than actually enjoying your smart devices. In this example we're going to be adding WiFi plug switches that can easily be bought online. Now that we have Home Assistant set up, we need to configure it. I won't discuss these specific things, as they will differ for you: I'll discuss each of these topics in separate posts, as they're not relevant for this topic. Make sure the status is Healthy which indicates that your device is connected and ready to use.
Toit on the other hand provides an excellent platform for building and deploying IoT applications with very little effort. Click on Provision, enter the Wi-Fi credentials and wait for the provisioning process to complete. These examples were to demonstrate easy proof-of-concept demonstrations. Or click on following link to load your unused entities page (http://hassio.local:8123/lovelace/hass-unused-entities). After it's finished downloading, click start to enable the MQTT broker. While this guide walks you through the setup of Home Assistant, this is not a tutorial about how to get the most out of Home Assistant (but a quick Google search for Home Assistant tutorials will provide you with endless links to do so). In this example we'll create an automation to be triggered from the first MQTT example to toggle the Wifi Plug of the second example to show how to use Home Assistant to take a device that you made and programmed to control a commercially available device, or in our case a WiFi plug switch. Open the configuration.yaml file and copy the following lines into it-. I have installed the Visual Studio Code integration for doing so, but you can use any other editor as well. Next, we define a few global parameters for MQTT. 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. Check out some of these related tutorials: Get a background in how WiFi works as well as the hardware available to help you connect your project wirelessly. Once that's done we can save changes by pressing save icon. Now, inside the main function at the bottom, we create an infinite loop which will publish the sensor readings once every 10 seconds. My main use of the MQTT devices are plant sensors. To view the available add-ons, click on the Hass.io tab on the left side menu, followed by clicking on the ADD-ON STORE tab along the top as shown below (similarly, you can go to http://hassio.local:8123/hassio/store). With everything working as expected, we can now deploy the application. From there you can copy all of the service data and paste that into the service data entry for your automation. After that we set up the MQTT client with the username and password, which we had set in the MQTT broker. If you're able to see your components, clicking on the slide switch of the light or the lightning bolt symbol of the switch will turn the ESP32's LED on. After the very long first boot, you will be able to reach your device from a browser at http://hassio.local:8123 (assuming your router supports mDNS). publish client/mqtt.Client temp/float hum/float: print "Published message `$temp` on '$TOPIC'", // Publish readings at interval of 10 seconds, publish client driver.read_temperature driver.read_humidity, Sending high-temperature alerts to mobile phone, The last section explains how you can configure Home Assistant to. In this case you will enter home/livingroom/temperature. The main reason is the time it takes to connect to the WiFi network and then to the MQTT server. These are the topics I'm going to write about: Let's go to it! The parameter repeat: 1 defines the time interval in minutes after which the notification will appear again. Finally, well also send a high-temperature notification alert through the Home Assistants mobile phone app. Instead of configuring each sensor in Home Assistant, you're configuring it in your Arduino program and sending that to Home Assistant. Once it is, when you push the button connected to IO pin 0 on the ESP32, the plug should toggle states. Check out the MQTT tutorial below: The first device we'll add will be a switch that waits for the button connected to IO pin 0 to be pressed. 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. So why use Home Assistant? Hookup guide for the ESP32 Thing Plus (Micro-B) using the ESP32 WROOM's WiFi/Bluetooth system-on-chip in Arduino. In November (2020), I started to work with MQTT to set up a few smart devices in Home Assistant. To do this we'll need to flash the ESP32 Thing Plus boards with some code using Arduino.
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: Thats it, please comment below if you have any questions, and subscribe if you like to see the post similar to this. They do require that you download the app to connect the plugs to your wireless network. Pretty cool! "value_json" parses the incoming JSON string as a JSON object, so we can use the dot notation to get nested values. If you're not able to see your light and switch, they may be in your unused entities. If the syntax is correct, it will display a message "Configuration Valid". Need to find that next step after mastering the Arduino? In the first example we sent messages over MQTT of "on" and "off" over the "room/light" topic. In this tutorial, you'll learn everything you need to know to get started controlling your home with Home Assistant, an open source home automation hub that puts local control and privacy first, running off of a Raspberry Pi. The Configurator allows you to browse and modify the files on your Pi. Fill in the information for your WiFi credentials (your WiFi network must be on the same network as the Raspberry Pi), your user name and password you created for Home Assistant, and the IP address of the Pi. To create an automation, you can either use the configurator tool to edit the automations.yaml file, or in this case, use the automations tool to generate the code.
Click install, and wait for Home Assistant to download and add Mosquitto. With a constant power source, the amount of current the ESP32 draws isn't much of a concern. It is recommended to install VS code and Toits language extension as told in the installation link for hassle-free development. Finally, well also send a high-temperature notification alert through the Home Assistants mobile phone app. ssh to your Rapsberry Pi. This configuration page is where we'll initialize our MQTT broker and devices. Sending the discovery message requires a few things: I won't go into specifics here, but you can find the working code for this in the full code sample below. value_template: "{{states.sensor.temperature.state | float > 30.00}}", name: "Temperature recorded: {{states.sensor.temperature.state_with_unit}}". If you're searching for your own smart devices and aren't sure if they'll work with Home Assistant, a Google search with the device + Home Assistant should at the very least give you some Home Assistant forum questions with answers to help you decide. Home Assistant doesn't know what to do with that, so we need to tell it how to get the value of the temperature sensor from this JSON object. protocol and well be using it to send temperature and humidity values coming from an SHT31-D sensor. Learn how your comment data is processed. Every time we make a change to any of the files, we'll want to first check that the configuration is valid, and then restart Home Assistant. 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. Once the code has uploaded, open the terminal window to make sure that the light successfully connected to WiFi, and has connected to the MQTT network. After installing, enable the watchdog and start the broker. Before we can start using MQTT, we need to configure Home Assistant's MQTT broker. If it prompts for opening a.yaml file, select the one which we just created. If you're having a hard time finding the entity ID for a device, you can look it up under the developer tools section of the left menu bar under services. Next, we will create a function for publishing the temperature and humidity readings on the MQTT topic. Then run the following command to edit your Home Assistant configuration.yaml, Save the file using ^O and ^X to exit. And with the Qwiic Connect System on the ESP32 Thing Plus, you can add a ton of devices to Home Assistant without even having to wait for your soldering iron to heat up. This is a completely optional part, so you can skip it if you don't need it.
For this tutorial, we have set the topic name as /sensor/sht31.
To fix this, go into the configurator and open your configuration file. To follow along with the examples in this tutorial, you will need the following pieces of hardware.
I will, however, include them in the code sample for the full picture. This confirms that our MQTT broker is all set. The light will listen for commands from the topic "room/light", which is the same topic we defined in the ESP32's Light Arduino sketch. The very first thing would be to install the SHT31-D sensors driver and MQTT package from.
Note down the name of your phone as it appears in Home Assistant. Toit recently released the support for MQTT protocol and well be using it to send temperature and humidity values coming from an SHT31-D sensor.
First click on the Mosquitto broker under the offical add-ons section of the add-on store. Now that we have the MQTT server running, it's time to add some devices. homeassistant/sensor/plant_sensor_1/temperature/config, homeassistant/sensor/plant_sensor_1/humidity/config, homeassistant/sensor/plant_sensor_1/moisture/config, Reading Sensor data in your Arduino program, Sending MQTT sensor data to Home Assistant. Home Assistant is able to know what the state is of the MQTT light by using the state_topic both in the configuration file as well as publishing to the topic from the light's Arduino sketch. An introduction to MQTT, one of the main communication protocols used with the Internet of Things (IoT). For the service we'll select "switch.toggle", and for our service data we'll enter "{ "entity_id": "switch.xxxxxxxxxxxxx" }" where the x's represent your plug's serial number. mqtt_username and mqtt_password is the one you have setup during the installation of Mosquitto in the previous post step no 6. How can you use MQTT Discovery on an NodeMCU? In this tutorial we'll cover how to set up Home Assistant, and regardless of whether you created the devices yourself or purchased off the shelf IoT devices, get them working together in harmony. The very first thing would be to install the SHT31-D sensors driver and MQTT package from pkg.toit.io. Programming and Deploying Applications with Toit. Your device should appear in the list of serial ports, so select it. Go to the Overview tab, on the top right corner click on Edit dashboard > Add new card and select the one you like, I will be selecting the Sensor card. After installing, enable the watchdog and start the broker. I will be sharing my Arduino program in case you're looking to use this for your own projects as well. Well be using it in this tutorial. I'm using a pipe to specify a default value (0 in this case) in case there is no moisture sensor or there is something wrong. In total, there are 3 sensors on the device right now. This is because I'm using the "Deep sleep" mode of this NodeMCU to preserve energy. Once the code has uploaded, and the server has restarted, go to your overview or unused entities, and make sure your automation is enabled.
With this, Toit is all set and we can begin with programming. The line states.sensor.temperature.state | float > 30.00 defines the temperature threshold, which is 30 degrees in this case. You can change it to your liking. should be visible in the Home Assistants UI. Once restarted, make sure that your ESP32 is running and you should start receiving notifications on your phone if it gets too hot. Regardless of what you ultimately use Home Assistant for, one add-on that will make configuring Home Assistant easy without having to SSH into your Pi is the Configurator under Official add-ons. Add it to your cart, read through the guide, and adjust the cart as necessary. Once Home Assistant has finished updating and you're able to connect to the Pi, you should be greeted with a login screen to create a user name and password to log into Home Assistant. We enabled MQTT from the add-on store, but now we need to configure it. Now to test whether the broker is working properly, add any sample topic in the publish and listening field (make sure the same topic is in both the fields). Next we need to make sure your Pi will have an Internet connection. At the same time, the Home Assistant server shouldve received the MQTT message and the Temperature and Humidity values will now appear in the sensor cards. For more information about MQTT, check out the MQTT tutorial. The output should show that the app was successfully deployed. As we are using I2C communication with the sensor, the connection requires only four wires. The temperature sensor LM35 have 3 legs, the first leg is VCC, you can connect this to the 3.3V (ESP8266 boards output is 3.3V). Guide for getting going with the Raspberry Pi 3 Model B and Raspberry Pi 3 Model B+ starter kit. Again, this is my personal use for it and it might differ from you own uses.
Copy and paste the code below into your Arduino IDE, make sure to select ESP32 Dev Module as your board, and the correct COM port is selected. To get our automation to trigger correctly, we'll want to change our topic to "room/plug" and change the message we're sending to only be "toggle", to match the trigger event of our automation.
With the sketch below, the ESP32 Thing Plus will draw around 5mA while in sleep mode, and draw up to 160mA when the MQTT packet is sent. Once it comes back online, click on configuration on the left menu bar, and click on integrations (http://hassio.local:8123/config/integrations/dashboard).
Practically, this means that your MQTT broker will know about any and all devices that have announced themselves, without you having to do anything. Add WiFi to your project with this hookup guide for our Dialog's Ultra Low Power DA16200 R3 shield! The temperature and humidity values will be sent in JSON format whose value is then extracted using a value_template. you can quickly set up your ESP32 device using the Web serial interface.
You can change it to your liking. You can change the temperature_topic to your liking but make sure you also reflect this in your HomeAssistant Setup. If your Mosquitto broker add-on is enabled and running, you should see a MQTT component in the discovery section; click Configure. I'm sending the entire state of this MQTT device, which includes the values of 3 sensors, as a JSON object to Home Assistant. Well be using it in this tutorial. After this script has sent all sensor data to Home Assistant, the NodeMCU turns itself off and back on after 60 seconds. After the add-on has been installed, you'll want to click start and enable the "Show in Sidebar" option, and that will add a new option to your left menu bar with the name "Configurator" after you reload the page. YAML files are a human-readable data serialization language, similar to header files in C++, which are commonly used for configuration files. If your router doesn't support mDNS, or you're unsure, you'll need to use the IP address of your Pi instead (e.g. Coming to the main function, we configure the GPIO pins 21 and 22 for I2C communication. I help you achieve great SEO, higher conversions, and help you grow your business, MQTT Discovery with a NodeMCU and Home Assistant, create a simple MQTT switch in Home Assistant. To see what devices are currently supported from Home Assistant, check out the components page. This feature is one of my favorite discoveries recently, so I hope you enjoy it as well! . A notification should appear on your phone. All other packages come bundled with the firmware. By using MQTT Discovery, all I have to do is connect the Arduino to a power source and the MQTT broker instantly knows about the device and its various sensors. All programs used in this tutorial can be found inthis Github repository. Also, you need to change the last line to your phones name which we noted in the previous step. The cards will appear empty for the first time, so dont worry about that. After downloading the app, the entities of your phone such as battery state, charging state etc. The final step is to create cards for displaying sensor data. The payload is the message being sent over the command topic to turn the light on and off. With this, the Home Assistant part is all set to take in temperature and humidity readings. To fix that, we'll need to get Home Assistant to subscribe to those same topics, which is done by adding the components to the configuration file. We'll also send alerts to a mobile phone through t. In this tutorial, well have a look at how we can create an MQTT sensor for Home Assistant with ESP32 and the Toit platform. If we had removed the state_topic from the light, we would be able to control the light, but we wouldn't be able see from Home Assistant if the light is on or off. Next step is to create an MQTT sensor, which will be done by editing the configuration.yaml file. Home Assistant will automatically scan your network to add devices. You'll also see that from Home Assistant, the light bulb symbol of the light also turned on. After its done, go to the Devices tab and you should see your Devices name. // Home Assistant's Mosquitto MQTT broker, // Connect the Toit MQTT client to the broker, print "Connected to MQTT Broker @ $HOST:$PORT", Function to publish the sensor value on MQTT topic. With this, Toit is all set and we can begin with programming. To test whether everything is working properly, run the program by clicking on the run button associated with your device in VS codes Toit extension. If not it will show you a debug message showing where the error occurred that needs to be fixed. Home Assistant has such a discovery topic built-in: MQTT discovery topic. I hope this was helpful to you! The second device we'll add is the light component just below the switch component with an empty line separating the switch component with the light component. First click on "Configurator" from the left menu bar. In this post, I'll go over a few things that I've done to use this feature to automate my MQTT devices. This 7" Raspberry Pi Touchscreen LCD provides you with the ability to create a standalone device that can be utilized as a cu, With Single-Board Computers (SBCs) on the rise, it is a good idea to have an easy way to interface with them.
This way, your MQTT device announces itself to the broker, without you having to manually configure the broker. In this code sample I will skip over a few things. Mosquitto is an open source MQTT broker server that receives and distribute messages over the MQTT server. From this page, you can check the enable discovery box and press submit. We are going to use the temperature sensor LM35 connected to the A0 of ESP8266 like in the following picture. If you don't immediately see your device, you can try searching for your device under the left side bar based on its function. Select homeassistant.restart from the list of service and click on Call Service to restart home assistant. Finally, save the configuration file and restart your Home Assistant server.
Head over to Supervisor > Add-on store and search for Mosquitto MQTT. only during a certain time, or only if a switch is currently on or off), and actions which Home Assistant will do when the automation is triggered. Create a new.toit file in the project folder and name it sht31-mqtt.toit. The first device we'll add is the switch component, just like when we added the MQTT component, it doesn't matter where we add the switch to the configuration file, but to keep things organized, we'll add the following lines below the mqtt initialization. is one of the largest open-source platforms for home automation and is very popular for managing things through IoT. The Host should be your Home Assistant servers IP address on the network and the port should be 1883 which we defined while setting up the MQTT broker. To create an automation, select configuration from the left menu bar, and click automation (or go to http://hassio.local:8123/config/automation) and click the "Add Automation" button from the bottom right. When done press the save button in the lower right, and go back to configuration -> general and check the config and restart the server. Also, you need to change the last line to your phones name which we noted in the previous step. 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. When you push the button this time, you should notice a delay before the plug turns on or off. When the server comes back online, you can navigate back over to the overview or unused entities page and you should see your devices added under switches. The parameter repeat: 1 defines the time interval in minutes after which the notification will appear again. 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: Then modify the following section to reflect your Home Assistant setup. MQTT Discovery is essentially a way to tell your MQTT broker, Mosquitto running inside of Home Assistant in this case, which topics it needs to listen to. Now, to test the mobile notification service, go to Developers Tools > Services and start typing notification. I break my disovery messages into functions, so I can group them together while not pollution the heart of the Arduino program. Go back to the configurator tab and open the configuration.yaml file again. Once the network has been setup, insert the SD card and USB stick, if used, into the Pi. The next example will show that you can not only add your own devices, but devices you can buy and get working out of the box. Similarly to the switch component, we told Home Assistant that we would like to add a light component. Later on we'll be using this add-on to modify the configuration.yaml and automations.yaml files. It makes using new devices as simple as powering it on and leaving it alone. In my case, my discovery topics are: If you were to register each of these sensors manually for each MQTT device, you would do more configuration than actually enjoying your smart devices. In this example we're going to be adding WiFi plug switches that can easily be bought online. Now that we have Home Assistant set up, we need to configure it. I won't discuss these specific things, as they will differ for you: I'll discuss each of these topics in separate posts, as they're not relevant for this topic. Make sure the status is Healthy which indicates that your device is connected and ready to use.
Toit on the other hand provides an excellent platform for building and deploying IoT applications with very little effort. Click on Provision, enter the Wi-Fi credentials and wait for the provisioning process to complete. These examples were to demonstrate easy proof-of-concept demonstrations. Or click on following link to load your unused entities page (http://hassio.local:8123/lovelace/hass-unused-entities). After it's finished downloading, click start to enable the MQTT broker. While this guide walks you through the setup of Home Assistant, this is not a tutorial about how to get the most out of Home Assistant (but a quick Google search for Home Assistant tutorials will provide you with endless links to do so). In this example we'll create an automation to be triggered from the first MQTT example to toggle the Wifi Plug of the second example to show how to use Home Assistant to take a device that you made and programmed to control a commercially available device, or in our case a WiFi plug switch. Open the configuration.yaml file and copy the following lines into it-. I have installed the Visual Studio Code integration for doing so, but you can use any other editor as well. Next, we define a few global parameters for MQTT. 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. Check out some of these related tutorials: Get a background in how WiFi works as well as the hardware available to help you connect your project wirelessly. Once that's done we can save changes by pressing save icon. Now, inside the main function at the bottom, we create an infinite loop which will publish the sensor readings once every 10 seconds. My main use of the MQTT devices are plant sensors. To view the available add-ons, click on the Hass.io tab on the left side menu, followed by clicking on the ADD-ON STORE tab along the top as shown below (similarly, you can go to http://hassio.local:8123/hassio/store). With everything working as expected, we can now deploy the application. From there you can copy all of the service data and paste that into the service data entry for your automation. After that we set up the MQTT client with the username and password, which we had set in the MQTT broker. If you're able to see your components, clicking on the slide switch of the light or the lightning bolt symbol of the switch will turn the ESP32's LED on. After the very long first boot, you will be able to reach your device from a browser at http://hassio.local:8123 (assuming your router supports mDNS). publish client/mqtt.Client temp/float hum/float: print "Published message `$temp` on '$TOPIC'", // Publish readings at interval of 10 seconds, publish client driver.read_temperature driver.read_humidity, Sending high-temperature alerts to mobile phone, The last section explains how you can configure Home Assistant to. In this case you will enter home/livingroom/temperature. The main reason is the time it takes to connect to the WiFi network and then to the MQTT server. These are the topics I'm going to write about: Let's go to it! The parameter repeat: 1 defines the time interval in minutes after which the notification will appear again. Finally, well also send a high-temperature notification alert through the Home Assistants mobile phone app. Instead of configuring each sensor in Home Assistant, you're configuring it in your Arduino program and sending that to Home Assistant. Once it is, when you push the button connected to IO pin 0 on the ESP32, the plug should toggle states. Check out the MQTT tutorial below: The first device we'll add will be a switch that waits for the button connected to IO pin 0 to be pressed. 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. So why use Home Assistant? Hookup guide for the ESP32 Thing Plus (Micro-B) using the ESP32 WROOM's WiFi/Bluetooth system-on-chip in Arduino. In November (2020), I started to work with MQTT to set up a few smart devices in Home Assistant. To do this we'll need to flash the ESP32 Thing Plus boards with some code using Arduino.
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: Thats it, please comment below if you have any questions, and subscribe if you like to see the post similar to this. They do require that you download the app to connect the plugs to your wireless network. Pretty cool! "value_json" parses the incoming JSON string as a JSON object, so we can use the dot notation to get nested values. If you're not able to see your light and switch, they may be in your unused entities. If the syntax is correct, it will display a message "Configuration Valid". Need to find that next step after mastering the Arduino? In the first example we sent messages over MQTT of "on" and "off" over the "room/light" topic. In this tutorial, you'll learn everything you need to know to get started controlling your home with Home Assistant, an open source home automation hub that puts local control and privacy first, running off of a Raspberry Pi. The Configurator allows you to browse and modify the files on your Pi. Fill in the information for your WiFi credentials (your WiFi network must be on the same network as the Raspberry Pi), your user name and password you created for Home Assistant, and the IP address of the Pi. To create an automation, you can either use the configurator tool to edit the automations.yaml file, or in this case, use the automations tool to generate the code.
Click install, and wait for Home Assistant to download and add Mosquitto. With a constant power source, the amount of current the ESP32 draws isn't much of a concern. It is recommended to install VS code and Toits language extension as told in the installation link for hassle-free development. Finally, well also send a high-temperature notification alert through the Home Assistants mobile phone app. ssh to your Rapsberry Pi. This configuration page is where we'll initialize our MQTT broker and devices. Sending the discovery message requires a few things: I won't go into specifics here, but you can find the working code for this in the full code sample below. value_template: "{{states.sensor.temperature.state | float > 30.00}}", name: "Temperature recorded: {{states.sensor.temperature.state_with_unit}}". If you're searching for your own smart devices and aren't sure if they'll work with Home Assistant, a Google search with the device + Home Assistant should at the very least give you some Home Assistant forum questions with answers to help you decide. Home Assistant doesn't know what to do with that, so we need to tell it how to get the value of the temperature sensor from this JSON object. protocol and well be using it to send temperature and humidity values coming from an SHT31-D sensor. Learn how your comment data is processed. Every time we make a change to any of the files, we'll want to first check that the configuration is valid, and then restart Home Assistant. 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. Once the code has uploaded, open the terminal window to make sure that the light successfully connected to WiFi, and has connected to the MQTT network. After installing, enable the watchdog and start the broker. Before we can start using MQTT, we need to configure Home Assistant's MQTT broker. If it prompts for opening a.yaml file, select the one which we just created. If you're having a hard time finding the entity ID for a device, you can look it up under the developer tools section of the left menu bar under services. Next, we will create a function for publishing the temperature and humidity readings on the MQTT topic. Then run the following command to edit your Home Assistant configuration.yaml, Save the file using ^O and ^X to exit. And with the Qwiic Connect System on the ESP32 Thing Plus, you can add a ton of devices to Home Assistant without even having to wait for your soldering iron to heat up. This is a completely optional part, so you can skip it if you don't need it.
For this tutorial, we have set the topic name as /sensor/sht31.
To fix this, go into the configurator and open your configuration file. To follow along with the examples in this tutorial, you will need the following pieces of hardware.
I will, however, include them in the code sample for the full picture. This confirms that our MQTT broker is all set. The light will listen for commands from the topic "room/light", which is the same topic we defined in the ESP32's Light Arduino sketch. The very first thing would be to install the SHT31-D sensors driver and MQTT package from.
Note down the name of your phone as it appears in Home Assistant. Toit recently released the support for MQTT protocol and well be using it to send temperature and humidity values coming from an SHT31-D sensor.
First click on the Mosquitto broker under the offical add-ons section of the add-on store. Now that we have the MQTT server running, it's time to add some devices. homeassistant/sensor/plant_sensor_1/temperature/config, homeassistant/sensor/plant_sensor_1/humidity/config, homeassistant/sensor/plant_sensor_1/moisture/config, Reading Sensor data in your Arduino program, Sending MQTT sensor data to Home Assistant. Home Assistant is able to know what the state is of the MQTT light by using the state_topic both in the configuration file as well as publishing to the topic from the light's Arduino sketch. An introduction to MQTT, one of the main communication protocols used with the Internet of Things (IoT). For the service we'll select "switch.toggle", and for our service data we'll enter "{ "entity_id": "switch.xxxxxxxxxxxxx" }" where the x's represent your plug's serial number. mqtt_username and mqtt_password is the one you have setup during the installation of Mosquitto in the previous post step no 6. How can you use MQTT Discovery on an NodeMCU? In this tutorial we'll cover how to set up Home Assistant, and regardless of whether you created the devices yourself or purchased off the shelf IoT devices, get them working together in harmony. The very first thing would be to install the SHT31-D sensors driver and MQTT package from pkg.toit.io. Programming and Deploying Applications with Toit. Your device should appear in the list of serial ports, so select it. Go to the Overview tab, on the top right corner click on Edit dashboard > Add new card and select the one you like, I will be selecting the Sensor card. After installing, enable the watchdog and start the broker. I will be sharing my Arduino program in case you're looking to use this for your own projects as well. Well be using it in this tutorial. I'm using a pipe to specify a default value (0 in this case) in case there is no moisture sensor or there is something wrong. In total, there are 3 sensors on the device right now. This is because I'm using the "Deep sleep" mode of this NodeMCU to preserve energy. Once the code has uploaded, and the server has restarted, go to your overview or unused entities, and make sure your automation is enabled.
With this, Toit is all set and we can begin with programming. The line states.sensor.temperature.state | float > 30.00 defines the temperature threshold, which is 30 degrees in this case. You can change it to your liking. should be visible in the Home Assistants UI. Once restarted, make sure that your ESP32 is running and you should start receiving notifications on your phone if it gets too hot. Regardless of what you ultimately use Home Assistant for, one add-on that will make configuring Home Assistant easy without having to SSH into your Pi is the Configurator under Official add-ons. Add it to your cart, read through the guide, and adjust the cart as necessary. Once Home Assistant has finished updating and you're able to connect to the Pi, you should be greeted with a login screen to create a user name and password to log into Home Assistant. We enabled MQTT from the add-on store, but now we need to configure it. Now to test whether the broker is working properly, add any sample topic in the publish and listening field (make sure the same topic is in both the fields). Next we need to make sure your Pi will have an Internet connection. At the same time, the Home Assistant server shouldve received the MQTT message and the Temperature and Humidity values will now appear in the sensor cards. For more information about MQTT, check out the MQTT tutorial. The output should show that the app was successfully deployed. As we are using I2C communication with the sensor, the connection requires only four wires. The temperature sensor LM35 have 3 legs, the first leg is VCC, you can connect this to the 3.3V (ESP8266 boards output is 3.3V). Guide for getting going with the Raspberry Pi 3 Model B and Raspberry Pi 3 Model B+ starter kit. Again, this is my personal use for it and it might differ from you own uses.
Copy and paste the code below into your Arduino IDE, make sure to select ESP32 Dev Module as your board, and the correct COM port is selected. To get our automation to trigger correctly, we'll want to change our topic to "room/plug" and change the message we're sending to only be "toggle", to match the trigger event of our automation.
With the sketch below, the ESP32 Thing Plus will draw around 5mA while in sleep mode, and draw up to 160mA when the MQTT packet is sent. Once it comes back online, click on configuration on the left menu bar, and click on integrations (http://hassio.local:8123/config/integrations/dashboard).
Practically, this means that your MQTT broker will know about any and all devices that have announced themselves, without you having to do anything. Add WiFi to your project with this hookup guide for our Dialog's Ultra Low Power DA16200 R3 shield! The temperature and humidity values will be sent in JSON format whose value is then extracted using a value_template. you can quickly set up your ESP32 device using the Web serial interface.
You can change it to your liking. You can change the temperature_topic to your liking but make sure you also reflect this in your HomeAssistant Setup. If your Mosquitto broker add-on is enabled and running, you should see a MQTT component in the discovery section; click Configure. I'm sending the entire state of this MQTT device, which includes the values of 3 sensors, as a JSON object to Home Assistant. Well be using it in this tutorial. After this script has sent all sensor data to Home Assistant, the NodeMCU turns itself off and back on after 60 seconds. After the add-on has been installed, you'll want to click start and enable the "Show in Sidebar" option, and that will add a new option to your left menu bar with the name "Configurator" after you reload the page. YAML files are a human-readable data serialization language, similar to header files in C++, which are commonly used for configuration files. If your router doesn't support mDNS, or you're unsure, you'll need to use the IP address of your Pi instead (e.g. Coming to the main function, we configure the GPIO pins 21 and 22 for I2C communication. I help you achieve great SEO, higher conversions, and help you grow your business, MQTT Discovery with a NodeMCU and Home Assistant, create a simple MQTT switch in Home Assistant. To see what devices are currently supported from Home Assistant, check out the components page. This feature is one of my favorite discoveries recently, so I hope you enjoy it as well! . A notification should appear on your phone. All other packages come bundled with the firmware. By using MQTT Discovery, all I have to do is connect the Arduino to a power source and the MQTT broker instantly knows about the device and its various sensors. All programs used in this tutorial can be found inthis Github repository. Also, you need to change the last line to your phones name which we noted in the previous step. The cards will appear empty for the first time, so dont worry about that. After downloading the app, the entities of your phone such as battery state, charging state etc. The final step is to create cards for displaying sensor data. The payload is the message being sent over the command topic to turn the light on and off. With this, the Home Assistant part is all set to take in temperature and humidity readings. To fix that, we'll need to get Home Assistant to subscribe to those same topics, which is done by adding the components to the configuration file. We'll also send alerts to a mobile phone through t. In this tutorial, well have a look at how we can create an MQTT sensor for Home Assistant with ESP32 and the Toit platform. If we had removed the state_topic from the light, we would be able to control the light, but we wouldn't be able see from Home Assistant if the light is on or off. Next step is to create an MQTT sensor, which will be done by editing the configuration.yaml file. Home Assistant will automatically scan your network to add devices. You'll also see that from Home Assistant, the light bulb symbol of the light also turned on. After its done, go to the Devices tab and you should see your Devices name. // Home Assistant's Mosquitto MQTT broker, // Connect the Toit MQTT client to the broker, print "Connected to MQTT Broker @ $HOST:$PORT", Function to publish the sensor value on MQTT topic. With this, Toit is all set and we can begin with programming. To test whether everything is working properly, run the program by clicking on the run button associated with your device in VS codes Toit extension. If not it will show you a debug message showing where the error occurred that needs to be fixed. Home Assistant has such a discovery topic built-in: MQTT discovery topic. I hope this was helpful to you! The second device we'll add is the light component just below the switch component with an empty line separating the switch component with the light component. First click on "Configurator" from the left menu bar. In this post, I'll go over a few things that I've done to use this feature to automate my MQTT devices. This 7" Raspberry Pi Touchscreen LCD provides you with the ability to create a standalone device that can be utilized as a cu, With Single-Board Computers (SBCs) on the rise, it is a good idea to have an easy way to interface with them.
This way, your MQTT device announces itself to the broker, without you having to manually configure the broker. In this code sample I will skip over a few things. Mosquitto is an open source MQTT broker server that receives and distribute messages over the MQTT server. From this page, you can check the enable discovery box and press submit. We are going to use the temperature sensor LM35 connected to the A0 of ESP8266 like in the following picture. If you don't immediately see your device, you can try searching for your device under the left side bar based on its function. Select homeassistant.restart from the list of service and click on Call Service to restart home assistant. Finally, save the configuration file and restart your Home Assistant server.