Logo

dev-resources.site

for different kinds of informations.

Building a Smart Heater Controller with Python, Docker, and Bluetooth #3

Published at
12/29/2024
Categories
raspberrypi
bash
Author
mecorre1
Categories
2 categories in total
raspberrypi
open
bash
open
Author
8 person written this
mecorre1
open
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3

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:

  1. Open LightBlue: I launched the app and started scanning for devices nearby.
  2. 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.
  3. 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"]
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. Reads from rooms.json:
    The script reads device addresses and room assignments from the JSON file, ensuring every heater is accounted for.

  2. 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.
  3. 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.

  4. Ensures Stability:
    I included delays between Bluetooth commands to prevent issues caused by rapid interactions.

  5. 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
Enter fullscreen mode Exit fullscreen mode

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:

  1. 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.

  2. 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.

  3. 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?

raspberrypi Article's
30 articles in total
Favicon
Nextcloud on Raspberry Pi - Fedora + Podman Quadlets
Favicon
Vecchio RaspberryPi, Nuova HomeLab!
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #2
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #1
Favicon
How to Install Docker on Raspberry Pi
Favicon
How to SSH into a Raspberry Pi?
Favicon
node.js and docker on raspberry
Favicon
Open source mealie type docker container for recipes
Favicon
Deploy a Node.js Application Using MySQL and Prisma on a Raspberry Pi
Favicon
Getting started with Nix and Nix Flakes
Favicon
How to edit boot config Raspberry Pi 5 USB?
Favicon
Weather Dashboard with 5-Day Forecast and Wi-Fi Menu
Favicon
The Intelligent Disaster Management System with Alternative Energy and AI-Powered
Favicon
How I Leverage Raspberry Pi as a DevOps Engineer
Favicon
How to self-host MongoDB on a Raspberry Pi 4
Favicon
How to Turn a USB-Only Printer into a Wireless Printer Using CUPS and a Raspberry Pi
Favicon
Raspberry Pi project: Install project management software
Favicon
Running Raspberry Pi OS in a Docker Container
Favicon
The Smart House Experience: Crafting Innovation with a Raspberry Pi
Favicon
From Mobile Simplicity to Pi Complexity
Favicon
Setting Up Home Assistant on a Raspberry Pi 3 Model B
Favicon
Create an AP with Network Manager
Favicon
🏠 GPT Home 🤖💬 an Open-source Raspberry Pi Home Assistant!
Favicon
Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi
Favicon
How to Build Your Own Website with a Raspberry Pi: An Entertaining Guide
Favicon
How to self-host Postgres Database on Linux
Favicon
[K8s] A Complete Series of Articles on Kubernetes Environment Locally
Favicon
How to control a Noctua NF-A20 PWM fan from Raspberry Pi (OFF or 20%-100%)
Favicon
Rockchip SoC Roadmap for AI & Vision

Featured ones: