You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/translations/en/1-getting-started/lessons/4-connect-internet/single-board-computer-mqtt.md

3.9 KiB

Control your nightlight over the Internet - Virtual IoT Hardware and Raspberry Pi

The IoT device needs to be programmed to communicate with test.mosquitto.org using MQTT to send telemetry data with the light sensor readings and receive commands to control the LED.

In this part of the lesson, you will connect your Raspberry Pi or virtual IoT device to an MQTT broker.

Install the MQTT client package

To communicate with the MQTT broker, you need to install an MQTT library pip package either on your Raspberry Pi or in your virtual environment if you are using a virtual device.

Task

Install the pip package

  1. Open the nightlight project in VS Code.

  2. If you are using a virtual IoT device, ensure the terminal is running the virtual environment. If you are using a Raspberry Pi, you won't be using a virtual environment.

  3. Run the following command to install the MQTT pip package:

    pip3 install paho-mqtt
    

Code the device

The device is ready to be programmed.

Task

Write the device code.

  1. Add the following import to the top of the app.py file:

    import paho.mqtt.client as mqtt
    

    The paho.mqtt.client library allows your app to communicate using MQTT.

  2. Add the following code after the definitions of the light sensor and LED:

    id = '<ID>'
    
    client_name = id + 'nightlight_client'
    

    Replace <ID> with a unique ID that will serve as the name of this device client and later for the topics that this device publishes and subscribes to. The test.mosquitto.org broker is public and used by many people, including other students working on this assignment. Using a unique MQTT client name and topic names ensures your code won't interfere with anyone else's. You will also need this ID when creating the server code later in this assignment.

    💁 You can use a website like GUIDGen to generate a unique ID.

    The client_name is a unique identifier for this MQTT client on the broker.

  3. Add the following code below this new code to create an MQTT client object and connect to the MQTT broker:

    mqtt_client = mqtt.Client(client_name)
    mqtt_client.connect('test.mosquitto.org')
    
    mqtt_client.loop_start()
    
    print("MQTT connected!")
    

    This code creates the client object, connects to the public MQTT broker, and starts a processing loop that runs in a background thread, listening for messages on any subscribed topics.

  4. Run the code in the same way as you ran the code from the previous part of the assignment. If you are using a virtual IoT device, make sure the CounterFit app is running and the light sensor and LED have been created on the correct pins.

    (.venv) ➜  nightlight python app.py 
    MQTT connected!
    Light level: 0
    Light level: 0
    

💁 You can find this code in the code-mqtt/virtual-device folder or the code-mqtt/pi folder.

😀 You have successfully connected your device to an MQTT broker.


Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we aim for accuracy, please note that automated translations may include errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.