> For the complete documentation index, see [llms.txt](https://docs.airtrace.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.airtrace.io/data-sending/clients/mqtt/python-example.md).

# Python example

```
#!/usr/bin/env python3

import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, flags, rc):
    global dev_id_enc
    print("rc"+str(rc))
    if rc==0:
        print(dev_id+": connected ok")

def on_message(client, userdata, msg):

    array = msg.payload.decode().split(',')
    global dev_id
    print("<<<<<<<<<<<<< The IoT service RECEIVES something")
    print(str(array))
    print(str(msg.topic))

    if msg.topic == 'admin/provider/'+str(dev_id):
        global password
        password = array[0]
        print("2. "+ dev_id+": password received - " +password)

if __name__ == '__main__':

    password = ''

    # The IoT device knows his own dev_id
    dev_id = "asdfasdf"

    # 0. First connection for credential retrieval
    client = mqtt.Client()
    client.on_connect=on_connect
    client.on_message=on_message
    client.user_data_set('')
    client.connect("broker.airtrace.io",1883,60)
    client.subscribe("admin/provider/" + str(dev_id))

    # 1. We do not have token, so first we need to get one by asking admin/provider topic
    client.loop_start()
    res = client.publish("admin/provider", dev_id)
    print("1. >>>>>>>>>>>>> The IoT service does not have password. Connect to admin/provider topic and request one.")
    time.sleep(10)

    client.loop_stop()
    client.disconnect()

    # 1. Second connection (authenticated) for data submission
    client2 = mqtt.Client()
    client2.on_connect=on_connect
    client2.on_message=on_message

    print(dev_id, password)
    client2.username_pw_set(dev_id, password)
    client2.connect("broker.airtrace.io",1884,60)
    client2.loop_start()

    # Sending data
    i = 0
    while i < 5:

        payload = "200,c8y_Radon,R,16,,2021-11-07T00:30:06Z"
        print(
            "3. >>>>>>>>>>>>> The IoT publishes messages in the topic admin/data")
        res = client2.publish('admin/data/'+dev_id, payload)
        time.sleep(1)
        i = i + 1

    client2.loop_stop()
    client2.disconnect()

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.airtrace.io/data-sending/clients/mqtt/python-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
