MQTT Python Example - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

Overview

This page describes how to set up the MQTT Client-Server model on Buildroot using python libraries. A working example for Raspberry Pi can be found at this pull request

Introduction

The MQTT protocol is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. Designed as an extremely lightweight publish/subscribe messaging transport, it is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

Paho-MQTT Project provides a client class that enables applications to connect to an MQTT broker to publish messages, and to subscribe to topics and receive published messages. It also provides some helper functions to make publishing one-off messages to an MQTT server very straightforward.

Paho is an Eclipse Foundation project.

You can learn more about the Python paho-mqtt library here : Paho Mqtt

Target Build System

Buildroot version 2019.05.x is used as a bring-up tool in order to manage kernel, toolchain and packages for this example.

Hardware Platform

Raspberry Pi 3B+ has been chosen as target hardware platform for the demo of this MQTT python example.

Implementation Steps:

  1. This working example of MQTT Server/Client needs python2.7 or above setup on Buildroot.

  2. For setting up python2.7 follow below-mentioned steps in your Buildroot assignment base:

     A. open buildroot directory.
    
     B. open menuconfig using "$ make menuconfig"
    
     C. Go to "> Target packages > Interpreter languages and scripting" and select python package.
    
     D. After selecting the python package go to the "core python module" and select hashlib package.
    
     E. After selecting core-modules, Go to "> Target packages > Interpreter languages and scripting > python > 
        External python modules" and select "python-paho-mqtt" package. You also need to have "WiFi" setup on buildroot 
        which you can set up using this link in step 12. 
    
     F. After selecting python packages, Go to "> Target packages > Networking applications " and select "mosquitto" 
        package. Mosquitto is an open-source message broker service that uses the MQTT protocol to send and receive 
        messages.
    
     G. You can also add other packages from "> Target packages > Interpreter languages and scripting > python > 
        External python modules" path as per your application requirement.
    
  3. After setting up the Python with Mqtt-paho library, create a rootfs-overlay folder in base_external directory of your Buildroot assignment base and follow steps A and B mentioned above.

  4. In menuconfig Go to "> System configuration" and add path of your rootfs_overlay to the users table as mentioned below:

rootfs_overlay

  1. Add Server Client scripts inside bin directory(create one if you havn't) in rootfs_overlay.

  2. run build.sh and load the sdcard.img into sdcard and load this image into your raspberry-pi board.

  3. After login, open bin directory where Server Client scripts are stored already.

Client-Server Scripts

  1. On the client-side, run Client-Publisher.py script on the command line to set up MQTT Client and provide IP address of the server you wish to connect with. for example:

        # python2.7 Client-Publisher.py 10.0.0.185
    
  2. On the Server-side, run Server-Subscriber.py script on the command line as below.

        # python2.7 Server-Subscriber.py
    

Run Scripts

  1. Once the connection is established between client and server, the server will show connection established with code 0, and the client will ask for the data you want to send to the server-side.

  2. In this way, you can establish communication between client and server using python paho-mqtt and the same can be modified as per your application. The working demo is given below In the demo section.

  3. WiFi Setup on Buildroot.

Steps to set up MQTT python example on your Buildroot.

Please follow below-mentioned steps to set up this MQTT python example on your Buildroot.

     A. git clone https://github.com/Sankalppund/buildroot-assignments-base
     B. git checkout hw-rpi-mqtt-python-br-2019.05
     C. run ./build.sh 
     D. run ./build.sh again for a successful build of packages.
     E. run ./save_config.sh
     F. check if all the required packages are mentioned in the implementation step above are present in 
        /base_external/configs/aesd_rpi_defconfig. if not, then follow the implementation steps.
     G. After a successful build, follow steps from 6 to 11 mentioned in the implementation steps.

Note: (You need to have Network connectivity for example through Wi-Fi or Ethernet in order to test this example)

Demo

The Connection between Client and Server is established and data from the client-side has been sent to the server-side.

References:

Python-paho MQTT

https://github.com/eclipse/paho.mqtt.python