How to setup unity to connect esp32 wroom automatically

Viewed 356

I am developing a small program using unity and esp32. my problem is each time esp32 is connected to pc over Bluetooth the port changes. So I have to change the port each time on the script. how can I make the unity scan and connect to esp32 automatically each time it connects to pc over Bluetooth. below is my code .

using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;

public class Movement : MonoBehaviour
{
    public Text outText;
    public Color clr;
    public string SerialOutData;

    public Renderer myRenderer;
    SerialPort sp=new SerialPort("COM8",9600);
    // Start is called before the first frame update
    void Start()
    {
        sp.Open();
        sp.ReadTimeout =1;
        myRenderer= gameObject.GetComponent<Renderer>();
        //sp.DtrEnable= true; 

    }

    // Update is called once per frame
    void Update()
    {
         if(sp.IsOpen){
            
            try
            {
            SerialOutData = sp.ReadLine().ToString();
            
            outText.text=SerialOutData;
            ColorChange(SerialOutData);
            // sp.WriteLine("A");
            }
            catch(System.Exception e)
            {
                //print(e);
            }
        }
    void ColorChange(string Dat)
    {
        if(Dat=="black")
        {
            print("one");
            clr = new Color(0.0f,0.0f,0.0f,255f);
            myRenderer.material.color=clr;
        }
        else if(Dat=="white")
        {
            clr = new Color(255f,255f,255f,255f);
            myRenderer.material.color=clr;
        }
    }
    
    }
}
1 Answers

You can search your device with it's name in connected device. I'm using below code to do that for almost all times. (Maybe you can need to assign name to your ESP32 device):

            try
            {
                ManagementObjectCollection mReturn;
                ManagementObjectSearcher mSearch;
                mSearch = new ManagementObjectSearcher("Select * from Win32_SerialPort");
                mReturn = mSearch.Get();

                foreach (ManagementObject mObj in mReturn)
                {
                    Console.WriteLine("Name  " + mObj["Name"].ToString());  //You can check your device name with this and assign in below if statement
                    if (mObj["Name"].ToString().Contains("YOUR-ESP32-NAME"))
                    {
                        //Console.WriteLine("ID" + mObj["DeviceID"].ToString
                        serialPort= new SerialPort();
                        serialPort.PortName = mObj["DeviceID"].ToString();
                        serialPort.BaudRate = 115200;
                        serialPort.RtsEnable = true;
                        serialPort.DtrEnable = true;
                        serialPort.Parity = Parity.None;
                        serialPort.DataBits = 8;
                        serialPort.Handshake = Handshake.None;                                          
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
Related