Experiment 2

  • L2.1 Name
  • MQTT with Broker Raspberry Pi 5 and Pico2/5 W client



  • L2.2 General presentation, purpose

  • The experiment aims to provide a possibility to explore the MQTT protocol using as a central core the Moskitto Broker application and a Raspberry Pi 5 AI Kit platform. For the development of publisher/subscriber nodes, Raspberry Pi Pico 2 W platforms with sensors and actuators are used. Publisher programs are developed on Pico and subscriber programs on Pi5. At the end of the experiment, detailed information will be available about the creation of a network of sensors/actuators interconnected to an MQTT broker. The possibility of exploring the literature with MQTT specifications is another aspect pursued.

  • L2.3 Resources
  • Hardware: Rpi Pico 2 W, Thonny, Micropython, Platform Raspberry Pi AI kit



  • L2.4 Prototype Program


  • For Pico 2 W
    1. Send/Publish data program for Pico2 with DHT22
    2. Send/Publish JSON data program for Pico2 with DHT22

    For Pi 5
    3. Receive/subscribe sensor data program on PI 5
    4. JSON format data reception/subscriber program on PI 5

  • L2.5 How to conduct/perform the experiment
  • MQTT (Message Queuing Telemetry Transport) is a communications protocol for M2M (Machine to Machine) and IoT applications that uses a server that can receive data and then provide it to a client entity. is the "gold standard" in the Internet of Things (IoT) world. It was created in 1999 to monitor oil pipelines via satellite, so it is built from the ground up to be extremely efficient and resilient to poor connection performance. The main components are represented in the figure below:

    The command line for installing Mosquitto broker on Raspberry pi 5 is the following:

    > sudo apt install mosquitto mosquitto-clients -y

    Broker activation/deactivation line:

    >sudo systemctl start mosquitto.service
    >sudo systemctl stop mosquitto.service

    Configure start/stop broker at Linux startup:

    >sudo systemctl enable mosquitto.service
    >sudo systemctl disable mosquitto.service

    Verify operation Broker:

    > mosquitto -v
    A possibility to visualize the proper functioning can be achieved using 2 sessions in 2 terminals, in one the publisher function is called and in the other the subscriber function, the result being observable in the images below:

    Terminal 1:


    Terminal 2:


    To allow access to external client systems, the configuration file must be modified as follows:

    > sudo nano /etc/mosquitto/mosquitto.conf

    where the following 2 lines must be added:

    listener 1883
    allow_anonymous true

    To enable these functions, the broker must be restarted as follows:

    > sudo systemctl restart mosquitto

    Mosquitto Client with Pico 2 W

    The schematic of an MQTT client made with Pico 2 W is represented in the following figure.



    In this case, a DH22 humidity and temperature sensor is used, prototype programs 1 and 2 perform the MQTT client function, the first one sends the data directly, the second one sends it in JSON format.
    The RP2350 processor in the Pico2 W structure has multiple interfaces with which an extended range of MQTT clients can be made for sensors and actuators. In this case, a potentiometer connected with the cursor to the ADC! to replace the analog sensors, a DS3231 real-time clock to communicate time information and a TM1637 local display for testing subscriber situations are included.

    MQTT Subscriber with Raspberry PI 5

    Visualizing the data sent by the client can be done on the raspberry pi 5 with the prototype programs 4 and 5 that implement subscriber functions equivalent to calling the following command line:

    >mosquitto_sub -h localhost -t "casa/sensor/umiditate"



    to receive data in JSON format, you can use the prototype program no. 4 with a subscriber function on Raspberry Pi 5 that will return the following information:



  • L2.6 Proposed Problems


  • 1. Based on experiment 1, create a client that will send atmospheric pressure to the Broker on Pi5.
    2. Based on experiment 2, create a program that will activate an LED on the PI when the humidity sent by a client exceeds 40%.
    3. Create a client/node with Pico 2, with a sensor and potentiometer connected to the ADC whose values ​​will be sent to the Broker.
    4. Experiment with a system/node with Pico 2 with dual functions, namely publisher and subscriber.
    5. Develop 2 systems/nodes with Pico 2 that will communicate with each other via the Mosquitto Broker on Raspberry Pi 5.



  • L2.7 The experiment can be extended for:
  • L2.8 Documentary references:

  • © 2010-2026 - Embedded Advanced Computer