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.
- A Dwenguino board.
- Enclosed USB cable.
- Dwengo bluetooth module.
- 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:
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.
- #include <LiquidCrystal.h>
- #include <Wire.h>
- #include <Dwenguino.h>
- void setup()
- Serial1.begin(9600); // Serial1 oepn communication with the Bluetooth module
- // at 9600 baud rate.
- // Write the messages to show to the user.
- dwenguinoLCD.print("Input: ");
- dwenguinoLCD.print("Sum: ");
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.
- void loop()
- // Listen for data on the serial connection
- if ( Serial1.available() > 0 )
- // Read in 2 real numbers
- float a = Serial1.parseFloat();
- float b = Serial1.parseFloat();
- // Print them to the screen after "Input"
- // Add them together and return the result
- float sum = a + b;
- Serial1.println( sum );
- // Print the result to the LCD after "Sum"
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
If your PC has bluetooth functionalities you will be able to run the following command in the terminal.
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.
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
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.
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.
Now you are ready to start the serial monitor