1

I want to send data to ThingsBoard and display it in widget On dashboard.

By Following this tutorial I have:

  1. Created Device on ThingsBoard by logging into Tenant Administrator.

Device Description:

enter image description here

Device created sucessfully:

enter image description here

Device Access Token:

enter image description here

  1. Created MQTT client in python with the access token of my device and publish data there.

But Data was not received on the platform as my devices latest telemetry was not showing any data.

Image showing DH11 Device latest telemetry:

enter image description here

Python Code:

import paho.mqtt.client as paho             #mqtt library
import os
import json
import time
from datetime import datetime

ACCESS_TOKEN='NN7QEiWaX6mxPRnVdJsQ'                 #Token of your device
broker="demo.thingsboard.io"                #host name
port=1883                       #data listening port

def on_publish(client,userdata,result):             #create function for 
    callback
    print("data published to thingsboard \n")
    pass
client1= paho.Client("control1")                    #create client object
client1.on_publish = on_publish                     #assign function to 
callback
client1.username_pw_set(ACCESS_TOKEN)               #access token from 
thingsboard device
client1.connect(broker,port,keepalive=60)           #establish connection

while True:

   payload="{"
   payload+="\"Humidity\":60,"; 
   payload+="\"Temperature\":25"; 
   payload+="}"
   ret= client1.publish("v1/devices/me/telemetry",payload) #topic- 
   v1/devices/me/telemetry
   print("Please check LATEST TELEMETRY field of your device")
   print(payload);
   time.sleep(5)

What might be the issue? I am looking ahead for your suggestions in fixing it. Thanks in Advance.

EDIT: I have reported this Issue on github as well

4
  • You have not replaced the token. So please replace your device token in your python code Oct 7, 2019 at 8:08
  • Yes, once you set the username correctly, you should see what we saw youtube.com/watch?v=5oQwyyWIZnw Oct 8, 2019 at 12:53
  • @GambitSupport, Currently I am working on another project, I need some time to reproduce this issue, I'll respond you back in few days and let you know that if this works or not! Thank you very much for your response,
    – Al Fahad
    Oct 9, 2019 at 4:29
  • @sagarkhamkar. Currently I am working on another project, I need some time to reproduce this issue, I'll respond you back in few days and let you know that if this works or not! Thank you very much for your response,
    – Al Fahad
    Oct 9, 2019 at 4:30

2 Answers 2

2

I have used the Docker image of Thingsboard in my local machine to run the appliaction. You can follow the steps with this link, it's easy to follow if you know a little about Docker containers... thingsboard/tb

I have used this credentials to send data over MQTT by Python Client. Customer User: [email protected] / customer

This is the code in Python:

import paho.mqtt.client as mqtt     
import time
import datetime

ACCESS_TOKEN        = 'A1_TEST_TOKEN'    #Token of your device
BROKER              = "127.0.0.1"  
PORT                = 1883   #data listening port

payload = {
    "datetime": str(datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")),
    "dummy": 20
}

def on_publish(client,userdata,result):
    print("data published to thingsboard \n")

client1 = mqtt.Client("control1")   #create client object
client1.on_publish = on_publish     #assign function to callback
client1.username_pw_set(ACCESS_TOKEN)   #access token from  thingsboard device
client1.connect(BROKER , PORT , keepalive=60)   #establish connection

val = 0

while True:
    payload["dummy"] = val
    payload["datetime"] = str(datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
    ret = client1.publish("v1/devices/me/telemetry", str(payload)) #topic- v1/devices/me/telemetry
    val += 1

    print("Please check LATEST TELEMETRY field of your device")
    print(payload)
    
    time.sleep(2)
0
1

I'm not familiar with this application, but if security allows use MQTT.fx to subscribe to this topic and sniff what is being published. Could be malformed JSON? Considering they don't appear to require TLS this should be possible.

Also, it's been awhile since I've used Python, but your semicolons confuse me.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.