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 to generate real data.
// Create a client instanceclient =newPaho.MQTT.Client("mqtt.datacake.co",9001,"clientId");// set callback handlersclient.onConnectionLost = onConnectionLost;client.onMessageArrived = onMessageArrived;// connect the clientclient.connect({ onSuccess: onConnect, useSSL:true, userName:"API TOKEN", password:"API TOKEN"});// called when the client connectsfunctiononConnect() {// 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 =newPaho.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 connectionfunctiononConnectionLost(responseObject) {if (responseObject.errorCode !==0) {console.log("onConnectionLost:"+responseObject.errorMessage); }}// called when a message arrivesfunctiononMessageArrived(message) {console.log("onMessageArrived:"+message.payloadString);}