Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using uPLibrary.Networking.M2Mqtt;
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using System.IO;
- using UnityEditor;
- public class box : MonoBehaviour
- {
- public int MQTTexist = 1;
- public int rcvdID;
- public float q1;
- public float q2;
- public float q3;
- public float q4;
- public float acc1;
- public float acc2;
- public float acc3;
- public string brokerHostname = "192.168.43.1"; // !!!!!!!!!!!!!!!!
- public int brokerPort = 1883;
- public string userName = null;
- public string password = null;
- public string topic = "#";
- public MqttClient4Unity client;
- void Start() // Use this for initialization
- {
- if (MQTTexist == 0)
- {
- return;
- }
- Connect(); //connect to mqtt broker
- client.Subscribe("sensors/q"); // subscribe to mqtt topics !!!!!!!!!!!!!
- }
- void Update() // Update is called once per frame
- {
- if (MQTTexist == 0)
- {
- return;
- }
- while (client.Count() > 0)
- {
- string s = client.Receive();
- Deconstruct(s);
- if (rcvdID == 56843) // ID of ball sensor 2 !!!!!!!!!!!!!!!!!!
- {
- transform.rotation = new Quaternion(q3, -q4, -q2, q1);
- }
- }
- }
- void Deconstruct(string input)
- {
- char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
- string[] output = input.Split(delimiterchars);
- // locations: yaw -> 7, pitch -> 13, roll -> 19
- rcvdID = Convert.ToUInt16(output[6]);
- q1 = (float)Convert.ToDouble(output[12]);
- q2 = (float)Convert.ToDouble(output[18]);
- q3 = (float)Convert.ToDouble(output[24]);
- q4 = (float)Convert.ToDouble(output[30]);
- acc1 = (float)Convert.ToDouble(output[36]);
- acc2 = (float)Convert.ToDouble(output[42]);
- acc3 = (float)Convert.ToDouble(output[48]);
- }
- public void Connect()
- {
- try
- {
- client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
- string clientId = "UnityClient_pc" + UnityEngine.Random.Range(1, 10000); /*Guid.NewGuid().ToString();*/
- client.Connect(clientId, userName, password);
- }
- catch (Exception ex)
- {
- Debug.Log("failed to connect" + ex);
- }
- }
- public void Publish(string _topic, string msg)
- {
- client.Publish(
- _topic, Encoding.UTF8.GetBytes(msg),
- MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
- }
- //ezzel baszakszunk, a fenti a ,működő kód// public class HandleTextFile
- {
- //[MenuItem("Tools/Write file")]
- static void WriteString()
- {
- string path = "Assets/Resources/test.txt";
- //Write some text to the test.txt file
- StreamWriter writer = new StreamWriter(path, true);
- writer.WriteLine("Test");
- writer.Close();
- /* //Re-import the file to update the reference in the editor
- AssetDatabase.ImportAsset(path);
- TextAsset asset = Resources.Load("test");
- //Print the text from the file
- Debug.Log(asset.text);*/
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement