Table of Contents

Bluetooth BTBee

Required knowledge: [HW] Kontrollermoodul, [HW] Kombomoodul, [AVR] USART, [LIB] Jadaliides,
[LIB] Graafiline LCD

Theory

BTBee module

Bluetooth is an industry standard for creating wireless connections between devices. Bluetooth is primarily intended for connecting computers and mobile phones with various peripheral devices, such as headsets, printers, GPS receivers, etc. Bluetooth also enables easy and secure data exchange between two devices. Bluetooth is a two-layer standard that defines the physical layer (radio frequencies) and the protocol layer (how data is transmitted). It is a wireless data exchange standard that uses radio frequencies in the range 2.402 - 2.480 GHz. Many other consumer radio devices operate in the same band. Bluetooth has three power classes, but most devices are class 2, which limits the range to about 10 meters. Line of sight is not required, although walls and other obstacles reduce the range. Up to 8 Bluetooth devices can be connected at the same time without interfering with each other. This is ensured by spread-spectrum frequency hopping, which makes connected devices use slightly different frequencies and change them continuously.

One characteristic of Bluetooth is automatic connectivity. When a Bluetooth device appears within the visibility range of another device, an automatic information exchange takes place to determine whether data exchange should begin or whether one device should take control of the other. This exchange happens automatically without any special commands from the user. If the exchange shows that the devices are authorized to communicate, a personal area network (PAN) or a so-called piconet is created, which is a small master-slave style network, for example a mobile phone and headphones. Once the network is created, its members choose a frequency to avoid interference with other Bluetooth devices in the area that are not part of the network. While in the same network, members ignore other devices even if they try to start a data exchange session.

Security is an important factor in Bluetooth, and different modes ensure secure data exchange and connections. Generally the user can define trusted devices with which connections can be established without asking for a separate password. If a new device tries to connect, the user can allow or deny it. Security measures include authorization and device identification before data exchange begins.

Today there are four main Bluetooth versions and many minor versions. Most modules intended for hobby projects are still based on version 2.0. Version 3 introduced higher data rates (up to 24 Mbit/s) and version 4 introduced lower power consumption, hence the term Bluetooth low energy.

Practice

Bluetooth terminal on a mobile phone

Bluetooth Bee (BTBee) is a Bluetooth Serial Port Profile (Bluetooth SPP) module that fits into the Robotic HomeLab Combo module's XBee socket. As with other XBee modules, communication between the microcontroller and the module takes place over the UART interface. Typically Bluetooth is used to create a link between a computer/mobile phone and the microcontroller, although Bluetooth modules can also be used for communication between two microcontrollers. When the module is connected to the XBee socket and the HomeLab kit is powered, the BTBee enters slave mode by default. After that, commands can be sent to the module over UART. Communication uses AT commands documented in the module user manual. The BTBee module interprets a command as valid when it is terminated with ASCII characters CR (13, '\\r') and LF (10, '\\n'). The module can also be connected to a computer serial interface for testing. For example, in Hyper Terminal, select Properties, Settings tab, ASCII Setup… and enable Send line ends with line feeds and Echo typed characters locally. Before writing a command, press ENTER once, then type only the command without additional characters, and press ENTER again. If the command is invalid, the module responds with “ERR”, otherwise “OK”. When sending commands from the controller to the module, you must still add CR and LF at the end, i.e., the command must be sent in the form “[MESSAGE]\\r\\n”.

Default settings of the BTBee module:

  • Mode: slave
  • Serial interface: 38400 b/s, 1 STOP, No Parity
  • Password: 1234

To communicate with the module, you can use a computer (usually a laptop) or a mobile phone Bluetooth interface. For Android phones you can use the “Bluetooth Terminal/Graphics” application. When a connection is established, the blue LED on the module changes state. After that you can start data exchange with the controller through the BTBee module.

// HomeLab wireless BTBee module example program
// Text received from an external device is displayed on the LCD screen
#include <homelab/pin.h>
#include <homelab/usart.h>
#include <homelab/module/lcd_gfx.h>
 
// Main program
int main(void)
{
	// LCD setup
	lcd_gfx_init();
	lcd_gfx_goto_char_xy(0,0);
	lcd_gfx_write_string("   Homelab Bluetooth    ");
 
	// USART interface setup
	usart_init_async(1,
	                 USART_DATABITS_8,
	                 USART_STOPBITS_ONE,
	                 USART_PARITY_NONE,
	                 USART_BAUDRATE_ASYNC(38400));
	// Infinite loop
	while (1)
	{
		char info[30];
 
		// Is there incoming data?
		if (usart_has_data(1))
		{
			// Read incoming data
			usart_read_string(1,info,20);
			// Display text on LCD
			lcd_gfx_write_string(info);
		}
 
		// Is the button pressed?
		if (button_read(S1))
		{
			// Send text
			usart_send_string(1,"Bluetooth Message\r\n");
			// Wait for button release
			while(button_read(S1));
		}
	}
}
en/examples/communication/bluetooth/btbee.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0