How to write code to upload data to mqtt cloud server through edgexfoundry

Written in front: This tutorial refers to the official documentation EdgeX Foundry Hands On Tutorial

The tools used are: Ubuntu system (with docker and docker-compose installed), postman tool and MQTTBox tool ( mqtt service construction tutorial)

The data is not collected by real sensor devices, but random numbers are generated by python programs. I am also a novice in the Internet of Things. All codes are drawn according to gourds. If there are mistakes or mentally handicapped operations, please point out directly in the comment area Greatful!

  • *You can follow my steps to generate files in sequence, or you can download all files at one time. Here is an address to download all files: EdgeX_Tutorial

1. Create a folder

Download the docker-compose file and pull the image

mkdir geneva1
cd geneva1

Here is a docker-compose address: (This docker-compose file is in the tutorial, which is different from the docker-compose file of the official official project. It does not have functions such as ui. I don’t know if the file of the official project can be used, and try again later try)



After downloading, save it in the geneva1 folder and rename it to: docker-compose.yml

Pull the image:

docker-compose up
docker-compose ps verify

As shown in the figure:

2. Create the device

There are three steps to creating a device

  • Create value descriptors
  • upload device profile
  • create device

2.1 Create value descriptors

value descriptors describe the data format and labels of Edgex, our data is just random numbers, created using postman

postman mode select post

http://<edgex ip>:48080/api/v1/valuedescriptor

Set the Body to "raw" and "JSON" (I'll explain why later)

    "name": "number",
    "description": "Random number",//Description Write whatever you want
    "min": "0",
    "max": "200",
    "type": "Int64",
    "uomLabel": "number1",
    "defaultValue": "0",
    "formatting": "%s",
    "labels": [

If there is no problem, a string of ID s will be generated, don't remember, just know that it means success

If your data has multiple attributes, change the content of the body description and post it a few more times.

2.2 Upload device profile

A device profile is essentially a template that describes a device, its data format, and supported commands. It's a text file written in YAML format, uploaded to EdgeX, and referenced later whenever a new device is created. Only one configuration file is required per device type. Similarly, we also use postman to upload

postman mode select post

http://<edgex ip>:48081/api/v1/deviceprofile/uploadfile

Finally as shown:

Here is number_porduce.yaml (draw the scoop according to the gourd, do not spray)

name: "number_produce"                             // remember this name
manufacturer: "liu"
model: "aaaa"
  - "rpi"  //I don't know why it's rpi
description: "suiji chansheng shuzi"

      - name: number
        description: "suiji chansheng shuzi"
             { type: "Int64", readWrite: "RW", minimum: "0", maximum: "100", size: "4", LSB: "true", defaultValue: "0"}

If there is no problem, a string of ID s will be generated.

2.3 Create device

Since we are generating the device with rest, use the device service "edgex device rest"

postman mode select post

http://<edgex ip>:48081/api/v1/device

body is

    "name": "number_device",
    "description": "suijishushengcheng",
    "adminState": "unlocked",
    "operatingState": "enabled",
    "protocols": {
        "example": {
            "host": "dummy",
            "port": "1234",
            "unitID": "1"
    "labels": [
    "location": "Tokyo",
    "service": {
        "name": "edgex-device-rest"
    "profile": {
        "name": "number_produce"       //Corresponds to the name of number_porduce.yaml above

If there is no problem, a string of ID s will be generated.

3. Upload data

3.1 First create a random folder and create a python virtual environment

  • sudo apt install python3-venv -y
  • . ./venv/bin/activate
  • pip install requests

3.2 Copy the python file used to generate the data (

import requests
import json
import random
import time

edgexip = ''//Change to your own ip running edgex
number = 66

def generateSensorData(number): 
    number = random.randint(number-5,number+5)
    print("Sending nums: Value %s" % (number))

    return (number)

if __name__ == "__main__":
    sensorTypes = ["number"]
        (number) = generateSensorData(number)
        url = 'http://%s:49986/apurce/Temp_and_Humidity_sensor_cluster_01/humidity' % edgexip
        payload = number
        headers = {'content-type': 'application/json'}
        response =, data=json.dumps(payload), headers=headers, verify=False)

3.3 python3 ./

As shown in the figure:

Fourth, export the data to the mqtt server

Here we choose to use the kuiper rule engine to export the data, and we can also use the APP SERVICE to export, you can see the official documentation for details.

Here is a brief introduction to the kuiper rule engine, which has three steps:

  • Create a data stream, which is the data you upload
  • Create a rule
  • enforce the rules

4.1 Create data flow, use postman

    "sql": "create stream number_test() WITH (FORMAT=\"JSON\", TYPE=\"edgex\")"

4.2 Create rules

    "id": "mqtt_export_rule1",
    "sql": "SELECT * FROM number_test",    /// must be the same name as the previous step
    "actions": [
            "mqtt": {
                "server": "tcp://", //mqtt shared server address
                "topic": "EdgeXFoundryMQTT_01",                //Subscribed topics
                "username": "someuser",
                "password": "somepassword",
                "clientId": "someclientid"
            "log": {}

4.3 Open the MQTTBox software

The red box is the server address of the previous step, after save

The topic of subscription should be the same as the one in the rule. Next, you will receive the data sent by edgex. This data format is defined by the previous value description.

This is just to implement a simple data upload function. Later, I will continue to explore how to use the rule engine to process the passed data. Will write again!

Posted by guymclaren on Wed, 04 May 2022 09:17:35 +0300