Hi Team
I need some help, on this requirement the program must write it using python.
software written that gets X, Y, Z translations and rotations from a 3D connexion space mouse and publishes the data to a MQTT topic on a specific broker.
The data must be formatted as a JSON document with keys for Translation X , Translation Y, Translation Z, Pitch, Roll, Yaw .
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saravanan GanesanPosted Aug 4, 2023, 7:17 PM
Install Required Libraries:
Make sure you have the required libraries installed. You'll need the paho-mqtt library for MQTT communication and the pyserial library to read data from the 3Dconnexion space mouse.
pip install paho-mqtt pyserial
Initialize MQTT Client:
Create a Python script to initialize an MQTT client and connect it to the MQTT broker.
import paho.mqtt.client as mqtt
mqtt_broker = "broker_address"
mqtt_port = 1883
mqtt_topic = "topic_name"
client = mqtt.Client()
client.connect(mqtt_broker, mqtt_port)
Read Data from 3Dconnexion Space Mouse:
Use the pyserial library to read data from the 3Dconnexion space mouse. The exact steps might vary depending on the specific model and protocol of the space mouse.
import serial
ser = serial.Serial('COMx', 115200) # Replace 'COMx' with the correct serial port name and baud rate.
Format Data and Publish as JSON:
Read the X, Y, Z translations, and rotations from the space mouse, and format the data as a JSON document.
import json
while True:
# Replace the following lines with the actual code to read data from the space mouse.
x_translation = 0.0
y_translation = 0.0
z_translation = 0.0
pitch = 0.0
roll = 0.0
yaw = 0.0
data = {
"Translation X": x_translation,
"Translation Y": y_translation,
"Translation Z": z_translation,
"Pitch": pitch,
"Roll": roll,
"Yaw": yaw
}
json_data = json.dumps(data)
client.publish(mqtt_topic, json_data)
Run the Script:
Run the Python script to start reading data from the 3Dconnexion space mouse and publishing it to the MQTT topic on the broker.