Mqtt to Unity via shiftr.io: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(11 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:




#'''Create yourself a namespace on  
'''Create yourself a namespace on  
[https://shiftr.io/ shiftr.io] '''
[https://shiftr.io/ shiftr.io] '''


#'''Download MQQT library from
'''Download MQTT library from
[[hyperdramatik.net/mediawiki/index.php?title=MQTT_in_Unity]]'''
[[MQTT_in_Unity]]'''


#'''Create a Unity Project (2019 version)'''
'''Create a Unity Project (2019 version)'''


#* go to ASSETS --> Import Custom Pages --> Import the Mqtt Package you just downloaded to your Unity project
* go to ASSETS --> Import Custom Pages --> Import the Mqtt Package you just downloaded to your Unity project
 
[[File:Wiki1.png|thumb|caption]]
#* Go to Project Window --> Assets --> MQTT --> Scripts --> Test → delete TestScript. Create a new Script with the following Code (name it mqttShiftrIO):


* Go to Project Window --> Assets --> MQTT --> Scripts --> Test → delete TestScript. Create a new Script with the following Code (name it mqttShiftrIO):


[[Datei:Wiki1.PNG]]


----
----
Zeile 31: Zeile 30:




public class mqttShiftIO : Mirror.NetworkBehaviour
public class mqttShiftrIO : MonoBehaviour


{
{
private MqttClient client;
private MqttClient client;
// Use this for random Client_ID
//const string glyphs = "abcdefghijklmnopqrstuvwxyz0123456789";
//int charAmount;
public string Shiftrio_ClientID = "MyNameOnShiftIoIwant";
public string Shiftrio_ClientID = "MyNameOnShiftIoIwant";
Zeile 58: Zeile 53:
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
/////////////////////////////////////////////////////////Randomize Client ID ///////////////////////////////
//charAmount = UnityEngine.Random.Range(3, 8);
//for (int i = 0; i < charAmount; i++)
//{
// Shiftrio_ClientID += glyphs[UnityEngine.Random.Range(0, glyphs.Length)];
//}


client.Connect(Shiftrio_ClientID, Shiftrio_Key, Shiftrio_Secret);  
client.Connect(Shiftrio_ClientID, Shiftrio_Key, Shiftrio_Secret);  
Zeile 91: Zeile 81:
Debug.Log("sending...");
Debug.Log("sending...");
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                      //"hello/world" is the topic you send the message "Sending from Unity3D!!!"
Debug.Log("sent");
Debug.Log("sent");
}
}
Zeile 100: Zeile 91:
----
----


Create an empty game object and put the mqttShiftrIO script on it
*Create an empty game object and put the mqttShiftrIO script on it


go to the name space of Shiftr.io →  
*go to the name space of Shiftr.io → click on Token  
click on Token  


Check your Key(Username)
* Check your Key(Username)
Secret(Passwort) and transfer to Unity editor  
Secret(Passwort) and transfer to Unity editor  
--> you can change the message that is shown in the inspector in the mqttShiftrIO


*Topic subscribe → you can change the number of topics you subscribe to


you can change the message that is shown in the inspector in the mqttShiftrIO
*delay (100) --> the amount of data that is send to shiftr.io is still in it's converting rate
 
Topic subscribe → you can change the number of topics you subscribe to
 
delay (100) --> the amount of data that is send to shiftr.io is still in it's converting rate

Aktuelle Version vom 25. Mai 2020, 15:23 Uhr


Create yourself a namespace on shiftr.io

Download MQTT library from MQTT_in_Unity

Create a Unity Project (2019 version)

  • go to ASSETS --> Import Custom Pages --> Import the Mqtt Package you just downloaded to your Unity project
  • Go to Project Window --> Assets --> MQTT --> Scripts --> Test → delete TestScript. Create a new Script with the following Code (name it mqttShiftrIO):

Wiki1.PNG


using UnityEngine;
using System;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;



public class mqttShiftrIO : MonoBehaviour

{
	private MqttClient client;
	
	public string Shiftrio_ClientID = "MyNameOnShiftIoIwant";
	public string Shiftrio_Key = "key findest du im token";
	public string Shiftrio_Secret = "langeNummer findest du unter secret im token";

	
	public string msg = "";

	public List<string> topics_sub = new List<string>();

	
	void Start () {
		// create client instance
		
		client = new MqttClient(IPAddress.Parse("34.76.6.166"), 1883 , false , null ); ////////"34.76.6.166" ---> IP from boker.shiftr.io

		// register to message received ---> EventSystem
		client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
		
		

		client.Connect(Shiftrio_ClientID, Shiftrio_Key, Shiftrio_Secret); 
		
		// subscribe to the topics in the list with QoS 2 
		foreach (String topic in topics_sub) {
			client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
		}
		

	}

	
	void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) 
	{
		string topic = e.Topic;
		msg = System.Text.Encoding.UTF8.GetString(e.Message);
		Debug.Log("Received: " + topic +" --> "+ msg);
		debug.text = msg;
		
	}

	void OnGUI()//creates Button to Press
	{
		if (GUI.Button(new Rect(20, 40, 80, 20), "Send"))
		{
			Debug.Log("sending...");
			client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("Sending from Unity3D!!!"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                      //"hello/world" is the topic you send the message "Sending from Unity3D!!!"
			Debug.Log("sent");
		}
	}
}

  • Create an empty game object and put the mqttShiftrIO script on it
  • go to the name space of Shiftr.io → click on Token
  • Check your Key(Username)

Secret(Passwort) and transfer to Unity editor --> you can change the message that is shown in the inspector in the mqttShiftrIO

  • Topic subscribe → you can change the number of topics you subscribe to
  • delay (100) --> the amount of data that is send to shiftr.io is still in it's converting rate