Search Unity

Connecting bluetooth and Arduino

Discussion in 'Formats & External Tools' started by unity_ZKrwLUpxRFC6tg, Dec 11, 2017.

  1. unity_ZKrwLUpxRFC6tg

    unity_ZKrwLUpxRFC6tg

    Joined:
    Dec 11, 2017
    Posts:
    1
    Hello everyone,

    I realise a project in which I need to send a value from Unity and receive it with a bluetooth module connecting to an Arduino.
    In this case, when I click on a cube of color, the Unity sript send a char value ("r", "g" or "y") and the corresponding led is lighting.

    It works when I just use the COM4 Port without the bluetooth module but when I connect it, it doesn't work and I have the issue below:
    "
    TimeoutException: The operation has timed-out.
    System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
    System.IO.Ports.SerialPort.read_byte ()
    System.IO.Ports.SerialPort.ReadByte ()
    (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte ()
    Sending.Update () (at Assets/MyScripts/Sending.cs:25)
    "
    Here are my Arduino Code:
    int gLed = 10;
    int yLed = 11;
    int rLed = 12;
    char myCol[20];

    #include <SoftwareSerial.h>

    int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
    int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3

    SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

    void setup() {

    Serial.begin (9600);
    pinMode(gLed, OUTPUT);
    pinMode(yLed, OUTPUT);
    pinMode(rLed, OUTPUT);

    digitalWrite(gLed, LOW);
    digitalWrite(yLed, LOW);
    digitalWrite(rLed, LOW);

    bluetooth.begin(9600); // Start bluetooth serial at 9600

    }

    void loop() {
    int lf = 10;
    if (bluetooth.available()) // If the bluetooth sent any characters
    {
    // Send any characters the bluetooth prints to the serial monitor

    //Serial.println((char)bluetooth.read());
    myCol[0] = bluetooth.read();

    Serial.readBytesUntil(lf, myCol, 1);

    switch(myCol[0]){
    case 'r':
    digitalWrite(rLed, HIGH);
    digitalWrite(yLed, LOW);
    digitalWrite(gLed, LOW);
    break;

    case 'y':
    digitalWrite(rLed, LOW);
    digitalWrite(yLed, HIGH);
    digitalWrite(gLed, LOW);
    break;

    case 'g':
    digitalWrite(rLed, LOW);
    digitalWrite(yLed, LOW);
    digitalWrite(gLed, HIGH);
    break;
    }
    if (Serial.available()) // If stuff was typed in the serial monitor
    {
    // Send any characters the Serial monitor prints to the bluetooth
    //String myStr = (String)Serial.read();
    //char myStr1[] = "hello this is testing!";

    // uint8_t payload[myStr.length() + 1];
    // myStr.getBytes(payload, myStr.length()+1);

    int bytes=Serial.available();
    //Serial.readBytes(buffer, startPosition, bytes);

    //bluetooth.print((char)Serial.read());


    }
    }
    }

    and here my Unity script that send the value in the Port:

    using UnityEngine;
    using System.Collections;
    using System.IO.Ports;
    using System.Threading;

    public class Sending : MonoBehaviour {

    public static SerialPort sp = new SerialPort("COM4", 9600);
    public string message2;
    //float timePassed = 0.0f;
    // Use this for initialization
    void Start () {
    sp.Open(); // opens the connection
    sp.ReadTimeout = 16; // sets the timeout value before reporting error
    print("Port Opened!");

    }

    // Update is called once per frame
    void Update () {
    //timePassed+=Time.deltaTime;
    //if(timePassed>=0.2f){

    //print("BytesToRead" +sp.BytesToRead);
    message2 = sp.ReadLine();
    print(message2);
    // timePassed = 0.0f;
    //}
    }

    void OnApplicationQuit()
    {
    sp.Close();
    }

    public static void sendYellow(){
    sp.Write("y");
    }

    public static void sendGreen(){
    sp.Write("g");
    //sp.Write("\n");
    }

    public static void sendRed(){
    sp.Write("r");
    }
    }

    Has anyone an idea? Thanks in advancee
     
  2. verybinary

    verybinary

    Joined:
    Sep 23, 2015
    Posts:
    373
    Bluetooth library already imported into arduino ide?
    I'd build it and troubleshoot, but I don't have Bluetooth, just some rf.
    I have an also that has probably been taken care of, but is your pc and arduino paired?
     
  3. ichitaka

    ichitaka

    Joined:
    May 8, 2017
    Posts:
    2
    Hey, currently i'm working on some Arduino Bluetooth stuff myself, so i might be able to help you get to the next step. So you are getting a TimeoutException. As you can see, you are defining a ReadTimeout when creating your SerialPort. This means, when you call ReadLine(), your code will try to receive a message for exactly 16ms. This means, that your device is NOT receiving anything in the timeframe. You can catch the exception, so you won't see it in your console. Maybe then the actual messages becomes visible?
     
    unity_ZKrwLUpxRFC6tg likes this.
  4. TechDevTom

    TechDevTom

    Joined:
    Oct 21, 2011
    Posts:
    33
    Did you get anywhere with this? It would be interesting to see a video of the results! Also, thanks for sharing your code, it's helping me out with my own R&D.

    Would any of you be willing to chat arduino and Bluetooth with me so I can get a better understanding of it all? I'm working on a basic mobile to arduino app at the moment and need all the help I can get!
     
  5. ichitaka

    ichitaka

    Joined:
    May 8, 2017
    Posts:
    2
    My Android -> Arduino project just got into testing today. Sadly the only solution to get bluetooth working for me was to buy an asset from the asset store.
     
  6. spkmark

    spkmark

    Joined:
    Dec 8, 2016
    Posts:
    6
    I think the problem with this code is the unity side. Serial port is defined as com4... this will only work through com4 if com4 is a bluetooth serial port. I'm currently trying to do the same on android. trying to pass an int or string from unity android to arduino hc-06. not successful yet but ino I'm close.
     
  7. spkmark

    spkmark

    Joined:
    Dec 8, 2016
    Posts:
    6
    hello. I'm currently working on a project myself... do your have any handy insights on passing an int or string from unity to arduino? ty.
     
  8. nx-sm

    nx-sm

    Joined:
    Jul 4, 2015
    Posts:
    10
    Hi, I am trying to create my own custom gamepad using Arduino Nano, Bluetooth HC 05 and Unity. The connection works perfectly with USB and Bluetooth to PC as serial port is identified. I can't manage to open the stream to the android device via Bluetooth as it looks like there is no COM available. Did you manage to do it? Is it even possible without any addons? Cheers.
     
  9. Tony686

    Tony686

    Joined:
    Mar 20, 2015
    Posts:
    7
    Hii just use this asset from the asset store,
    It made connecting arduino to bluetooth easy task!!
    https://assetstore.unity.com/packages/tools/input-management/arduino-android-bluetooth-plugin-98960