Quick Start of WiFi for Pi

Asset ‘WiFi for Pi’ is an interface asset between a single Windows PC and plural Raspberry Pis via WiFi using MQTT communication.
A Unity 3D object ‘ vase’ in the Demo scene moves using the generated data in Raspberry Pi. TheRaspberry Pi generates 3D Object position data ‘x, y, z’. The Raspberry Pi sends that data from the Raspberry Pi to Unity through Windows PC via WiFi using MQTT communication.
When clicking a Unity 3D object ‘Button’ in the Demo scene, Unity sends the numerical data obtained from the Unity 3D object ‘Inputfield’ and Dropdown to the Raspberry Pi. Unity works at Windows PC and sends the numerical data to the Raspberry Pi using MQTT communication.
The following shows this asset function structure. Workflow is as follows.

  1. Unity with this asset on the Windows PC inputs moving data as Subscriber from Unity topic.
  2. This asset on the Raspberry Pi outputs moving data as Publisher to Unity topic.
  3. Unity with this asset on the Windows PC outputs command data as Publisher to Raspberry Pi topic.
  4. This asset on the Raspberry Pi inputs command data as Subscriber from Raspberry Pi topic.
  5. Moving data is for Horizontal or Vertical movement depending on Raspberry Pi No.
  6. Command data is for Raspberry Pi No and two input field data.

The following shows the capture screen when the Demo scene runs.


This Quick Start explains how to display the Demo scene on Unity using this asset. This Quick Start also includes some steps for downloading and setting up a package in Unity Project, and writing the Raspberry Pi code.

Step-1 Download and set up a package in Unity Project
Step-2 Copy Raspberry Pi code to the Raspberry Pi using SFTP
Step-3 Run Demo scene

Step-1 Download and set up a package in Unity Project

  1. Download this asset from the Asset Store, and click on the Import button.
  2. The package is imported under the Assets folder in your Unity project.
  3. Click the Project tab, choose ‘Assets’ > ‘PiWiFiInterface’ > ‘Scenes’ folder of the left side menu, double click on the Demo scene ‘PiWiFiSampleScene’ in the Project window.
  4. Set Windows PC IP address into label ’pc_ip_addr’ of ‘Scripts/PiWiFiSampleCode’ as follows.
  5.    …
    
        public string PC_IP_Addr;
    
        private string pc_ip_addr = "192.168.10.105";
        private PiWiFiLib m_PiWiFiLib;
    
       …
    

Step-2 Copy Raspberry Pi code to the Raspberry Pi using SFTP

  1. Connect Raspberry Pi 4 (include Raspberry Pi Zero W) or higher by USB cable to Windows PC.
  2. [Raspberry Pi Zero W]

    [Raspberry Pi 4]

  3. Transfer folder ‘piwifi’ to Raspberry Pi using SFTP (SSH File Transfer Protocol). We use the free FTP solution ‘FileZilla Client’ in our case as follows.
  4. Obtain Raspberry Pi code ‘main.py’ of the folder ‘/Assets / PiWiFiInterface/ piwifi’and modify the code for adapting your network environment as follows.
    • ‘mqtt_server’: Windows PC IP address
       …
    
    
    mqtt_server = "192.168.10.105"
    Pi_ID = 1
    outTopic_base = "Topic/Pi/"
    
       …
    

Step-3 Run Demo scene

  1. Connect Windows PC to Raspberry Pi using SSH (Secure Shell) protocol. In Raspberry Pi, install MQTT library ‘paho-mqtt’ using the following command from Windows PC.
  2. $ pip install paho-mqtt
    
  3. Run the Raspberry Pi code ‘main.py’ using the following command from Windows PC.
  4. $ ls
    $ cd piwifi
    $ ls
    $ python3 main.py
    

    We use the terminal emulator ‘TeraTerm’ in our case as follows.

  5. Click the ‘Play’ button on Unity. The Unity 3D object ‘vase’ moves in sync with the received data from the Raspberry Pi. The ‘Accelx, Accely, Accelz’ in the ‘Scripts/PiWiFiSampleCode’ of the ‘Inspector’ tab displays the received data ‘x, y, z’ from the Raspberry Pi as follows.
  6. The ’console’ tab displays the game status as follows.
  7. The terminal emulator ‘Tera Term’ in Windows PC displays the data ‘x, y, z’ from Raspberry Pi as follows.
  8. Select Dropdown and set No0 / No1 into Inputfield ’No0’ / Inputfield ‘No1’ in the game screen, then click the Button ‘On’ to send to the Raspberry Pi. Show Dropdown / Inputfield ’No0’ / Inputfield ‘No1’ as follows.
  9. In the case of the PiWiFiSampleCode, the Dropdown displays ‘1’ or ’2’ for selecting Raspberry Pi No.1 or No.2. When selecting ‘1’, the Unity 3D object ‘ vase’ moves horizontally. When select ’2’, the ‘ vase’ moves vertically.
  10. The ’console’ tab displays the game status at clicking Button ‘On’ as follows.
  11. The terminal emulator ‘Tera Term’in Windows PC displays the No0/No1 data from Windows PC as follows.