Smart Garden – When DFRobot meets HomeAssistant(Part 3)
In this part, we are going to set up the client part. As we mentioned in the previous instruction, we use ESP32 FireBeetle as the microcontroller, which integrated with Wi-Fi & Bluetooth dual-mode module.
In short, the ESP32 collects the environment information, such as temperature, humidity, light, etc, and publishes it to the MQTT server (running on the LattePanda) and then the user can check all the information on the Home Assistant UI. Also, I set up some actuators, like an LED strip, relay, and a servo motor, which allows the user to control them or when some conditions met and triggered automatically. Hardware setup
It’s not convenient to set up the connections directly on the ESP32, especially when you use multiple sensors. So we are going to use an expansion board, which makes the development more quickly. In this project, we use two ESP32 boards. The following tables are Pin settings.
After connecting the hardware, now we can deal with coding things. Before we upload the code, we need to do some preparations. First of all, we need to set up the development environment. You can check the link below and follow the instruction to do this.
Then there are also some libraries required to be installed.
You can download the extract from the following zip file to the Arduino library directory.
The following attachments are the complete codes.
Now let’s take a close look at the code. The first place we need to modify is the local network information.
Local Network Settings
MQTT_SERVER： This is the IP address of the LattePanda. (Open the terminal on LP and enter the command ‘ifconfig’ to check the WLAN settings)
ssid: Your local network name.
password: Your local network password.
MQTT Topics Settings
In the previous instruction, we talked about how does the MQTT server and MQTT topic work. And in this part of the code, we set the MQTT topics names. As you see, the topic names have levels. We have two different first topic levels, /pond/ and /smartgarden/, which means different locations. We can create a hierarchy of directories, for example, we have two temperatures in both garden and pond, so we create sub-directories, “/smartgarden/temperature” and “/pond/temperature”. Please note that topic names are case-sensitive, and therefore “/smartgarden/temperature” is different from “/Smartgarden/Temperature”. So please make sure that your topic names set in Arduino code are exactly the same as the settings in HA configuration files.
The main loop is quite simple, build the connection, maintain the connection, and publish the information. Let’s take ambient light as an examples. The ambient_light_pin has been defined at the beginning of the code. Read the ambient light value and publish it with pre-defined topic name, lightTopic, which is “/smartgarden/light”.
It’s easy to achieve this kind of automation when the sensor and actuator are on the same device. You may notice that there is an if statement below. The LED string will be turned on and off according to the ambient light value. But what if the sensor and actuator are on different devices? Or if you want to control some actuators through Home Assistant UI? How can we achieve this function? The answer is the callback function.
What is the callback function? It’s a function that will be called when something happens. In this case, the callback function will be called when the MQTT client (ESP32) receives a publish message from the MQTT server.
This is how a callback function looks like. There are two essential parameters required, topic and payload. In the previous instruction, we set up a switch entity in the Home Assistant configuration file as shown below.
And we set the topic names in command topic and also the payload value on&off. Then we can accomplish the callback function.
When we receive a message that has a certain topic, let’s say “/smartgarden/led”. Then the callback function will be called. Check the payload if it’s ‘1’ or ‘0’ and perform the corresponding actions, turn the led on or off.
For sure, you can make your conditions and actions. For example, if you want to have a switch to control the watering system, you can add a switch with a command topic,
Let’s go back to the previous question, what if we have the sensor and actuator are on the different devices? For example, if the ambient light sensor is on device 1 and the LED light is on device 2. So how will the LED light perform actions corresponding to the ambient light sensor? Similarly, on the device 1, when the sensor value is less/greater than the threshold value, publish a message with the topic name, “/smartgarden/watering” and the payload value “1” or “0” rather than control the GPIO directly. On device 2, the same as a previous callback function, change the topic to “/smartgarden/watering”, and according to the payload value to do the corresponding actions.
This is all of the information you need to know. I hope this project can help you to make your smart projects.