cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending data from Raspberry Pi to Thingworx using MQTT

Highlighted
kt1
Level 5

Sending data from Raspberry Pi to Thingworx using MQTT

I want to connect raspberry pi to thingworx via mqtt. I am using Mosquitto broker and Paho mqtt as a client on raspberry pi to send data to thingworx. But i am not able to see the message on thingworx can anyone help. The steps I followed are given below.

 

Raspberry Pi

  • Installed Mosquitto broker
  • Installed Paho mqtt client
  • python script for publishing messgae is attached below. where in broker IP address of raspberry pi is written.
  • when tried to publish on the topic from raspberry pi it works fine but when tried the same for Thingworx it doesnt work.

Steps for Thingworx

  • All the steps are attached below where the server name is the Ip adress of the raspberry pi
  • After this when a any mesaage is published on broker there is no change in the property of thingworx. can anyone please tell me why?

Thanks in advance.

1 REPLY 1

Re: Sending data from Raspberry Pi to Thingworx using MQTT

Hi,

 

Did you manage to solve this issue?

I did this a while ago have a look here and let me know if this helps.

The full link to the thread is at the bottom.

Hi,

I have managed to get this working using MQTT, After reading quite a bit of documentation and the sites that you provided I'm 99% happy with the results.

This is what I did to get it working.

 

# MQTT Publish Demo

# Publish two messages, to two different topics

 

import paho.mqtt.publish as publish

#client.connect("192.168.2.207", 1883, 60)

#publish.single("CoreElectronics/test", "Hello", hostname="test.mosquitto.org")

publish.single("Thingworx/Name", "Hello", hostname="192.168.2.207")

#publish.single("CoreElectronics/topic", "World!", hostname="test.mosquitto.org")

publish.single("Thingworx/Topic", "World!", hostname="192.168.2.207")

print("Done")

 

  • This is useful but not nessary as the idea is to have that information sent from thingworx

 

  • The Client on the other hand is the important part for the PI. (I will need to tweak this but in general its working)
  • The client runs as a script and listens to the topics. In my case the topics are Thingworx/ Name , Topic and Color.
  • When the messages comes in the sense hat shows the message.

 

# MQTT Client demo

# Continuously monitor two different MQTT topics for data,

# check if the received data matches two predefined 'commands'

 

import paho.mqtt.client as mqtt

 

from sense_hat import SenseHat

 

sense = SenseHat()

# The callback for when the client receives a CONNACK response from the server.

def on_connect(client, userdata, flags, rc):

    print("Connected with result code "+str(rc))

 

    # Subscribing in on_connect() - if we lose the connection and

    # reconnect then subscriptions will be renewed.

    #client.subscribe("CoreElectronics/test")

    client.subscribe("Thingworx/Name")

    cleint.subscribe("Thingworx/Topic")

    client.subscribe("Thingworx/Color")

    #client.subscribe("CoreElectronics/topic")

 

# The callback for when a PUBLISH message is received from the server.

def on_message(client, userdata, msg):

    print(msg.topic+" "+str(msg.payload))

    #sense.show_message(msg.payload) # placing this here will print all the messages on the sence hat screen which is not optimal

    if msg.payload == "Hello":

        print("Received message #1, do something")

        # Do something

 

    if msg.payload == "World!":

        print("Received message #2, do something else")

        # Do something else

    if msg.payload =="red":

        print("Recieved a message from the PI to ")

        # Do Something here like print a message to the the sense hat screen

        sense.show_message(msg.payload) # This is a better place to do something like set the screen to red/blue/clear etc.

 

# Create an MQTT client and attach our routines to it.

client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

 

#client.connect("test.mosquitto.org", 1883, 60)

client.connect("192.168.2.207", 1883, 60)

 

# Process network traffic and dispatch callbacks. This will also handle

# reconnecting. Check the documentation at

# https://github.com/eclipse/paho.mqtt.python

# for information on how to use other loop*() functions

client.loop_forever()

 

 

Now that that was working I can connect my thingworx server and test the theory.

Create a new thing and use the MQTT template.

 

Create 3 properties called Name Topic and Screen_Color

In configuration the Name must be the same as the property and the value that you want it to be set to must be the full path to the topic.

For example Name: Screen_Color Topic Thingworx/Color

 

https://community.ptc.com/t5/ThingWorx-Developers/Is-it-possible-to-trigger-a-raspberry-pi-with-data...