Do you want to communicate with your controller board via Bluetooth? You can do this if you have a Bluetooth module compatible with Arduino, or a board with built-in Bluetooth, such as the ZUM BT-328. Communication always takes place between two parties; two makers, two robots, two lettuces… well, communication between lettuces is a bit more difficult… So as I was saying, to communicate wirelessly via Bluetooth, you will need two programs, one that runs on the computer, mobile device or tablet, and another that runs on the brain board that you are using.
In this post, I will explain how to communicate with a computer via Bluetooth. So here´s what we need:
- A program on the controller board that sends/receives data. (We will be using bitbloq)
- A program on the computer or device with Bluetooth that sends/receives data. (We will use the Arduino IDE Serial Monitor)
WRITING VIA BLUETOOTH SERIAL PORT
In this first example, we want to send the phrase ‘Bluetooth was a viking‘ via the Bluetooth port, waiting 2 seconds between each sending.
Part 1: TRANSMITTER – Programming with bitbloq
First we will use this block from the Communication tab in bitloq to start running the Bluetooth serial port. If you´re using a ZUM BT-328 board, you will need to set the baud rate at 19200:
On the other hand, if you´re using the “Mi Primer Kit de Robótica” Bluetooth module, which is an HC-05, you will need to set it at 38400 bauds and connect it as follows:
WATCH OUT! When you hit the button to Load the program on the controller board, the orange cable of pin 0 must be disconnected. However, when you run it, connect it again as shown in the diagram.
The bitbloq blocks for this program are this simple:
As you will have seen, the Bluetooth: Send block is only necessary where we connect the text block for our phrase. Load this program onto your board:
Part 2: RECEIVER – Using the Arduino IDE Serial Monitor
Now for part two, configuring the computer to act as a receiver.
If you´re using the ZUM BT-328 board, remember that the interrupter must be “ON” and the commutators must be in the following position:
Use a battery pack, charger or USB cable to connect the board to a power source.
>> Bluetooth configuration on Windows 7:
Using the Start button, search for the Bluetooth setup wizard to guide you through setting up the connection.
The Bluetooth setup wizard may vary from computer to computer, must the steps to follow will be more or less the same:
Once configured and connected, it will ask you to enter the PIN, which will be 1234 by default.
- >> Bluetooth configuration on Linux (Ubuntu):
- Open a Terminal (Alt + F2) and type in gnome-terminal.
- Search for the enable new Bluetooth devices settings by entering the following command.
$ sudo hcitool scan
- Connect your device to the ‘/dev/rfcomm0′ port by typing in the following command and using the address obtained from the previous step:
$ sudo rfcomm bind /dev/rfcomm0 98:D3:31:B2:DB:09
>> Bluetooth configuration on MacOS:
- Select Bluetooth under System preferences.
- Search for your board and click on Connect. If it fails to connect and an error message appears, you will need to pair the Bluetooth device with the computer. To do this click on Options and enter the code “1234” and then click on pair again.
Now we´re connected!! And we will know the number of the port created according to the operating system, for example: ‘COM46′, ‘/dev/rfcomm0′, ‘/dev/tty.bqZUM_BT320-SPPDev’…
Open Arduino IDE, go to Tools and select the board [ArduinoUNO or Arduino BT w/ATmega328] and the port [e.g. COM46
We can now open the Serial Monitor via Tools/Serial Monitor or by clicking on this icon:
Remember to select the correct baud rate for your board, at 19200 or 38400. You will see that the phrase appears every two seconds:
READING THE BLUETOOTH SERIAL PORT
Receiving via Bluetooth:
We will use an example of receiving data through the port that we have opened. This block will verify whether something has been received via Bluetooth:
If we have received something through the port created, we will save it in a local variable called “data”, which we will compare to see if it´s the letter A (upper case!) and if it is, we will switch on the LED on the board, which means on digital pin 13 (so you don´t need to connect the LED module). If any other data is received, we will switch off that LED.
“- But… Have you compared the data with 65?! – ” Of course, dear robonaut, because bitbloq doesn´t have a block for comparing char text variables at the moment, (it´s only possible with strings). Don´t worry if this all sounds like Greek to you, all you need to know in this example is that we are using numbers to identify letters.
Letter A corresponds to decimal number 65 in ASCII code (see table at end of post).
Load this program on your board and open the new Serial Monitor. Type in an upper case A and hit send… Do you see how the small LED lights up? What happens if you send something else?
Now that you´re an expert in bitbloq-Bluetooth communication, you might know that the RoboPad application for Android sends the characters: S,U,D,R,L,C,M… (stop, up, down, right, left, claw, manual) on pressing the control buttons of the PrintBots. I have provided the ASCII table below so you can compare the letters and characters that are sent:
Did you notice that the numbers 0, 1, 2, 3… were also sent as characters and they correspond decimally in ASCII code? The things you can learn in a day… You´ll be reading Matrix code before you know it!