DEV Community

qing
qing

Posted on

Build a Smart Home Controller with Python and MQTT

Build a Smart Home Controller with Python and MQTT

tags: python, iot, mqtt, tutorial


Imagine your living room lights turning on automatically when you walk in, or your thermostat adjusting before you even get home—not because of a vague smart algorithm, but because you built the brain behind it yourself. That power is exactly what you get when you combine Python and MQTT to create a custom smart home controller. Forget expensive ecosystems that lock you into their hardware; with a few lines of code and an open-source message broker, you can build a system that’s faster, cheaper, and infinitely more flexible.

Today, you’re going to build a working prototype that publishes sensor data and listens for commands to control devices. By the end of this post, you’ll have a real, functional controller running on your machine that you can expand with lights, locks, or even your coffee maker.

Why Python and MQTT?

Before we dive into the code, let’s talk about why this combo is the gold standard for DIY smart homes.

Python is the language of choice for IoT because it’s readable, has massive library support, and runs on everything from high-end servers to tiny Raspberry Pis. You don’t need to be a software engineer to write effective Python scripts; the syntax is intuitive, and the community is huge.

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for small devices and low-bandwidth networks. Unlike HTTP, which is heavy and request-response based, MQTT uses a publish-subscribe model. Devices publish messages to a central broker (like Mosquitto), and other devices subscribe to topics to receive those messages. This means your temperature sensor doesn’t need to know who’s listening; it just publishes, and your controller, your phone app, and your dashboard all receive the update instantly.

The result? A system that’s real-time, scalable, and energy-efficient.

Step 1: Set Up Your Environment

First, you need Python installed. If you’re on Windows, macOS, or Linux, download the latest version from python.org if you haven’t already.

Next, install the core libraries you’ll need. We’ll use Paho-MQTT for communication and Flask if you want to add a web interface later (we’ll stick to the core MQTT logic for now to keep it actionable).

Run this in your terminal:

pip install paho-mqtt
Enter fullscreen mode Exit fullscreen mode

Now, you need an MQTT broker. The easiest option for testing is Mosquitto. On Ubuntu, install it with:

sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
Enter fullscreen mode Exit fullscreen mode

On macOS, you can use Homebrew:

brew install mosquitto
Enter fullscreen mode Exit fullscreen mode

Once installed, start the broker. If you’re on Linux, it’s running as a service. On macOS, you might need to start it manually:

mosquitto
Enter fullscreen mode Exit fullscreen mode

You can test your broker immediately using the command-line tools:

mosquitto_sub -h localhost -t "test/topic"
Enter fullscreen mode Exit fullscreen mode

This subscribes to the test/topic channel. Now, in a new terminal, publish a message:

mosquitto_pub -h localhost -t "test/topic" -m "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

You should see Hello, World! appear in the subscriber terminal. If you do, your broker is working!

Step 2: Build Your Smart Home Controller in Python

Now, let’s write the Python script that will act as your controller. This script will:

  1. Subscribe to a command topic (e.g., smarthome/lights) to receive on/off commands.
  2. Publish sensor data (e.g., temperature) to a status topic (e.g., smarthome/temperature).
  3. React to commands by simulating device control (we’ll print the action to the console).

Here’s a complete, working example you can run today:

import paho.mqtt.client as mqtt
import time
import random

# Broker configuration
BROKER = "localhost"
PORT = 1883
CLIENT_ID = "smart-home-controller"

# Topics
COMMAND_TOPIC = "smarthome/lights"
STATUS_TOPIC = "smarthome/temperature"

# Callback when a message is received
def on_message(client, userdata, msg):
    print(f"Received command: {msg.topic} -> {msg.payload.decode()}")

    if msg.payload.decode() == "ON":
        print("🔦 Action: Turning lights ON")
        # In a real setup, you'd toggle a GPIO pin or send an HTTP request here
    elif msg.payload.decode() == "OFF":
        print("🔦 Action: Turning lights OFF")

# Callback when connected to the broker
def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")
    client.subscribe(COMMAND_TOPIC)
    print(f"Subscribed to {COMMAND_TOPIC}")

# Create the MQTT client
client = mqtt.Client(client_id=CLIENT_ID)
client.on_connect = on_connect
client.on_message = on_message

# Connect to the broker
client.connect(BROKER, PORT)

print("🚀 Smart Home Controller is running...")
print(f"Send commands to '{COMMAND_TOPIC}': 'ON' or 'OFF'")
print(f"Publishing temperature to '{STATUS_TOPIC}' every 5 seconds")

# Start an infinite loop to publish temperature data
while True:
    # Simulate a temperature reading between 20 and 25°C
    temp = random.uniform(20.0, 25.0)
    client.publish(STATUS_TOPIC, f"{temp:.2f}")
    print(f"🌡️ Published temperature: {temp:.2f}°C")
    time.sleep(5)

# Keep the client loop running
client.loop_start()
Enter fullscreen mode Exit fullscreen mode

Save this as controller.py and run it:

python controller.py
Enter fullscreen mode Exit fullscreen mode

You’ll see the script connecting to the broker, subscribing to the lights command topic, and publishing temperature readings every 5 seconds.

Step 3: Test Your Controller

Now, let’s send a command to turn your lights on. In a new terminal, use the Mosquitto client to publish:

mosquitto_pub -h localhost -t "smarthome/lights" -m "ON"
Enter fullscreen mode Exit fullscreen mode

Watch your controller.py output. You should see:

Received command: smarthome/lights -> ON
🔦 Action: Turning lights ON
Enter fullscreen mode Exit fullscreen mode

Now try turning them off:

mosquitto_pub -h localhost -t "smarthome/lights" -m "OFF"
Enter fullscreen mode Exit fullscreen mode

You’ll see the controller respond instantly. This is the core of your smart home: instant, reliable communication between devices.

Making It Real: From Simulation to Hardware

Right now, your controller simulates actions by printing to the console. To make it real, you can:

  • Connect an LED to a Raspberry Pi GPIO pin and toggle it using gpiozero or RPi.GPIO when you receive an "ON" command.
  • Send HTTP requests to smart plugs (like TP-Link Kasa) using the requests library.
  • Integrate with Home Assistant by publishing to standard topics like stat/lights and cmnd/lights (a common convention where stat = state and cmnd = command)[5].

The beauty of MQTT is that your Python script doesn’t need to know what device is listening. You can add a smart bulb, a motor, or a web dashboard—all subscribing to the same topic.

Expand Your System

Once you have this working, here are three immediate upgrades:

  1. Add a Web Interface: Use Flask to create a simple dashboard with buttons that publish "ON" and "OFF" to your MQTT broker.
  2. Monitor Multiple Sensors: Subscribe to multiple topics (e.g., smarthome/humidity, smarthome/door) and log them to a database or display them on a dashboard.
  3. Add Rules: Use Python to create logic. For example, if temperature > 24°C, automatically publish "ON" to a fan topic.

Your Turn to Build

You now have a working smart home controller built with Python and MQTT. No subscriptions, no cloud dependencies, and no black boxes. Just clean code and open protocols.

The best part? You can start small and scale up. Add one sensor, then one light, then a whole room. The architecture is already there.

Go ahead and run the code today. Modify the topics, add your own logic, and watch your smart home come to life.

If you build something cool, share it on Dev.to or tag me in your project—I’d love to see what you create. And if you hit a snag, drop a comment below; the community is here to help.

Now, grab your Raspberry Pi, install Mosquitto, and let’s build the future of your home together.


If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!

Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.

Top comments (0)