1
votes

I got a python program on Raspberry Pi 3B sending data on Thingsboard. The device in Thingsboard is connected as a single entity to the dashboard. When running the program, showing the data on dashboard's charts and cards works, and server-side RPC commands can be sent trough the rule chain. But the control widgets claim that "Device is offline."

Here's the Raspberry's code:

import paho.mqtt.client as mqtt
import time
import json
import random

def on_message(client, data, message):
  print json.loads(message.payload)

def main():
  client = mqtt.Client("python1")

  client.connected_flag=False
  client.suppress_puback_flag=False

  client.on_message = on_message

  token=[DEVICE ID]
  if token !="":
     pass
  client.username_pw_set(token, "")

  client.connect([MY THINGSBOARD HOST'S IP], 1883)
  client.subscribe("v1/devices/me/attributes", 1)
  client.subscribe("v1/devices/me/rpc/request/+", 1)
  time.sleep(2)

  while not client.connected_flag:
     client.loop()

  while True:
      message = {"number": random.randint(0, 100)};
      message_json = json.parse(message)

      client.publish("v1/devices/me/telemetry", message_json, 1)
      client.loop_start()
      time.sleep(3)
      client.loop_stop()

if __name__== '__main__':
    main()

How can I get the control widgets to realize that device is online?

1

1 Answers

0
votes

When you open dashboard control widget send to you request:

{"method":"getValue","params":{}}

You need to respond for this message before timeout. Otherwise device goes offline and I don't know if there is a way to make device online.

EDIT

J-Dobu, Yes you need to use two-way RPC.

Please find step by step path:

To solve your problem (assuming your API is HTML API): 1) When you open your dashboard RPC widget generates RPC command 2) Subscribe this command using message: curl -v -X GET http://localhost:8080/api/v1/$ACCESS_TOKEN/rpc 3) Thingsboard response should look similar to that: {"id":956,"method":"getValue","params":null} 4) Reply for this using command (replace 956 with your ID in your case): curl -v -X POST -d '20' https://cloud.thingsboard.io/api/v1/$ACCESS_TOKEN/rpc/956 --header "Content-Type:application/json"

Control RPC Widget has some timeout (it is on edit mode and advanced tab). Make sure that you reply in this timeout time period. Otherwise device will generate again RPC command with another ID and it won't listen to last reply.

In root rule chain I have directly connected Message Type Switch to RPC Call request. Make sure you have the shortest path. In my case that was the problem. When I tried to add some logic it didn't work properly.

Regarding to your Raspberry if you don't see proper answer you might use your API wrong. Are you able to see proper response with id and method?