====== Bluetooth ====== //The necessary knowledge: [HW] [[en:hardware:homelab:controller]], [AVR] [[en:avr:usart]], [LIB] [[en:software:homelab:library:usart]], \\ [LIB] [[en:software:homelab:library:module:lcd_graphic]]// ===== Theory ===== [{{ :examples:communication:bluetoothlogo.png?250|Bluetooth logo}}] Bluetooth is a proprietary open industry standard for wireless communication between devices over a short distance. Bluetooth communication uses short-wave radio transmissions, typically 2400–2480 MHz frequency. With a Bluetooth protocol a secure personal area networks (PAN) can be created between fixed and mobile devices. Bluetooth is managed by the Bluetooth Special Interest Group, where most of the world important telecommunication, computing, network and electronic companies having a membership. The organization manages the specification, discusses and confirms new developments and holds trademark. If new development wants to mark its device as a Bluetooth device, it must be qualified according to the standard issued by Bluetooth Special Interest Group and get the licence from the organization. Bluetooth was initially developed as a replacement for cabled connection, mainly RS232 in 1994 by Ericsson in Lund, Sweden. For now, Bluetooth specification has several version, but all are designed to be backward compatible. Version 1.0 had many problems which were fixed with version 1.1 and 1.2. In 2004 version 2 was released where main difference compared with 1.2 was the enhanced data rate for faster communication. The nominal data rate was about 3 Mbit/s, but less in practice.In 2009 version 3 was introduced with theoretical 24 Mbit/s data transfer rate. The latest version is a Bluetooth Core Specification version 4.0, which includes classic Bluetooth, Bluetooth high speed and Bluetooth low energy protocols. Bluetooth high speed is based on WiFi, and classic Bluetooth consists of legacy Bluetooth protocols. ===== Practice ===== [{{ ::examples:communication:bt.jpg?220|BT module.}}] Bluetooth Bee module fits into "XBee" slot on Communication board and on Combo board. As with the ZigBee also Bluetooth Bee uses for communicating with controller controller's USART interface. By default when module is powered up it goes to command mode. Bluetooth Bee module considers command correct if it starts and ends with ACII characters CR (13 ie '\r') and LF (10 ie '\n'). For testing it is possible to connect it to computer's serial interface. Hyper Terminal can be used for communicating with it. Hyper Terminal should be configure as following - //Properties//, //Settings tab//, //ASCII Setup...// and check boxes //Send line ends with line feeds// and //Echo typed characters locally//. Before writing a command to the terminal press Enter and write command without any other characters (eg „+INQ=1“) and press Enter one more time. In case of incorrect command the module will response "ERROR", in case of correct command - "OK". Sending commands from the controller it's mandatory to add at the beginning and to the end of the command CR and LF, thus command must be sent in a following form „\r\n+INQ=1\r\n“, for that string is compiled with a first character '\r\' etc. If module is powered up and is available for communication trough serial interface, then the green LED flashes at a rate of two times per second. By those default settings the Bluetooth Bee isn't discoverable on the computer if this is used to search other Bluetooth devices in range. Bluetooth Bee module can be connected to a computer in two way's: To configure Bluetooth Bee as a slave and connect with it using the computer or configure it as a master, detect computer with Bluetooth support and connect with it. Firstly module pin must be set to pair Bluetooth devices. This is done by „\r\n+STPIN=0000\r\n“ command, where 0000 is default pin code. Command „\r\n+STWMOD=0\r\n“ sets Bluetooth Bee as a slave, „\r\n+INQ=1\r\n“ command makes devise discoverable for other devises (green and red LED starts to flash in turns. Now its discoverable from computer, ti's possible to pair and to connect with it using serial interface. Module asks for PIN code to confirm connection. In case of active connection green LED flashes once in every two seconds. In other case „\r\n+STWMOD=1\r\n“ makes Bluetooth Bee as a master devise and „\r\n+INQ=1\r\n“ makes modules to search other devices. Found Bluetooth devises addresses are transmitted in the form eg „\r\n+RTINQ=0,11,67,AF,95,7E;LAPTOP\r\n“. Connections can be made with desired devise using found addresses „\r\n+CONN=0,11,67,AF,95,7E\r\n“. If connection is established using serial interface, then it's possible to send data with Bluetooth Bee from computer to controller using Hyper Terminal etc. Rising front which is generated whit computer or with controller in Bluetooth Bee PIO0 pin ends the connection. Following source code explains the usage of the Bluetooth module which is similar to the usage of ZigBee. In this example Bluetooth Bee is set as master (that is needed to be done once, as with - setting PIN code, changing name etc.) and connection is made with some other devise. #include #include #include #include //Include ZigBee module library #include // defing USART interface usart port = USART(1); // defing buttons and LEDs pin leds[3] = { PIN(C, 3), PIN(C, 4), PIN(C, 5) }; pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; int8_t wait_button(uint16_t ms); void bluetooth_read_string(usart port, char *text, char chr); //Bluetooth devise address what is connected char btadr[16] = {'0',',','1','1',',','6','7',',','A','F', ',','9','5',',','7','E'}; int main(void) { char tekst[24]; int a = 0; // Configure LED and button I/O ports for (int i=0; i<3; i++) { pin_setup_output(leds[i]); pin_setup_input(buttons[i]); } // USART configuration for Bluetooth communication usart_init_async(port, USART_DATABITS_8, USART_STOPBITS_ONE, USART_PARITY_NONE, USART_BAUDRATE_ASYNC(38400)); // LCD display initalization lcd_gfx_init(); // Turning back light ON lcd_gfx_backlight(true); // Configuration of Bluetooth device as a master // it's necessarily to do once, // the devise will save settings lcd_gfx_clear(); // Send command to the module usart_send_string(port,"\r\n+STWMOD=1\r\n"); // reads until correct answer is received while (tekst[9] != '1') { //Read one sent string bluetooth_read_string(port,tekst,10); //Write to the display lcd_gfx_goto_char_xy(0, a>>1); lcd_gfx_write_string(tekst); a++; } hw_delay_ms(3000); // Making the connection with desired device lcd_gfx_clear(); //Send command usart_send_string(port,"\r\n+CONN="); // Send the address of a device to connect to usart_send_string(port,btadr); // End command usart_send_string(port,"\r\n"); //wait for replay for (a=0; a<8; a++) { bluetooth_read_string(port,tekst,10); lcd_gfx_goto_char_xy(0, a>>1); lcd_gfx_write_string(tekst); } hw_delay_ms(3000); // Displays info on LCD lcd_gfx_clear(); lcd_gfx_goto_char_xy(0, 0); lcd_gfx_write_string("Bluetooth demo"); lcd_gfx_goto_char_xy(0, 1); lcd_gfx_write_string("Nupp1: LED roh"); lcd_gfx_goto_char_xy(0, 2); lcd_gfx_write_string("Nupp2: LED kol"); lcd_gfx_goto_char_xy(0, 3); lcd_gfx_write_string("Nupp2: LED pun"); // Save the state of previous button to avoid multiple button pushes at once. // At first is -1 ie none of the buttons is pushed. int8_t previousButton = -1; // Endles-loop for communicating between modules while (true) { int8_t button; //Variable for saving button pushes // Wait 1 millisecond for button button = wait_button(1); // If in last cycle button wasn't pressed but now is if (previousButton == -1 && button != -1) { // Convert button's index by adding A // and sent it to other modules // A is for first button, B for second and so on usart_send_char(port, 'A' + button); } // Read from USART if (usart_has_data(port)) { // Read bait, convert to leds array index //and change the output. pin_toggle(leds[usart_read_char(port) - 'A']); } // remember what button was pressed previousButton = button; } } // Wait for button to be pressed for ms milliseconds. // If button is pressed returns the queue number of the button. int8_t wait_button(uint16_t ms) { // By default -1 means, that no button is pressed. int8_t button_nr = -1; uint16_t counter = 0; do { // Check if one of the buttons is pressed for (uint8_t i=0; i<3; i++) { if (!pin_get_value(buttons[i])) { button_nr = i; break; } } // Wait for 1 millisecond hw_delay_ms(1); // increase millisecond counter counter++; } while (button_nr == -1 && (counter < ms)); return button_nr; } void bluetooth_read_string(usart port, char *text, char chr) { char buf = 0; int end = 0; int lnd = 0; // Save necessary parts of received string while (end < 2) { // Wait for receiving bait while (!usart_has_data(port)); // Load bait buf = usart_read_char(port); // If saving is enabled if (end == 1) { // End in case of CR, else save if (buf == 13) end = 2; else text[lnd] = buf; // index counter lnd++; } // If character is correct then save if (buf == chr) end = 1; } // string terminator at the end text[lnd-1] = 0; }