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:48] – 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 29: | Line 39: | ||
| Kui ühendus on loodud, tuleks Bluetooth moodul panna " | Kui ühendus on loodud, tuleks Bluetooth moodul panna " | ||
| - | Bluetooth Bee moodulit saab ühendada arvutiga kahel viisil: kas seadistada Bluetooth Bee alluvaks seadmeks ja alustada ühendust arvutist või seadistada see ülemaks seadmeks, detekteerida Bluetooth toega arvuti ja ühendada see endaga. | + | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid |
| - | 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. | + | Järgnev |
| - | Käsk „\r\n+STWMOD=0\r\n“ seab Bluetooth Bee mooduli alluvaks seadmeks. | + | <code c> |
| - | „\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. | + | #define F_CPU 14745600 |
| - | Teisel juhul viib „\r\n+STWMOD=1\r\n“ Bluetooth Bee ülema seadme režiimi. | + | #include < |
| - | „\r\n+INQ=1\r\n“ paneb mooduli teisi seadmeid otsima. | + | #include < |
| - | Leitud Bluetooth seadmete või arvutite aadressid saadab moodul tagasi näiteks kujul „\r\n+RTINQ=0, | + | #include < |
| - | Leitud aadresside abil saab luua ühenduse soovitud seadmega „\r\n+CONN=0, | + | #include < |
| - | 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. | + | // |
| + | // Select USART channel | ||
| + | usart port = USART(1); | ||
| - | Järgnev kood selgitab mooduli kasutamist analoogselt ZigBee näitele. Siin seatakse Bluetooth Bee ülemaks seadmeks | + | // |
| + | // 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,str,5); | ||
| + | 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; | ||
| + | } | ||
| - | <code c> | + | // |
| + | // 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, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||