This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| et:examples:communication:bluetooth:eunistone [2012/10/22 10:25] – tekitatud rellermaa | et:examples:communication:bluetooth:eunistone [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Bluetooth ====== | ====== Bluetooth ====== | ||
| - | //Vajalikud teadmised: \\ | + | Vajalikud teadmised: \\ |
| [HW] [[et: | [HW] [[et: | ||
| [AVR] [[et: | [AVR] [[et: | ||
| Line 7: | Line 7: | ||
| [LIB] [[et: | [LIB] [[et: | ||
| - | ===== Teooria ===== | + | **BTBee Pro mooduli puhul vaata siia:** |
| + | [[et/ | ||
| + | **Bluetooth Bee mooduli puhul vaata siia:** | ||
| + | [[et/ | ||
| + | \\ | ||
| + | \\ | ||
| + | **Täielik AT käskude nimekiri (en):** \\ | ||
| + | [[http:// | ||
| - | [{{ :: | + | |
| + | ===== 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, | ||
| - | ===== Praktika ===== | + | ===== Praktika ===== |
| Bluetooth Bee moodul sobib Kodulabori sidemooduli plaadile „XBee" | Bluetooth Bee moodul sobib Kodulabori sidemooduli plaadile „XBee" | ||
| - | Bluetoot Bee moodul loeb käsu õigeks, | + | Bluetoot Bee moodul loeb käsu õigeks, lõpetatud ASCII märkidega CR (13 ehk ’\r’) ja LF (10 ehk ’\n’). Moodulit on võimalik testimiseks ühendada ka arvuti jadaliidesesse. Suhtlemiseks näiteks Hyper Terminali abil tuleb seal valida // |
| - | Kui mooduli | + | Bloetooth |
| + | ^ Käsk ^ Seletus ^ | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| + | | " | ||
| - | Bluetooth | + | Kui ühendus on loodud, tuleks |
| - | Esmalt tuleb määrata mooduli pin-kood Bluetooth seadmete paarimiseks. See toimub käsuga „\r\n+STPIN=0000\r\n“, kus 0000 on pin-koodina vaikimisi kasutusel. | + | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid |
| - | Käsk „\r\n+STWMOD=0\r\n“ seab Bluetooth Bee mooduli alluvaks seadmeks. | + | |
| - | „\r\n+INQ=1\r\n“ lubab moodulit leida teistest seadmetest (punane ja roheline LED hakkavad vaheldusmisi vilkuma). Arvutist on seade nüüd leitav, selle saab paarida ja luua ühenduse virtuaalse jadaliidese kaudu. Moodul küsib ühenduse loomisel arvutist ka pin-koodi. Aktiivse ühenduse olemasolul vilgub mooduli roheline LED kord kahe sekundi jooksul. | + | |
| - | Teisel juhul viib „\r\n+STWMOD=1\r\n“ Bluetooth Bee ülema seadme režiimi. | + | Järgnev kood näitab lühidalt, kuidas luua ühendus Kodulabori kontrolleri ning arvuti vahel. |
| - | „\r\n+INQ=1\r\n“ paneb mooduli teisi seadmeid otsima. | + | <code c> |
| - | Leitud Bluetooth seadmete või arvutite aadressid saadab moodul tagasi näiteks kujul „\r\n+RTINQ=0, | + | #define F_CPU 14745600 |
| - | Leitud aadresside abil saab luua ühenduse soovitud seadmega „\r\n+CONN=0, | + | |
| - | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid Bluetooth Bee vahendusel kontrollerile. Ühenduse katkestamiseks tuleb seda teha arvutist või tekitada kontrolleriga Bluetooth Bee PIO0 sisendisse tõusev front. | + | #include < |
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| - | Järgnev kood selgitab mooduli kasutamist analoogselt ZigBee näitele. Siin seatakse Bluetooth Bee ülemaks seadmeks | + | // |
| + | // Select USART channel | ||
| + | usart port = USART(1); | ||
| - | <code c> | + | // |
| + | // 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<<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, | ||
| + | }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, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||