Step 2: Communicate between Unity and Python using MQTT
In Step 1 of this series run the code for human body pose estimation and find the key points of the body. In this step, we send this key points on the internet. Firstly, we must create the broker(server) of the MQTT system. I am using Node.js for MQTT server but you can use any other languages.
Server.js (Broker) code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
var mosca = require('mosca') var settings = { port: 1883 }; //here we start mosca var server = new mosca.Server(settings); server.on('ready', setup); // fired when the mqtt server is ready function setup() { console.log('Mosca server is up and running') } // fired whena client is connected server.on('clientConnected', function(client) { console.log('client connected', client.id); }); // fired when a message is received server.on('published', function(packet, client) { console.log('Published : ', packet.payload); }); // fired when a client subscribes to a topic server.on('subscribed', function(topic, client) { console.log('subscribed : ', topic); }); // fired when a client subscribes to a topic server.on('unsubscribed', function(topic, client) { console.log('unsubscribed : ', topic); }); // fired when a client is disconnecting server.on('clientDisconnecting', function(client) { console.log('clientDisconnecting : ', client.id); }); // fired when a client is disconnected server.on('clientDisconnected', function(client) { console.log('clientDisconnected : ', client.id); }); |
Client sides of this communication are python and C# therefore we must prepare two code for sending and receiving the messages. On the python side I use this code for sending the messages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import time import paho.mqtt.client as paho broker="IPadress" #define callback def on_message(client, userdata, message): time.sleep(1) print("received message =",str(message.payload.decode("utf-8"))) client= paho.Client("client-001") #create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on") ######Bind function to callback client.on_message=on_message ##### print("connecting to broker ",broker) client.connect(broker)#connect client.loop_start() #start loop to process received messages print("subscribing ") client.subscribe("house/bulb1")#subscribe time.sleep(2) print("publishing ") client.publish("house/bulb1","on")#publish time.sleep(4) client.disconnect() #disconnect client.loop_stop() #stop loopw |
In the C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using UnityEngine; using System.Collections; using System.Net; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; using uPLibrary.Networking.M2Mqtt.Utility; using uPLibrary.Networking.M2Mqtt.Exceptions; using System; public class mqttTest : MonoBehaviour { private MqttClient client; // Use this for initialization void Start () { // create client instance client = new MqttClient(IPAddress.Parse("143.185.118.233"),8080 , false , null ); // register to message received client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); // subscribe to the topic "/home/temperature" with QoS 2 client.Subscribe(new string[] { "hello/world" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); } void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message) ); } void OnGUI(){ if ( GUI.Button (new Rect (20,40,80,20), "Level 1")) { Debug.Log("sending..."); client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true); Debug.Log("sent"); } } // Update is called once per frame void Update () { } } |
I will use these example codes for the making connection. Human Body Estimation Code is pretty long so that I share this example codes.