#!/usr/bin/env python3 import paho.mqtt.client as mqtt import threading import json import subprocess from time import sleep from os import path p = None # 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() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("video/cam2/preview") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global p print(msg.topic+" "+str(msg.payload)) if msg.topic == "video/cam2/preview": try: run = int(msg.payload) except Exception as e: print(e) return if run == 1 and p is None: print("Starting") p = subprocess.Popen(["/home/curso/code/snowmix/camera1/play-cam2.sh"]) if (p is not None and run == 0): print("Stopping") p.terminate() sleep(0.1) if p is not None: p.kill() sleep(0.1) p = None client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect("192.168.2.123", 1883, 60) t = threading.Thread(target=client.loop_forever) t.start() t.join()