This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:examples:communication:bluetooth [2012/04/16 21:54] – created illo | en:examples:communication:bluetooth [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <note important> | ||
- | |||
====== Bluetooth ====== | ====== Bluetooth ====== | ||
//The necessary knowledge: [HW] [[en: | //The necessary knowledge: [HW] [[en: | ||
- | ===== Teooria | + | ===== Theory |
- | [{{ | + | [{{ : |
- | Bluetooth is a industry standard for wireless communication between devices. | + | Bluetooth is a proprietary open industry standard for wireless communication between devices |
+ | Bluetooth is managed by the Bluetooth Special Interest Group, where most of the world important telecommunication, computing, network | ||
- | ===== Praktika ===== | + | 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 fits into " | ||
- | Bluetoot | + | Bluetooth |
- | Kui mooduli saab toite ja on jadaliidese pordi kaudu suhtlemiseks saadaval, siis vilgub roheline | + | If module is powered up and is available for communication trough serial interface, then the green LED flashes at a rate of two times per second. By those default settings the Bluetooth Bee isn't discoverable on the computer if this is used to search other Bluetooth |
- | Bluetooth Bee moodulit saab ühendada arvutiga kahel viisil: kas seadistada | + | Bluetooth Bee module can be connected to a computer in two way's: To configure |
- | Esmalt tuleb määrata mooduli | + | Firstly module |
- | Käsk „\r\n+STWMOD=0\r\n“ | + | |
- | „\r\n+INQ=1\r\n“ | + | |
- | Teisel juhul viib „\r\n+STWMOD=1\r\n“ Bluetooth Bee ülema seadme režiimi. | + | In other case „\r\n+STWMOD=1\r\n“ |
- | „\r\n+INQ=1\r\n“ | + | |
- | Leitud | + | |
- | Leitud aadresside abil saab luua ühenduse soovitud seadmega | + | |
- | Kui ühendus on loodud virtuaalse jadaliidese abil, siis on võimalik arvutist saata Hyper Terminali vms. kaudu andmeid | + | If connection is established using serial interface, then it's possible to send data with Bluetooth Bee from computer to controller using Hyper Terminal etc. Rising front which is generated whit computer or with controller in Bluetooth Bee PIO0 pin ends the connection. |
- | Järgnev kood selgitab mooduli kasutamist analoogselt | + | Following source code explains the usage of the Bluetooth module which is similar to the usage of ZigBee. |
<code c> | <code c> | ||
Line 41: | Line 39: | ||
#include < | #include < | ||
- | //Lisame | + | //Include |
#include < | #include < | ||
- | // USART liidese määramine | + | // defing |
usart port = USART(1); | usart port = USART(1); | ||
- | // LEDid ja nupud määrata | + | // defing buttons and LEDs |
pin leds[3] = { PIN(C, 3), PIN(C, 4), PIN(C, 5) }; | pin leds[3] = { PIN(C, 3), PIN(C, 4), PIN(C, 5) }; | ||
pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | ||
Line 54: | Line 52: | ||
void bluetooth_read_string(usart port, char *text, char chr); | void bluetooth_read_string(usart port, char *text, char chr); | ||
- | //Ühendatavad seadme | + | // |
char btadr[16] = {' | char btadr[16] = {' | ||
',',' | ',',' | ||
Line 63: | Line 61: | ||
int a = 0; | int a = 0; | ||
- | // seadista LEDide ja nuppude | + | // Configure LED and button |
for (int i=0; i<3; i++) | for (int i=0; i<3; i++) | ||
{ | { | ||
Line 70: | Line 68: | ||
} | } | ||
- | // USART liidese seadistamine | + | // USART configuration for Bluetooth |
usart_init_async(port, | usart_init_async(port, | ||
| | ||
Line 77: | Line 75: | ||
| | ||
- | // LCD ekraani algseadistamine | + | // LCD display initalization |
lcd_gfx_init(); | lcd_gfx_init(); | ||
- | // Taustavalgustuse tööle lülitamine | + | // Turning back light ON |
lcd_gfx_backlight(true); | lcd_gfx_backlight(true); | ||
- | // Bluetooth | + | // Configuration of Bluetooth |
- | // seda on vaja teha ainult üks kord, | + | // it's necessarily to do once, |
- | // järgmine kord moodul juba mäletab seadeid | + | // the devise will save settings |
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
- | // saada moodulile käsk | + | // Send command to the module |
usart_send_string(port," | usart_send_string(port," | ||
- | // loeb kuni õige vastus tuleb | + | // reads until correct answer is received |
while (tekst[9] != ' | while (tekst[9] != ' | ||
{ | { | ||
- | //loe üks mooduli saadetud | + | //Read one sent string |
bluetooth_read_string(port, | bluetooth_read_string(port, | ||
- | //kirjuta ekraanile | + | //Write to the display |
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
lcd_gfx_write_string(tekst); | lcd_gfx_write_string(tekst); | ||
Line 101: | Line 99: | ||
hw_delay_ms(3000); | hw_delay_ms(3000); | ||
- | // Ühendamine soovitud seadmega | + | // Making the connection with desired device |
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
- | //saada käsk | + | //Send command |
usart_send_string(port," | usart_send_string(port," | ||
- | // saada ühendatava seadme aadress | + | // Send the address of a device to connect to |
usart_send_string(port, | usart_send_string(port, | ||
- | // lõpeta käsk | + | // End command |
usart_send_string(port," | usart_send_string(port," | ||
- | //oota vastuseid | + | //wait for replay |
for (a=0; a<8; a++) | for (a=0; a<8; a++) | ||
{ | { | ||
Line 118: | Line 116: | ||
hw_delay_ms(3000); | hw_delay_ms(3000); | ||
- | // Joonistab ekraanile infot | + | // Displays info on LCD |
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
Line 129: | Line 127: | ||
lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
- | // Hoia meeles eelmist nuppu selleks, et vältida olukorda, | + | // Save the state of previous button to avoid multiple button pushes at once. |
- | // kus nuppu all hoides tuleb sada nupuvajutusi järjest. | + | // At first is -1 ie none of the buttons is pushed. |
- | // Algatuseks olgu see -1 ehk ükski nupp pole all. | + | |
int8_t previousButton = -1; | int8_t previousButton = -1; | ||
- | // Lõputu tsükkel moodulite andmetevahetuse näitamiseks | + | // Endles-loop for communicating between modules |
while (true) | while (true) | ||
{ | { | ||
- | int8_t button; //muutuja nupuvajutuse registreerimiseks | + | int8_t button; //Variable for saving button pushes |
- | // Oota nuppu 1 millisekund | + | // Wait 1 millisecond for button |
button = wait_button(1); | button = wait_button(1); | ||
- | // Kui eelmises tsüklis oli nupp üleval ja nüüd on all | + | // If in last cycle button wasn't pressed but now is |
if (previousButton == -1 && button != -1) | if (previousButton == -1 && button != -1) | ||
{ | { | ||
- | // teisenda nupu indeks liites | + | // Convert button' |
- | // ja saada teisele moodulile | + | // and sent it to other modules |
- | // A täht on esimene nupp, B täht teine nupp jne | + | // A is for first button, B for second and so on |
usart_send_char(port, | usart_send_char(port, | ||
} | } | ||
- | // Loe USART'st | + | // Read from USART |
if (usart_has_data(port)) | if (usart_has_data(port)) | ||
{ | { | ||
- | // loe bait, teisenda | + | // Read bait, convert to leds array index |
- | //ja muuda väljundit. | + | //and change the output. |
pin_toggle(leds[usart_read_char(port) - ' | pin_toggle(leds[usart_read_char(port) - ' | ||
} | } | ||
- | // Jäta meelde mis nupp oli hetkel alla vajutatud | + | // remember what button was pressed |
previousButton = button; | previousButton = button; | ||
} | } | ||
} | } | ||
- | // Ootab nupu vajutust | + | // Wait for button to be pressed for ms milliseconds. |
- | //Kui tuleb nupu vajutus, tagastab vastava nupu järjekorra nr-i | + | // If button is pressed returns the queue number of the button. |
int8_t wait_button(uint16_t ms) | int8_t wait_button(uint16_t ms) | ||
{ | { | ||
- | // Vaikimisi | + | // By default |
int8_t button_nr = -1; | int8_t button_nr = -1; | ||
uint16_t counter = 0; | uint16_t counter = 0; | ||
do | do | ||
{ | { | ||
- | // vaata kas mõni nupp on all | + | // Check if one of the buttons is pressed |
for (uint8_t i=0; i<3; i++) | for (uint8_t i=0; i<3; i++) | ||
{ | { | ||
Line 183: | Line 180: | ||
} | } | ||
- | // oota 1 millisekund | + | // Wait for 1 millisecond |
hw_delay_ms(1); | hw_delay_ms(1); | ||
- | // suurenda millisekundi loendurit | + | // increase millisecond counter |
counter++; | counter++; | ||
} while (button_nr == -1 && (counter < ms)); | } while (button_nr == -1 && (counter < ms)); | ||
Line 199: | Line 196: | ||
int lnd = 0; | int lnd = 0; | ||
- | // salvestab vajaliku osa saadud stringist | + | // Save necessary parts of received string |
while (end < 2) | while (end < 2) | ||
{ | { | ||
- | // oota saabuvat baiti | + | // Wait for receiving bait |
while (!usart_has_data(port)); | while (!usart_has_data(port)); | ||
- | // lae ringi saadud | + | // Load bait |
buf = usart_read_char(port); | buf = usart_read_char(port); | ||
- | // kui lubatud salvestada | + | // If saving is enabled |
if (end == 1) | if (end == 1) | ||
{ | { | ||
- | // | + | // |
if (buf == 13) end = 2; | if (buf == 13) end = 2; | ||
else text[lnd] = buf; | else text[lnd] = buf; | ||
- | // | + | // |
| | ||
} | } | ||
- | // kui on õige tähemärk, siis salvestama | + | // If character is correct then save |
if (buf == chr) end = 1; | if (buf == chr) end = 1; | ||
} | } | ||
- | // stringi terminaator lõppu | + | // string terminator at the end |
text[lnd-1] = 0; | text[lnd-1] = 0; | ||
} | } | ||
</ | </ |