Contents
- 1 Design Tips of WiFi for Pi
- 2 1. How to transfer data from Raspberry Pi to Unity
- 3 1.1 How to set up data to Unity in Raspberry Pi
- 4 1.2 How to get data from Raspberry Pi in Unity
- 5 2. How to transfer data from Unity to Raspberry Pi
- 6 2.1 How to set up data to Raspberry Pi in Unity
- 7 2.2 How to get data from Unity in Raspberry Pi
- 8 3. How to add new Raspberry Pis
Design Tips of WiFi for Pi
Asset ‘WiFi for Pi’ is an adapter interface tool for transfer from Raspberry Pi to Unity through Windows PC via WiFi using MQTT communication. By connecting the sensor device (gyro, accelerometer, GPS, etc.) with Raspberry Pi you can input various sense data to Unity. You can also output various data from Unity by connecting the display device (LED, LCD panel, etc.) with Raspberry Pi. This design tip explains how to modify for transfer data between Unity and Raspberry Pi or add new Raspberry Pis, based on the purchased PiWiFiSampleCode and the purchased Raspberry Pi code.
1. How to transfer data from Raspberry Pi to Unity
1.1 How to set up data to Unity in Raspberry Pi
1.2 How to get data from Raspberry Pi in Unity
2. How to transfer data from Unity to Raspberry Pi
2.1 How to set up data to Raspberry Pi in Unity
2.2 How to get data from Unity in Raspberry Pi
3. How to add new Raspberry Pi
1. How to transfer data from Raspberry Pi to Unity
1.1 How to set up data to Unity in Raspberry Pi
The Raspberry Pi code ‘main.py’ generates 3D Object‘vase’ move data ‘x, y, z’ and convert it to strings using the ‘format’ function. The Raspberry Pi code needs to change the strings to byte array data type using ‘str(formatted_msg).encode’ function for sending data to Unity when Raspberry Pi connects some sensors device.
… def generate_data(client): global pls_flg, value, Pi_ID, pi_id init_x = "0" init_y = "-1" init_z = "-7" formatted_msg = "" if pls_flg: value += 1 else: value -= 1 if Pi_ID == 1: x = 0.05 * value if (x >= 2.0) and pls_flg: pls_flg = False elif (x <= -2.0) and (not pls_flg): pls_flg = True formatted_msg = "{0:.2f},{1},{2},".format(x, init_y, init_z) elif Pi_ID == 2: y = 0.05 * value if (y >= 1.0) and pls_flg: pls_flg = False elif (y <= -2.0) and (not pls_flg): pls_flg = True formatted_msg = "{0},{1:.2f},{2},".format(init_x, y, init_z) print(formatted_msg) if pi_id == Pi_ID: msg = str(formatted_msg).encode() pi_num = "{0}".format(Pi_ID) client.publish(outTopic_base + pi_num, payload=msg, qos=0, retain=False) print(f"send {msg} to " + outTopic_base + pi_num) …
The Raspberry Pi code ‘main.py’ calls the ‘encode()’ function in the following format for sending data to Unity. The ‘encode’ function encodes the string, using the specified encoding.
string.encode(encoding=encoding, errors=errors)
1.2 How to get data from Raspberry Pi in Unity
Unity script example ‘Scripts/PiWiFiSampleCode.cs’ receives the move data from Raspberry Pi as byte array data ‘readdata’ of return value of ‘m_PiWiFiLib.UpdateRead’ function. The byte array data ‘readdata’ encodes string code using ‘System.Text.Encoding.UTF8.GetString’ function and, using ‘text.Split’ function and ‘float.Parse’ function, the string code is converted to float position data.
… // Update is called once per frame void Update() { byte[] readdata = new byte[] { }; //UnityEngine.Debug.Log("Update"); if (!m_PiWiFiLib.UpdateRead(ref readdata)) { return; } UnityEngine.Debug.Log(" readdata: " + readdata[0] + " " + readdata[1] + " " + readdata[2]); UnityEngine.Debug.Log(" Length: " + readdata.Length); string text = System.Text.Encoding.UTF8.GetString(readdata); UnityEngine.Debug.Log(" String: " + text); string[] arr = text.Split(','); float[] acceldata = new float[3]; acceldata[0] = float.Parse(arr[0]); acceldata[1] = float.Parse(arr[1]); acceldata[2] = float.Parse(arr[2]); accelx = acceldata[0]; accely = acceldata[1]; accelz = acceldata[2]; Transform myTransform = this.transform; Vector3 pos = myTransform.position; pos.x = accelx; pos.y = accely; pos.z = accelz; myTransform.position = pos; } …
Unity script example ‘Scripts/PiWiFiSampleCode.cs’ calls the ‘UpdateRead’ function in the following format for reading data from Raspberry Pi
bool UpdateRead(ref byte[] readdata)
2. How to transfer data from Unity to Raspberry Pi
2.1 How to set up data to Raspberry Pi in Unity
Unity script example ‘Scripts/PiWiFiSampleCode.cs’ call back ‘ButtonClick’ function at button ‘On’ clicked. ‘ButtonClick’ function inputs data from ’inputFieldno0’ / ‘inputFieldno1’ / ’Dropdown’, then calls ‘m_PiWiFiLib.Command’ function with the parameter of ‘writedata’ byte array.
… public void ButtonClick() { UnityEngine.Debug.Log("ButtonClick: "); byte[] writedata = new byte[3] { (byte)(dropdown.value +1) ,byte.Parse(inputFieldno0.text), byte.Parse(inputFieldno1.text) }; UnityEngine.Debug.Log(writedata[0] + " " + writedata[1] + " " + writedata[2]); m_PiWiFiLib.Command(writedata); } …
Unity script example ‘Scripts/PiWiFiSampleCode.cs’ calls the ‘Command’ function in the following format for sending byte array data to Raspberry Pi
void Command(byte[] writedata);
2.2 How to get data from Unity in Raspberry Pi
The Raspberry Pi code ‘main.py’calls back ‘on_message_subscriber’ function at received data from Unity. The ‘on_message_subscriber’ function receives Inputfield ’No0’ / Inputfield ‘No1’ / ’Dropdown’ of Unity as 3 bytes of data using the ‘on_message_subscriber’ function parameter ’msg’.
… def on_message_subscriber(client, userdata, msg): global pi_id pi_id = msg.payload[0] row = msg.payload[1] col = msg.payload[2] print("************** receive data! ******************") print(pi_id) print(row) print(col) …
3. How to add new Raspberry Pis
This asset can connect plural Raspberry Pis to Unity. Raspberry Pi code and ‘Scripts/PiWiFiSampleCode’ of this asset are designed to work at two Raspberry Pi. Changing between Raspberry Pi #1 and #2 uses UI ‘Dropdown’ on the Unity screen. When selecting ‘1’, the Unity 3D object ‘vase’ moves horizontally. When select ’2’, the ‘vase’ moves vertically.
Just add the Topic Name for the new Raspberry Pi to the following list, the Raspberry Pi connects to Unity using MQTT communication.
To add the Topic Name, Change the following Topic code definition ‘Pi_ID’ of Raspberry Pi code.
… const char* mqtt_server = "192.168.10.105"; const byte Pi_ID = 1; const char* outTopic_base = "Topic/Pi/"; const char* inTopic = "Topic/Unity"; …