I plan to create a smart hub using Pico and Home Assistant to automate my home and integrate with other HA devices using Zigbee, Bluetooth, IR, and other functionalities.
This time, I would like to introduce the basic aspects of setting up the environment.
I will be using Arduino as the development tool and Raspberry Pi Pico board as the hardware. Additionally, I will utilize the Pico Bricks development kit to integrate various types of devices. Pico Bricks allows for convenient usage without the need to individually purchase and connect components. It provides an easy way to add and integrate other devices as well.
Both Pico Bricks and Home Assistant RP2040 utilize the Pico W, which is a Wi-Fi-based board. However, if you wish to use wired Ethernet instead, you can change it to the W5100S-EVB-Pico board(WIZnet). This board provides support for wired Ethernet connectivity.
Since Linux is the preferred operating system, if you want to set up the server on a Windows PC, you will need to use a virtual machine. The official Home Assistant website provides the necessary files and manuals, allowing you to configure it according to your specific environment.
I am using VMware for virtualization.
For VMware, there is a detailed tutorial with images available on the following page. Please refer to it for guidance.
Once the installation is complete and you see the prompt, you have successfully installed it!
*The IP address displayed on the screen will be set as the MQTT Broker IP.
Now, let's proceed with adding a user account. The user ID and password set here will be used directly for MQTT communication.
If you don't see the User tab, please go to the Profile Settings, and select 'Enable Advanced Mode
Next, go to the ADD-ON STORE, search for MQTT, and add the Mosquitto broker.
Set up the environment for W5100S-EVB-Pico. Please follow the instructions on this website
Let's install the necessary libraries using the Arduino Library Manager. In this step, we will install the Home Assistant and MQTT libraries.
[Sketch> include library > manage libraries> Home-assistant-integration]
[Sketch> include library > manage libraries> MQTTPubSubClient]
Once you have completed these steps, you should have the basic functionality in place. As you continue with your development, make sure to install any additional libraries you may need along the way.
4. LED Switch Control
Now that the basic setup is complete, let's verify the basic functionality. You can refer to the Arduino examples on the following page for guidance.
Enter the necessary information for Home Assistant MQTT communication.
#define BROKER_ADDR IPAddress(192, 168, 11, 103)
#define BROKER_USERNAME "scarlet_pico"
#define BROKER_PASSWORD "1234"
Also, please provide the control pin information for W5100S.
Note that Pin 20 of Pico Bricks is used for the Buzzer function, so if you want to use the Buzzer feature, please modify the pin settings and hardware accordingly
#define ETH_SPI_CS_PIN 17
#define ETH_RESET_PIN 20
Please also configure the LED and Button pins
#define LED_PIN 7
#define BUTTON_PIN 10
If you want to change the button check time, please modify the 'define' statement below. The current button check time is set to 0.1 seconds.
#define DEBOUNCE_TIME 100
Let's register the device in Home Assistant. Once you have completed the coding and the system is functioning correctly, it will be automatically registered on the Home Assistant page. Here, the parameter for HASwitch (in this example, scalet_pico_led) is the unique ID.
HAMqtt HA_mqtt(client, device);
HASwitch led("scalet_pico_led");
Register a callback function for the command to be executed whenever an MQTT message is subscribed to.
led.onCommand(onSwitchCommand);
led.setName("Scarlet Pico LED");
Compile and upload the firmware from Arduino and check if the device is successfully registered on the Home Assistant page. On the device information page, you should see the configured name and unique ID of the device.
When you click on the icon, the switch screen will be displayed, and when you move the switch, the LED will activate.
That covers the basic functionality. Actually, I have completed the development up to the point where I can control the LED based on button inputs and integrate it with Home Assistant. However, there seems to be an issue with the additional configuration, as the device registration on Home Assistant is not working properly. Let me provide an overview based on the remaining information. I'll explain it again in the next project.
Configure the LED to blink based on button actions in the automation page
[Settings > Automations & Scenes > CREATE AUTOMATION]
In the Triggers configuration, select [Device], then choose the configured button switch turned on.
In the Actions configuration, select [Device], then choose the configured LED switch turned on.
Here is the code to use the push button on Pico as a toggle button.
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH && lastButtonState == LOW) {
ledState =! ledState;
setLedStateButton(ledState);
}
lastButtonState = buttonState;
The LED status is sent via MQTT Publish. LED control is done exclusively through Home Assistant switches. The button is used solely as an input device. This approach ensures that there are no conflicts in control status even when additional devices are added.