Communication through Bluetooth

In this tutorial you will learn how to communicate wirelessly to the Dwenguino board using the Dwengo bluetooth module. After this tutorial you will be able to include wireless communication in all your projects.

Acknowledgement: This tutorial is based on the similar tutorial by Dawn Robotics.


Requirements

  1. A Dwenguino board.
  2. Enclosed USB cable.
  3. Dwengo bluetooth module.
  4. A PC with bluetooth communication.

Connecting the bluetooth module

It is very easy to communicate wirelessly with the Dwenguino board. We use a Dwengo bluetooth module that provides serial interface. You start by connecting the bluetooth module on the Dwenguino board. This bluetooth module has four pins: two for power (ground and 5 V) and two pins that are required for the serial communication (one for sending (TXD) and one for receiving (RXD) data). The pins are illustrated in the following picture:

bluetooth pins

Connect this module to the UART connector of the board (see where it is placed), making sure that the RXD pin of the module matches the TXD pin on the UART connector.

Adding numbers via bluetooth serial port

Let's use Dwenguino board to add two numbers sent from your PC. First we declare our variables and objects. Then we prepare our board with the setup function.

  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3. #include <Dwenguino.h>
  4.  
  5. void setup()
  6. {
  7. initDwenguino();
  8. Serial1.begin(9600); // Serial1 oepn communication with the Bluetooth module
  9. // at 9600 baud rate.
  10.  
  11. // Write the messages to show to the user.
  12. dwenguinoLCD.setCursor(0,0);
  13. dwenguinoLCD.print("Input: ");
  14. dwenguinoLCD.setCursor(0,1);
  15. dwenguinoLCD.print("Sum: ");
  16. }

We initialize the communication with the class Serial1 (note the "1" after "Serial", if you omit this number you will be exchanging data over the USB cable, not the bluetooth module). We also initialize the LCD so that we can see the results on the board. We set some titles so that reading the results is easy and clear. In the top row of the LCD we will print the inputs and int he second row we will print their sum.

The next thing is to write the loop function that will run continuously.

  1. void loop()
  2. {
  3. // Listen for data on the serial connection
  4. if ( Serial1.available() > 0 )
  5. {
  6. // Read in 2 real numbers
  7. float a = Serial1.parseFloat();
  8. float b = Serial1.parseFloat();
  9. // Print them to the screen after "Input"
  10. dwenguinoLCD.setCursor(6,0);
  11. dwenguinoLCD.print(a);
  12. dwenguinoLCD.print("+");
  13. dwenguinoLCD.print(b);
  14.  
  15. // Add them together and return the result
  16. float sum = a + b;
  17. Serial1.println( sum );
  18. // Print the result to the LCD after "Sum"
  19. dwenguinoLCD.setCursor(5,1);
  20. dwenguinoLCD.print(sum);
  21. }
  22. }

The if condition checks whether there is data in the serial port using the method available. Whenever there is data in the port we read it as two consecutive real numbers using the method parseFloat. The results are stored in the variables a and b. Immediately after getting the values we print them to the LCD.

The next steps calculate the sum of a and b, send the result over the serial port and print it to the LCD. To send the sum over the serial we use the method println that sends the data as human-readable ASCII text followed by a carriage return character.

And we are done! We just need to send data to the board form our PC. for this last step we use the Serial monitor integrated to the Arduino IDE.

If you want to modify the code you can find the Arduino sketch in the Dwenguino Examples in Arduino IDE.

Sending data from your computer

Pair the bluetooth devices

Linux

If your PC has bluetooth functionalities you will be able to run the following command in the terminal.

hciconfig

It should be something like ‘hci0′

Make sure that the Dwenguino is poewered on and that the Dwengo bluetooth module is correctly connected. Then run the following command to find out the address of the serial bluetooth module.

hcitool scan

After a short delay, this should return the addresses of nearby bluetooth devices. The Dwengo bluetooth module should be called something like linvor.

Before bluetooth devices can communicate, they need to be paired. This can be done by running the following command

sudo bluez-simple-agent hci<n> xx:xx:xx:xx:xx:xx

where is the number of your device (probably hci0) and xx:xx:xx:xx:xx:xx is the address of the Dwengo bluetooth module. After a pause, this program should ask you for the pin code of the Dwengo bluetooth module. By default, the pin for the module is 1234.

At this point, we have 2 bluetooth devices that can communicate with each other, we need to set up a connection and we will be able to send data. Run

rfcomm connect 0  xx:xx:xx:xx:xx:xx

This command will tell you the device associated to the communication channel (e.g. /dev/rfcomm0).

Mac OS X

Turn your bluetooth on and make sure you find and pair the new device through Open Bluetooth Preferences. After that, you should be able to find the bluetooth module.

Windows

Serial Monitor

Once you have paired the devices, make sure that the corresponding port is selected in the Arduino IDE (Tools > Port). For example, in my Linux machine my bluetooth is the device /dev/rfcomm0 in Windows this will be PortX with X the number of your paired Dwenguino.

arduino IDE Ports

Now you are ready to start the serial monitor

arduino IDE serial monitor