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
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
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.
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.
It looks like you followed the steps to configure the Sense Hat laid out in the Sigma Tile Workshop, here:
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:
Please let us know how it goes with trying out the suggestions in that thread.
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.
read a file? value? from thingworx if available and print message on sensehat
wait 10 seconds
hope this helps clarify what I'm after.
Unless I am misreading your request, I think the answer you are looking for will be in this thread:
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
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.
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:
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:
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:
Subscribe RPI to an MQTT Broker example, uses the 'imapclient' Python library.
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:
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.
Thanks for the detailed response, I will test out the MQTT method. Hopefully this will be the answer!
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")
# 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.
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#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
# for information on how to use other loop*() functions
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!