Bluetooth: Server with multiple clients (OS X + Arduinos)

 My current goal has been roaming around integration of complex systems, i.e. Internet of Things (IoT). At the beginning of this project I was confronted with many sources constantly stating that it was impossible to connect several gadgets simultaneously with Bluetooth, that that was only possible in theory (which, then again, one could only integrate 7 slaves to a master). Also, I once found that the version of Bluetooth did count, that BT 2.0 could only connect to one device at a time, whilst with BT 4.0 LE we would be able to connect up to 7 devices. What I'm sure, though, is that you truly are limited by the category of the gadget, that is you can only connect one type of some devices (like audio speakers or BT printers), while that constraint doesnt happen with other BT Classes. 

So, as you can see, this project started with heavy research, research that wasn't always backed up by more than suspitions, doubts, rumors, and too few examples online. I started with Java, but I soon found that the BT implementation jars wouldn't work out with Mac OSX, as they only work with 32 bits, and there is no Java 32 bits for Mac OSX, only 64bits. Going to C/C++ didn't prove helpful, because for that I needed to use Apple's Bluetooth framework either way, and that path would be far more painful and frustrating than just using XCode + Objective C, and develop something with the native tools I had. And so I initiated a tiny bit less frustrating Objective-C learning curve. Bottom line: It worked!

 

To start such integration, I chose to create a device layer in my computer, that is able to accept and connect any type of device. I used Mac OSX with XCode, and I developed an application for mac osx that accepts several arduinos (I don't have many many arduinos to test the limit, so I'll leave that to you). I am also trying to connect to a LEGO NXT microcontroller, I did it successfully with this same code, but it is not being covered here (as programming to the NXT is a whole new discussion). Scheme:

 

 

For you to have arduino communicating through Bluetooth, you need to have a BT Shield, and I chose mine, ITEAD BT v2.2:

 

 And I tried this with 2 BT arduinos:

 

 

 I plugged in each arduino to both my USB ports (that's right, more than this, and I would have to buy an USB HUB), and then I used the follow command in the command line:

 

  • screen /dev/tty.usbmodem1411 38400                   (arduino on the first usb port)
  • screen /dev/tty.usbmodem1421 38400                   (arduino on the second usb port)

Off course that the USB port used will be different than mine, but you can figure that out with little debug. These commands create a serial connection with your native OSX, hence no need to use extra software (helpful, right? Told you). The two arduinos already have the code updated at this point, to be able to send text through bluetooth and to receive and display received text through BT. Looks like this:

 

 

The OSX App I develop is as follows:

You have a status text at the beginning, then the box where you can write your own text, and, when you hit Enter, that same text will be broadcasted to all the BT devices connected (I put a limit of 10 connected devices in the code, just stating...). The last box is where you receive any text sent from the devices, any connected device. The square box attached is how we can pair and add any BT device:

 

And then, the following happens:

 

 

The status message gets updated, and now we can write any text to be broadcasted. The first arduino already got it :)  :

 

 

The replies of the arduino are duly shown:

 

Now lets add the other arduino:

 

And now, lets send a message to BOTH the arduinos:

 

All working:P  :

 

Try the app:   BluetoothHub.app.zip (the OSX master app) BT_Slave.ino (code for the arduino slaves).

 

Conclusion:

It is possible to connect devices simultaneously and using mac osx as server, and with different BT versions (OSX has BT 4.0 and arduino shields are BT 2.0). I also didn't have to rely on multi-threading solution, as the COCOA frameworks handle events nicely.  Again, I didn't try the scalability of this, but I am a step closer to integrate all possible devices under the same framework! Next step, integrate different devices with different profiles.

 

Published by fxsf às 15:28