Reader small image

You're reading from  Mastering Arduino

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788830584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Bluetooth Classic

Bluetooth LE, which we saw in Chapter 20, Bluetooth LE, is an excellent choice when we need two devices to communicate wirelessly in short data bursts and where power consumption is a concern. There have been changes with Bluetooth LE with versions 4.2 and 5.0. That make it more appealing for devices that need to transfer large amounts of data or even stream data. However, there is another Bluetooth technology that has been doing this for very successfully for many years, this technology is known as Bluetooth Classic. While the name may imply that this technology is out of date, don't let the name fool you as Bluetooth Classic is used in many Bluetooth devices, and until there are more Bluetooth 5.0 modules available for the Arduino that also support some of the newer features, Bluetooth Classic will remain an excellent choice when we need to transfer large...

Introduction

Bluetooth is a wireless technology standard that is used by two devices to transmit or receive data over short distances using a 2.4GHz wireless connection. While the design goal of Bluetooth LE was to create a low-power wireless protocol, Bluetooth Classic had different design goals. Bluetooth Classic was created by engineers that work at Ericsson Mobile in Lund, Sweden, as a wireless alternative to serial (RS232) cables. This meant that this new protocol would be required to transmit large amounts of data, or even stream data, over short distances.

The Bluetooth Classic specifications are managed by the Bluetooth Special Interest Group (Bluetooth SIG) as part of the Bluetooth core specifications. As we mentioned in Chapter 20, Bluetooth LE, you can find information about both Bluetooth LE and Bluetooth Classic by downloading the specifications form from the Bluetooth...

Bluetooth radio

The range of a Bluetooth radio is dependent on the class. The following chart shows the range of a Bluetooth radio by the class:

Class

Power (mW)

Power (dBm)

Range in meters

1

100

20

~100

2

2.5

4

~10

3

1

0

~1

 

As with any radio technology the area surrounding the radio has a substantial effect on the range of the radio. The range listed in the previous chart is the theoretical maximum range with ideal conditions. The typical range is usually less than this theoretical maximum range.

Where the Bluetooth LE radio operates from 2,402 MHz to 2,480 MHz with each channel being 2 MHz apart, the Bluetooth Classic radio uses 79 channels from 2,402 MHz to 2,480 MHz with each channel being 1 MHz apart. As with Bluetooth LE, the Bluetooth Classic radio uses frequency hopping, where the radio changes channels 1,600 times a second...

Components needed

For these projects you will need the following items:

  • Two Arduino Uno or compatible boards
  • Two HC-05 Bluetooth modules
  • One Joystick breakout module for the Arduino
  • Jumper wires
  • Breadboard

Now let's look at the circuit diagram for our project.

Circuit diagrams

In this chapter, we will be writing code for three projects. In the first project we will be configuring the Bluetooth modules, in the second project we will create an application that will send data, in byte format, from one Bluetooth radio to another and in the last project we will attach a joystick to one of the Arduinos and stream the joystick position to the other Arduino through the Bluetooth connection. The following shows the circuit diagram for our projects:

The two Arduino circuits are completely isolated from each other, therefore they do not need a common ground. Both HC-06 Bluetooth modules are connected to the Arduino in the same way where the VCC pin on the HC-06 Bluetooth module is connected to 5V out, and the GND pin is connected to the ground out on the Arduino. The key pin on the Bluetooth module is connected to the digital 9 pin on the Arduino...

Project 1 – configuring the Bluetooth modules

To communicate with the HC-05 Bluetooth module, we will use the same SoftwareSerial library that we used in Chapter 20, Bluetooth LE. The code that is used to communicate is very similar between the HM-10 (Bluetooth LE) and the HC-05 (Bluetooth Classic). How the two radios transmit and receive the data is a lot different, therefore understanding how the radios work and what they should be used for will define when to use the different technologies.

For this first project, we will be writing an application that will allow us to configure the Bluetooth modules. This code will start off exactly as we did with the Bluetooth LE code by including the SoftwareSerial library and creating an instance of the SoftwareSerial type. The following code shows how to do this:

#include <SoftwareSerial.h>
SoftwareSerial HC05(10, 11);
bool...

Project 2 – serial connection, sending data

For this project, in order to see the data going from one device to the other, you will need two computers. One connected to the master device and one connected to the slave device. If you do not have two computers, it is still worth reading through this section to understand the protocol that we are creating because we will be using the same protocol for the third project as well.

When we are streaming data or sending large amounts of variable length data, we need some way to tell the receiving device where a new message starts and where it ends. Luckily for us, there are built-in ASCII codes that allow for this. The 0x01 SOH (Start Of Heading) and the 0x04 EOT (End Of Transmission) codes can be used to tell the receiving device when a message starts and when it ends.

In this project and the next one, the protocol that we will...

Project 3 – joystick remote control

If you have not connected the joystick breakout module to one of the Arduino's you will need to do it before you start this project. Once the joystick breakout module is connected to the Arduino, we will write the code that will read the position of the joystick and transmit it to the other Arduino via the HC-05 Bluetooth modules; however, before we do this we need to figure out the protocol that we are going to use.

For this example, we will use the same protocol that we used in the previous project, where the message will start off with a 0x01 byte and end with a 0x04 byte and everything in between is the message itself. The message itself will contain two bytes, one that indicates the x position of the joystick and one that indicates the y position. Therefore, a complete transmission will contain a total of four bytes likes this...

Summary

In this chapter, we learned a lot about Bluetooth Classic, starting with a brief introduction on how the radio works and the network topology for Bluetooth Classic connections. We demonstrated how we could configure the Bluetooth HC-05 Bluetooth module as both a slave and master. We also saw how we could configure the Bluetooth modules to connect to each other on startup automatically. Finally, we saw how we could stream data from one device to another using Bluetooth classic.

In Chapter 20, Bluetooth LE, and this chapter we looked at two different Bluetooth technologies, but the question may still be when to use which one. When we have a use case that defines that we want one device to ask another device for information periodically, like a weather station, we generally want to use Bluetooth LE. When we want to stream data from one device to another without waiting for...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Arduino
Published in: Sep 2018Publisher: PacktISBN-13: 9781788830584
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman