I have started using MQTT to control SharpCap. I use a two PC system. A laptop connected to the scope and a desktop in my office. The laptop has a Mosquitto Broker installed. I was using Mosquitto on a Raspberry PI, that I use for IOT projects, but decided to have one specifically for SharpCap control.
There is great MQTT python library (pure python) paho.mqtt (https://pypi.org/project/paho-mqtt/) that is very straightforward to integrate and use in SharpCap. I ripped some of the code out of my full script and copied it below as an example of how easy is it to integrate.
I could never get the web server code to work reliably and it always ended up locking and crashing SharpCap when I tried to close it down. If anybody has found a web server solution, I would love to see the code
Have fun
Pete
Code: Select all
import sys
import random
import time
# The location of the paho-mqtt python package (install where you like and tell SharpCap where other python packages live)
sys.path.append(r"C:\Users\xxxxx\AppData\Local\MyPythonPackages")
from paho.mqtt import client as mqtt_client
# Mosquitto MQTT Broker
broker = '192.168.0.xxx'
port = 1883
# Script subscribes to the command topic
CommandTopic = "SharpCap/command"
# Script published to the out topic
ResultTopic = "SharpCap/out"
# Generate a Client ID with the subscribe prefix.
client_id = f'subscribe-{random.randint(0, 100)}'
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
SCMsg = msg.payload.decode()
if len(SCMsg) == 0:
# Nothing in the message!
return
# Commands and parameters are passed in a '|' separated string e.g. Capture|captureprofile1|M42 Orion Nebula
params = SCMsg.split("|")
print(params)
if len(params) == 0:
# No parameters! Need at least one command parameter.
return
# First parameter is the command.
Cmd = params[0]
if Cmd == "Exit":
client.disconnect()
elif Cmd == "Find":
selectFindMode()
elif Cmd == "Capture":
# Check we have enough params - Command, Profile Name, Object ID
if len(params) == 3:
selectCaptureMode(params[1],params[2])
elif Cmd == "Log":
ImageDetails = selectSaveAsSeen()
if ImageDetails == "":
ImageDetails = "FAILED|Logging"
ret = client.publish(ResultTopic, ImageDetails)
client.subscribe(CommandTopic)
client.on_message = on_message
print("SharpCap subscribed to topic - " + CommandTopic)
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
if __name__ == '__main__':
run()