> For the complete documentation index, see [llms.txt](https://docs.datacake.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datacake.de/api/internal-mqtt/client-libraries.md).

# Example Code

Example code to connect popular MQTT libraries to the Datacake MQTT broker

In this section, you will find some examples of how to use some of the many available MQTT libraries and software functions to connect your Device to Datacake using MQTT.

## Paho MQTT

### Python

The following code snippet shows you a very basic implementation on how to send Data from your devices to Datacake using Python, Paho MQTT Library.

For this example, we have been using [Tinkerforge Hardware](https://www.tinkerforge.com/) to generate real data.

```javascript
from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_particulate_matter import BrickletParticulateMatter
import time
import paho.mqtt.client as mqtt

client = mqtt.Client()

HOST = "localhost"
PORT = 4223
UID = "FX5"

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

def cb_pm_concentration(pm10, pm25, pm100):
    client.publish("dtck-pub/balena-fin-cellular-demo/0e5d7891-7b73-4377-a7f9-f837a12df327/PM25", pm25)
    client.publish("dtck-pub/balena-fin-cellular-demo/0e5d7891-7b73-4377-a7f9-f837a12df327/PM10", pm100)

if __name__ == "__main__":

    ipcon = IPConnection()
    pm = BrickletParticulateMatter(UID, ipcon)

    ipcon.connect(HOST, PORT)

    pm.register_callback(pm.CALLBACK_PM_CONCENTRATION, cb_pm_concentration)
    pm.set_pm_concentration_callback_configuration(60000, False)

    client.on_connect = on_connect
    client.on_message = on_message

    client.tls_set()
    client.username_pw_set("yourdatacaketokenhere", password="yourdatacaketokenhere")
    client.connect("mqtt.datacake.co", 8883, 60)
    client.loop_forever()
```

### Javascript

```javascript
// Create a client instance
client = new Paho.MQTT.Client("mqtt.datacake.co", 9001, "clientId");
 
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
 
// connect the client
client.connect({
 onSuccess: onConnect,
 useSSL: true,
 userName: "API TOKEN",
 password: "API TOKEN"
});
 
 
// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("dtck/mqtt-test/06a446aa-6fba-4813-a6ce-b2e3c16f9ebb/+");
  
  message = new Paho.MQTT.Message("23.5");
  message.destinationName = "dtck-pub/mqtt-test/06a446aa-6fba-4813-a6ce-b2e3c16f9ebb/TEMPERATURE";
  client.send(message);
}
 
// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:" + responseObject.errorMessage);
  }
}
 
// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:" + message.payloadString);
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.datacake.de/api/internal-mqtt/client-libraries.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
