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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Is it possible to trigger a raspberry pi with data from the ThingWorx platform?

mkeppeler
1-Newbie

Is it possible to trigger a raspberry pi with data from the ThingWorx platform?

Hello everybody,

I want to address some raspberry pi's with data from the ThingWorx platform.

Is this possible in general ? And if, how can I do that?

Thank you in advance

Have a nice day

Matthias

10 REPLIES 10

Hello, what exactly are you asking? Would you like to connect a Raspberry Pi to ThingWorx Composer?

Have you seen this tutorial? Weather Applications with Raspberry Pi - ThingWorx : ThingWorx

Thanks!

Tori

BryanK
14-Alexandrite
(To:ttielebein)

Hi,

So this is an interesting question and its something that I'm also trying to get working.

I have the following setup.

1. Raspberry pi 3 with sense hat.

2. VM with TWX8, mfg_apps loaded and Kepware.

In kepware i can set the value of a property using the Asychronous 2.0 Write

And that works quite well to read a message accross the sense hat and show a picture, (both predefined)

I have also made that property in Thingworx and set the value to evoke the same thing.

(so essentially this works as long as I'm connected to kepware and i have saved the thing etc etc etc.)

The scenario I want to test now is with without kepware

I have a simple python script that waits for input from the user and then once it is received it runs it.

like this.

prompt = raw_input ("enter your message: ")

sense.show_message (prompt )

I want to be able to have a mashup where I can enter a random text string like "bob was here" and that will display on the sense hat.

Please let me know if you need more detail.

Regards

Bryan

Greetings Bryan,

It looks like you followed the steps to configure the Sense Hat laid out in the Sigma Tile Workshop, here:

Sigma Tile Workshop Guide.pdf

Part of that setup involves installing the Python library 'pymodbus' to implement the Modbus protocol on the Raspberry Pi. The Raspberry Pi was then able to fulfill the function of a Modbus Slave, which was how the KEPServerEX Modbus TCP/IP Ethernet Driver interfaced as the Modbus Master. This configuration is a great example of a solution involving KEPServerEX and ThingWorx. KEPServerEX enables connectivity to many Industrial Automation protocols, with Modbus being one of the most widely used.

There is a discussion of alternate scripts that can be used to interface the Raspberry Pi/Sense Hat directly with ThingWorx, which can be found in the ThingWorx Developer Community, here:

Re: Thingworx and EMS

Please let us know how it goes with trying out the suggestions in that thread.

Best regards,

Steven M

BryanK
14-Alexandrite
(To:stmarchessault)

Hi Steven,

Thanks for the Reply.

Yes I did follow those links and lots more, I used the API guide to create a script that runs on the pi that reads the sensor data and sends it to TWX.

I have this running every 10 second and I even have the screen flash every time the script runs.

Its not perfect but, what is great is that, I can now show and explain this using Kepware and not.

Using Kepware as per the Guide provided you can set the value of "screen control" from 0 to 5 and it will run a predefined python script to show a message and then a picture. I have created a button on my mashup that I can select that does this. Happy daze,

... but and this is actually my question, using either Kepware or just a script.

How to I send my own message to the sense hat from thingworx.

To word it a little differently, I have a Property? Service? event?  on my RaspberryPiThing that waits for input. this input it somehow sent to the Raspberrypi and starts the script.

The script will do something like this.

while true

     send Temp

     send Humidity

     flash screen
     read a file? value?  from thingworx if available and print message on sensehat

     wait 10 seconds

 

hope this helps clarify what I'm after.

Regards

Greetings Bryan,

Unless I am misreading your request, I think the answer you are looking for will be in this thread:

Thingworx and EMS

Specifically, the helpful conversation and suggestions given by Sushant Pandey​ and Denis Bujoreanu​ seem to address the breadth of options available for interfacing directly between the Raspberry Pi and ThingWorx to pass the values from the Sense HAT sensors

Best regards,

Steven M

BryanK
14-Alexandrite
(To:stmarchessault)

Hi Steven,

Yep thanks I used that thread to achieve 99% of what I'm trying to do. I did not use LUA to do this but the great thing is that there is yet another way to get this right. In the thread Denis Bujoreanu mentions the following:

The key idea is that you can call from your Pi LUA script, i.e. a PiTemplate.lua script to read basic Pi information, other Python scripts that read sensor data from the Sense HAT and then supply thier results back to set Thingworx thing properties values. (this I can do)


You can also send from Thingworx strings to be displayed on the Sense HAT LED matrix by defining a remote service on the Pi thing and call it with the string to be displayed.      (this is what I'm trying to do) and the rest of the thread does not address this.

Thanks for your help so far.

Regards,

Bryan

Hi Bryan,

Okay, I follow. I cannot find a specific example of this. Though, after some research, it appears this should be possible.

If your goal is that changing values will be moved from ThingWorx to the Raspberry Pi, then displayed on the LED display, here are some additional suggestions and resources that will hopefully bring more clarity.

1. ThingWorx could either host the changing values as a REST Server, and the Raspberry Pi would use the REST GET command to read the values; or, you could use the MQTT Publish/Subscribe methods to move the values in either direction via an MQTT Broker.

ThingWorx Community blog post, discussing the ThingWorx REST API:

REST API Overview and Examples

ThingWorx Community Blog, describes the use of the MQTT Marketplace Extension to connect with an Arduino. The ThingWorx side of things should still be relevant:

Delivering Arduino Collected Data to ThingWorx using MQTT

2. Now, in the Raspberry Pi, you will need to parse either the REST or MQTT payload.

RPI JSON parsing/assigning variables example, uses the 'requests' Python library:

Worksheet - People in Space Indicator | Raspberry Pi Learning Resources

Subscribe RPI to an MQTT Broker example, uses the 'imapclient' Python library.

https://www.rs-online.com/designspark/plug-and-play-iot-with-the-pi-sense-hat

3. I was unsure if the Raspberry Pi sense.show_message() method would allow a variable to be defined (instead of just a static text string), but according this example it can:

https://pimylifeup.com/raspberry-pi-email-notifier/

I am fairly new to Raspberry Pi and am unable to test this suggestion at the moment. Ideally, there are other users out there who can chime in on this. Please let us know how it goes.

Best regards,

Steven M

BryanK
14-Alexandrite
(To:stmarchessault)

Hi Steven,

Thanks for the detailed response, I will test out the MQTT method. Hopefully this will be the answer!

Regards

Bryan

BryanK
14-Alexandrite
(To:BryanK)

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

By setting the properties here I can update the PI!

And the Message shows on the Sensehat!

Hello Bryan,

That is really great news! Thank you kindly for sharing the well-organized details of your methods with everyone here. I am sure this will be useful information for other users.

Matthias Keppeler​: Following up with you on the original question posted here, can you please indicate if the recommendation given by Bryan Kirchner in his post on 25 Sept adequately answered your question? The best way to do this is to mark the post as the "Correct Answer."


Or, if you are still unresolved on your original question, let us know and we can continue troubleshooting this with you. This feedback is a key step toward ensuring Community users with a similar issue are able to locate the information they need.


Thanks again to you both for your helpful participation in the ThingWorx Developer Community.



Best regards,

Steven M

Top Tags