Chapter 3: Building the Pair Device Script and Setting Up Smart Heaters
In this chapter, I'll walk you through how I set up my Bluetooth-enabled heaters, from identifying each device to pairing them with a custom bash script and ensuring they’re ready for use with my Python code. If you've been following along, you already know the groundwork we’ve laid. Now let’s see how it all fits together.
Identifying Each Heater with LightBlue
The first step in managing multiple heaters was to identify their unique Bluetooth addresses. To do this, I used an Android app called LightBlue, which makes scanning and identifying Bluetooth devices straightforward. Here’s what I did:
- Open LightBlue: I launched the app and started scanning for devices nearby.
- Check Signal Strength: By moving closer to each heater, I could see the signal strength (RSSI) increase, helping me identify which device matched each physical heater.
-
Document Everything: I took note of each heater’s address and organized them by room (e.g., kitchen, living room) in a JSON file called
rooms.json
.
Here’s an example of how my rooms.json
file looks, with each room containing an array of device addresses:
{
"kitchen": ["CC:22:37:11:26:4F"],
"living_room": ["CC:22:37:11:26:50"],
"bedroom_1": ["CC:22:37:11:26:51"],
"bedroom_2": ["CC:22:37:11:26:52"]
}
This step ensured I had a clear map of which devices belonged to which rooms, making it easier to target them with scripts.
Writing the Pair Device Bash Script
To make the process even more flexible, I used the Termux app on my Android phone. Termux is a command-line terminal that allowed me to move around freely while running the pairing script, making it easier to work near the heaters as I set them up.
To streamline the setup process, I wrote a bash script called pair_heaters.sh
. Here’s what it does:
Reads from
rooms.json
:
The script reads device addresses and room assignments from the JSON file, ensuring every heater is accounted for.-
Handles Pairing Automatically:
For each device, the script:- Checks if it’s already paired.
- Attempts to connect if it’s paired.
- If not, prompts me to put the device in pairing mode and waits for my confirmation before proceeding.
- Logs the pairing and connectivity status of each heater.
Disconnects Devices Post-Pairing:
After pairing, the script disconnects each heater. This is necessary because the Python script needs the devices to be connectable but not actively connected.Ensures Stability:
I included delays between Bluetooth commands to prevent issues caused by rapid interactions.Provides Logs for Debugging:
Detailed logs make it easy to see what’s happening at each step, helping me fix any problems quickly.
To run the script, I simply use:
bash pair_heaters.sh
The script loops through all the devices in rooms.json
, making the pairing process much faster and less error-prone.
Setting Up the Heaters for Use
After pairing and trusting the devices, they’re almost ready for use. Here’s what happens next:
Check the Connectable State:
The heaters must be in a connectable state—not connected to another device. This ensures the Python script can interact with them freely.Control with Python:
Once ready, the Python script detects the heaters, reads their current mode and temperature, and lets me control them remotely. Pairing ensures they’re always ready for the script to connect.Re-run the Script Only When Necessary:
The pairing script is only needed if a heater gets paired with another device (e.g., my phone). Otherwise, the heaters stay ready for action.
With this setup, my heaters are ready for automated control, bringing me closer to a fully smart system. If you want to see the script and configuration files, you can check out my GitHub repository.
In the next chapter, I’ll explain how to integrate the heaters into a home automation platform and make the Python script even more robust.
Ready to pair your devices?
Top comments (0)