Blog Startseite AI generated Robot Image

Create a Smart Lighting System with MQTT

2024-12-11 - Engine: Gemini

Create a Smart Lighting System with MQTT

Building a Smart Lighting System over MQTT

Components Required:

  • Lights: Smart bulbs or LED strips with MQTT support
  • MQTT Broker: A server that bridges messages between devices (e.g., Mosquitto, HiveMQ)
  • MQTT Client: Software on lights that communicates with the broker
  • Controlling Device: A device or app that sends messages to the broker (e.g., Raspberry Pi, Smartphone)

Steps:

1. Setup MQTT Broker:

  • Install and configure an MQTT broker on a server or Raspberry Pi.
  • Ensure the broker is running and listening for messages.

2. Setup MQTT Client on Lights:

  • Install MQTT client software on the lights.
  • Configure the client with the broker's connection details (IP address, port, username, password).

3. Connect Controlling Device:

  • Install an MQTT client library on the controlling device (e.g., Paho Python).
  • Create an MQTT client and connect it to the broker.

4. Sending Messages:

  • Create a unique MQTT topic ID for controlling lights (e.g., "/home/light").
  • Send messages via the controlling device to the topic to control lights.
  • Messages can contain various commands like on/off, dimming, color change.

5. Lights Receiving Messages:

  • Have lights subscribe to the topic you created in step 4.
  • When a message is received, the client processes the commands within and controls lights accordingly.

Sample Code:

Light MQTT Client (Python):

import paho.mqtt.client as mqtt

# MQTT Broker Information
broker_ip = "localhost"
broker_port = 1883

# Create new MQTT client instance
client = mqtt.Client()

# Connect to the broker
client.connect(broker_ip, broker_port)

# Subscribe to topic
topic = "/home/light"
client.subscribe(topic)

# Loop to listen for messages
client.loop_forever()

Controlling Device MQTT Client (Python):

import paho.mqtt.client as mqtt

# MQTT Broker Information
broker_ip = "localhost"
broker_port = 1883

# Create new MQTT client instance
client = mqtt.Client()

# Connect to the broker
client.connect(broker_ip, broker_port)

# Toggle light on or off
def toggle_light(state):
    message = "on" if state else "off"
    topic = "/home/light"
    client.publish(topic, message)

# Set light brightness (0-255)
def set_brightness(level):
    topic = "/home/light"
    message = str(level)
    client.publish(topic, message)

# Start a console to control lights
while True:
    command = input("Command (on/off/brightness): ")
    if command == "on":
        toggle_light(True)
    elif command == "off":
        toggle_light(False)
    elif command.startswith("brightness"):
        level = int(command.split()[1])
        set_brightness(level)
    else:
        print("Invalid command")

Note: Topic ID and command formatting can vary based on the specific lights and MQTT client library used.


Note:

All texts on this blog are generated using Artificial Intelligence (AI). The purpose of this blog is to test the generated content in the context of SEO and analyze its rankings. Please be aware that I cannot take responsibility for the accuracy or completeness of the texts published here.


© 2024 ivosys.de - Made with iMicroBlog by ivosys.de - Code CSS3 HTML5 Javascript PHP