import asyncio import argparse import math import secrets import paho.mqtt.client as mqtt from pythonosc.dispatcher import Dispatcher from pythonosc import osc_server import secrets import paho.mqtt.client as mqtt # constants MY_OSC_SERVER = "127.0.0.1" MY_OSC_PORT = 5001 MY_MQTT_SERVER = "mqtt.klank.school" MY_MQTT_PORT = 7000 MY_OSC_MQTT_CHANNEL = "OSC" mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) def publish(topic, msg): result = mqttc.publish(topic, msg) status = result[0] if not status == 0: print(f"Failed to send message to topic {topic}") def print_anything(address, args): print(address, args) publish(MY_OSC_MQTT_CHANNEL + address, args) def on_connect(client, userdata, flags, reason_code, properties): publish(MY_OSC_MQTT_CHANNEL + "/activate", "Hai! OSC2MQTT is here!") def on_message(client, userdata, msg): print(msg.topic + " " + str(msg.payload)) async def main(): global loop loop = asyncio.get_running_loop() # Store the main event loop dispatcher = Dispatcher() dispatcher.map("/*", print_anything) server = osc_server.ThreadingOSCUDPServer( (MY_OSC_SERVER, MY_OSC_PORT), dispatcher) print("Serving on {}".format(server.server_address)) mqttc.on_connect = on_connect mqttc.on_message = on_message mqttc.username_pw_set(secrets.mqtt_username, secrets.mqtt_password) print("connect to klank.school") mqttc.connect(MY_MQTT_SERVER, MY_MQTT_PORT, 60) # Start the MQTT loop in a background thread mqttc.loop_start() server.serve_forever() # asyncio.run_coroutine_threadsafe(do_a_gesture(data.topics[0].removeprefix('main')), loop) # # Keep the asyncio event loop running await asyncio.Event().wait() # Run the main function asyncio.run(main())