The necessary knowledge:
[HW] Controller module,
[AVR] USART,
[LIB] Serial Interface,
[LIB] Graphic LCD
Complete AT command list:
AT commands
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.
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 <homelab/pin.h> #include <homelab/delay.h> #include <homelab/module/lcd_gfx.h> #include <homelab/usart.h> // // 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<<RXC1) ) dump = UDR1; return 1; } // // Put bluetooth module in transparent mode // uint8_t Bluetooth_transparent() { char str[50]; do { usart_has_data(port); usart_read_string(port,str,50); }while(!strstr(str,"+RSLE")); usart_send_string(port,"AT+JSCR\r\n"); } // // Main program // int main(void) { char text[15]; char dump; char c; int a = 0; int connect = 0; // Initialize LCD lcd_gfx_init(); lcd_gfx_clear(); // Initialize USART usart_init_async(port, USART_DATABITS_8, USART_STOPBITS_ONE, USART_PARITY_NONE, USART_BAUDRATE_ASYNC(115200)); lcd_gfx_write_string("Reset:"); //Connect to Bluetooth on PC while(!Bluetooth_Connect()); lcd_gfx_clear(); lcd_gfx_goto_char_xy(0,1); lcd_gfx_write_string("Connect me..."); //Wait for terminal connection from PC Bluetooth_transparent(); //Connection established, run rest of program. lcd_gfx_clear(); lcd_gfx_goto_char_xy(0,0); lcd_gfx_write_string("Connected"); lcd_gfx_goto_char_xy(0,1); lcd_gfx_write_string("Terminal text is mirrored toterminal and LCD; + sign clears screen "); while(1) { //Read USART data and mirror it to LCD and back to the terminal while(!(UCSR1A & (1<<RXC1))); dump = UDR1; //If '+' character is sent, clear LCD screen if(dump == '+') lcd_gfx_clear(); else { lcd_gfx_write_char(dump); usart_send_char(port,dump); } } }