Advertisement
Guest User

akármi

a guest
Dec 5th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using uPLibrary.Networking.M2Mqtt;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using uPLibrary.Networking.M2Mqtt.Messages;
  10. using System.IO;
  11. using UnityEditor;
  12.  
  13. public class box : MonoBehaviour
  14. {
  15.  
  16. public int MQTTexist = 1;
  17. public int rcvdID;
  18.  
  19. public float q1;
  20. public float q2;
  21. public float q3;
  22. public float q4;
  23.  
  24. public float acc1;
  25. public float acc2;
  26. public float acc3;
  27.  
  28. public string brokerHostname = "192.168.43.1"; // !!!!!!!!!!!!!!!!
  29. public int brokerPort = 1883;
  30. public string userName = null;
  31. public string password = null;
  32. public string topic = "#";
  33.  
  34. public MqttClient4Unity client;
  35.  
  36. void Start() // Use this for initialization
  37. {
  38. if (MQTTexist == 0)
  39. {
  40. return;
  41. }
  42. Connect(); //connect to mqtt broker
  43. client.Subscribe("sensors/q"); // subscribe to mqtt topics !!!!!!!!!!!!!
  44. }
  45.  
  46. void Update() // Update is called once per frame
  47. {
  48. if (MQTTexist == 0)
  49. {
  50. return;
  51. }
  52. while (client.Count() > 0)
  53. {
  54. string s = client.Receive();
  55.  
  56. Deconstruct(s);
  57.  
  58. if (rcvdID == 56843) // ID of ball sensor 2 !!!!!!!!!!!!!!!!!!
  59. {
  60. transform.rotation = new Quaternion(q3, -q4, -q2, q1);
  61. }
  62. }
  63. }
  64.  
  65. void Deconstruct(string input)
  66. {
  67. char[] delimiterchars = { '"', ',', ':', ' ', '{', '}', '\n' };
  68. string[] output = input.Split(delimiterchars);
  69. // locations: yaw -> 7, pitch -> 13, roll -> 19
  70.  
  71. rcvdID = Convert.ToUInt16(output[6]);
  72.  
  73. q1 = (float)Convert.ToDouble(output[12]);
  74. q2 = (float)Convert.ToDouble(output[18]);
  75. q3 = (float)Convert.ToDouble(output[24]);
  76. q4 = (float)Convert.ToDouble(output[30]);
  77.  
  78. acc1 = (float)Convert.ToDouble(output[36]);
  79. acc2 = (float)Convert.ToDouble(output[42]);
  80. acc3 = (float)Convert.ToDouble(output[48]);
  81. }
  82.  
  83. public void Connect()
  84. {
  85. try
  86. {
  87. client = new MqttClient4Unity(brokerHostname, brokerPort, false, null);
  88. string clientId = "UnityClient_pc" + UnityEngine.Random.Range(1, 10000); /*Guid.NewGuid().ToString();*/
  89. client.Connect(clientId, userName, password);
  90. }
  91. catch (Exception ex)
  92. {
  93. Debug.Log("failed to connect" + ex);
  94. }
  95. }
  96.  
  97. public void Publish(string _topic, string msg)
  98. {
  99. client.Publish(
  100. _topic, Encoding.UTF8.GetBytes(msg),
  101. MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE, false);
  102. }
  103.  
  104.  
  105.  
  106. //ezzel baszakszunk, a fenti a ,működő kód// public class HandleTextFile
  107. {
  108. //[MenuItem("Tools/Write file")]
  109. static void WriteString()
  110. {
  111. string path = "Assets/Resources/test.txt";
  112.  
  113. //Write some text to the test.txt file
  114. StreamWriter writer = new StreamWriter(path, true);
  115. writer.WriteLine("Test");
  116. writer.Close();
  117.  
  118. /* //Re-import the file to update the reference in the editor
  119. AssetDatabase.ImportAsset(path);
  120. TextAsset asset = Resources.Load("test");
  121.  
  122. //Print the text from the file
  123. Debug.Log(asset.text);*/
  124. }
  125.  
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement