====== 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]] \\ **Complete AT command list:** \\ [[http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/Bluetooth_SPP_AT_SW_description.pdf|AT commands]] ===== 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:bluetoothboard.png?220|BT moodul}} 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 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 „AT+JRES“) and press Enter one more time. In case of incorrect command the module will response "ERR", in case of correct command - "OK". Sending commands from the controller it's mandatory to add to the end of the command CR and LF, thus command must be sent in a following form „AT+JRES\r\n“. Easyest way to create a connection between Bluetooth module and a computer is to make the USB module a slave device. This is done using AT commands described here: ^ Command ^ Description ^ | "AT+JSEC=3,1,1,06,123456\r\n" | Select secure connection with 6-digit password "123456" | | "AT+JDIS=3\r\n" | Make module discoverable to other devices | | "AT+JRLS=1101,11,Serial Port,01,000000\r\n" | Register local services (serial port) | | "AT+JAAC=1\r\n" | Automatically accept all incoming connection requests | | "AT+JPCR=06,123456\r\n" | Connect to node using secure connection | After the connection has been established (string "+RSLE" is received from module) the bluetooth module can be put in transparent mode using command "AT+JSCR\r\n". After that has been made, no AT commands will be recognized until hardware reset has been made to the bluetooth chip (or power has been toggled). Example code describes how to create a simple connection between Homelab with Bluetooth module and PC with terminal. #define F_CPU 14745600 #include #include #include #include // // Select USART channel usart port = USART(1); // // Initialize bluetooth module // uint8_t Bluetooth_Connect() { char dump; char str[10]; _delay_ms(500); lcd_gfx_clear(); // Software reset do { while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JRES\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,5); lcd_gfx_write_string(str); } while (strcmp(str,"ROK")); // set request password 123456 while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JSEC=3,1,1,06,123456\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,4); lcd_gfx_write_string(str); if (strcmp(str,"OK") != 0) return 0; // Set to discoverable while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JDIS=3\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,4); lcd_gfx_write_string(str); if (strcmp(str,"OK") != 0) return 0; // Register local services while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JRLS=1101,11,Serial Port,01,000000\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,5); lcd_gfx_write_string(str); if (strcmp(str,"OK") != 0) return 0; // Auto accept all connection requests while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JAAC=1\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,5); lcd_gfx_write_string(str); if (strcmp(str,"OK") != 0) return 0; // Connect using password 123456 while(usart_has_data(port)) dump = usart_read_char(port); usart_send_string(port,"AT+JPCR=06,123456\r\n"); while(!usart_has_data(port)); usart_read_string(port,str,5); lcd_gfx_write_string(str); if (strcmp(str,"OK") != 0) return 0; while( UCSR1A & (1<