This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:examples:communication:bluetooth:eunistone [2012/10/22 10:53] – rellermaa | et:examples:communication:bluetooth:eunistone [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| [LIB] [[et: | [LIB] [[et: | ||
| [LIB] [[et: | [LIB] [[et: | ||
| + | |||
| + | **BTBee Pro mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| + | **Bluetooth Bee mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| + | \\ | ||
| + | \\ | ||
| + | **Täielik AT käskude nimekiri (en):** \\ | ||
| + | [[http:// | ||
| + | |||
| ===== Teooria ===== | ===== Teooria ===== | ||
| - | {{ :: | + | {{ :: |
| Bluetooth on tööstusstandard seadmete omavaheliseks traadita ühenduseks. Selle eesmärgiks on juhtmeühenduste asendamine mobiiltelefonide, | Bluetooth on tööstusstandard seadmete omavaheliseks traadita ühenduseks. Selle eesmärgiks on juhtmeühenduste asendamine mobiiltelefonide, | ||
| Line 31: | Line 41: | ||
| Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. | ||
| - | Järgnev kood selgitab mooduli kasutamist analoogselt ZigBee näitele. Siin seatakse Bluetooth Bee ülemaks seadmeks (seda on vaja teha ainult esimesel korral, nagu ka pin-koodi, mooduli nime jm. seadete sisestust) ja luuakse selle abil ühendus | + | Järgnev kood näitab lühidalt, kuidas luua ühendus |
| <code c> | <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, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||