This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:examples:communication:bluetooth:eunistone [2012/10/22 16:29] – created rellermaa | en:examples:communication:bluetooth:eunistone [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Bluetooth ====== | ||
| + | **The necessary knowledge: | ||
| + | [HW] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| + | [LIB] [[en: | ||
| + | |||
| + | **Complete AT command list:** \\ | ||
| + | [[http:// | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | Bluetooth is a proprietary open industry standard for wireless communication between devices over a short distance. Bluetooth communication uses short-wave radio transmissions, | ||
| + | Bluetooth is managed by the Bluetooth Special Interest Group, where most of the world important telecommunication, | ||
| + | |||
| + | 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 ===== | ||
| + | |||
| + | {{ :: | ||
| + | |||
| + | Bluetooth Bee module fits into " | ||
| + | |||
| + | Bluetooth Bee module considers command correct if it ends with ACII characters CR (13 ie ' | ||
| + | |||
| + | 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 ^ | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | |||
| + | After the connection has been established (string " | ||
| + | |||
| + | Example code describes how to create a simple connection between Homelab with Bluetooth module and PC with terminal. | ||
| + | |||
| + | <code c> | ||
| + | #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," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | } while (strcmp(str," | ||
| + | |||
| + | // set request password 123456 | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Set to discoverable | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Register local services | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Auto accept all connection requests | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | // Connect using password 123456 | ||
| + | while(usart_has_data(port)) | ||
| + | dump = usart_read_char(port); | ||
| + | usart_send_string(port," | ||
| + | while(!usart_has_data(port)); | ||
| + | usart_read_string(port, | ||
| + | lcd_gfx_write_string(str); | ||
| + | if (strcmp(str," | ||
| + | return 0; | ||
| + | |||
| + | while( UCSR1A & (1<< | ||
| + | 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, | ||
| + | }while(!strstr(str," | ||
| + | usart_send_string(port," | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // 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_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(115200)); | ||
| + | |||
| + | lcd_gfx_write_string(" | ||
| + | //Connect to Bluetooth on PC | ||
| + | while(!Bluetooth_Connect()); | ||
| + | |||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | //Wait for terminal connection from PC | ||
| + | Bluetooth_transparent(); | ||
| + | |||
| + | // | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | while(1) | ||
| + | { | ||
| + | //Read USART data and mirror it to LCD and back to the terminal | ||
| + | while(!(UCSR1A & (1<< | ||
| + | dump = UDR1; | ||
| + | //If ' | ||
| + | if(dump == ' | ||
| + | lcd_gfx_clear(); | ||
| + | else | ||
| + | { | ||
| + | lcd_gfx_write_char(dump); | ||
| + | usart_send_char(port, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||