This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:examples:communication:bluetooth [2012/07/18 00:52] – raivo.sell | en:examples:communication:bluetooth [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Bluetooth ====== | ||
| + | //The necessary knowledge: [HW] [[en: | ||
| + | |||
| + | ===== 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 starts and ends with ACII characters CR (13 ie ' | ||
| + | |||
| + | 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 devices in range. | ||
| + | |||
| + | Bluetooth Bee module can be connected to a computer in two way's: To configure Bluetooth Bee as a slave and connect with it using the computer or configure it as a master, detect computer with Bluetooth support and connect with it. | ||
| + | |||
| + | Firstly module pin must be set to pair Bluetooth devices. This is done by „\r\n+STPIN=0000\r\n“ command, where 0000 is default pin code. Command „\r\n+STWMOD=0\r\n“ sets Bluetooth Bee as a slave, „\r\n+INQ=1\r\n“ command makes devise discoverable for other devises (green and red LED starts to flash in turns. Now its discoverable from computer, ti's possible to pair and to connect with it using serial interface. Module asks for PIN code to confirm connection. In case of active connection green LED flashes once in every two seconds. | ||
| + | |||
| + | In other case „\r\n+STWMOD=1\r\n“ makes Bluetooth Bee as a master devise and „\r\n+INQ=1\r\n“ makes modules to search other devices. Found Bluetooth devises addresses are transmitted in the form eg „\r\n+RTINQ=0, | ||
| + | |||
| + | 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. | ||
| + | |||
| + | Following source code explains the usage of the Bluetooth module which is similar to the usage of ZigBee. In this example Bluetooth Bee is set as master (that is needed to be done once, as with - setting PIN code, changing name etc.) and connection is made with some other devise. | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | //Include ZigBee module library | ||
| + | #include < | ||
| + | |||
| + | // defing USART interface | ||
| + | usart port = USART(1); | ||
| + | |||
| + | // defing buttons and LEDs | ||
| + | pin leds[3] = { PIN(C, 3), PIN(C, 4), PIN(C, 5) }; | ||
| + | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | ||
| + | |||
| + | int8_t wait_button(uint16_t ms); | ||
| + | void bluetooth_read_string(usart port, char *text, char chr); | ||
| + | |||
| + | //Bluetooth devise address what is connected | ||
| + | char btadr[16] = {' | ||
| + | ',',' | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | char tekst[24]; | ||
| + | int a = 0; | ||
| + | |||
| + | // Configure LED and button I/O ports | ||
| + | for (int i=0; i<3; i++) | ||
| + | { | ||
| + | pin_setup_output(leds[i]); | ||
| + | pin_setup_input(buttons[i]); | ||
| + | } | ||
| + | |||
| + | // USART configuration for Bluetooth communication | ||
| + | usart_init_async(port, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | // LCD display initalization | ||
| + | lcd_gfx_init(); | ||
| + | |||
| + | // Turning back light ON | ||
| + | lcd_gfx_backlight(true); | ||
| + | |||
| + | // Configuration of Bluetooth device as a master | ||
| + | // it's necessarily to do once, | ||
| + | // the devise will save settings | ||
| + | lcd_gfx_clear(); | ||
| + | // Send command to the module | ||
| + | usart_send_string(port," | ||
| + | // reads until correct answer is received | ||
| + | while (tekst[9] != ' | ||
| + | { | ||
| + | //Read one sent string | ||
| + | bluetooth_read_string(port, | ||
| + | //Write to the display | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(tekst); | ||
| + | a++; | ||
| + | } | ||
| + | hw_delay_ms(3000); | ||
| + | |||
| + | // Making the connection with desired device | ||
| + | lcd_gfx_clear(); | ||
| + | //Send command | ||
| + | usart_send_string(port," | ||
| + | // Send the address of a device to connect to | ||
| + | usart_send_string(port, | ||
| + | // End command | ||
| + | usart_send_string(port," | ||
| + | //wait for replay | ||
| + | for (a=0; a<8; a++) | ||
| + | { | ||
| + | bluetooth_read_string(port, | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(tekst); | ||
| + | } | ||
| + | hw_delay_ms(3000); | ||
| + | |||
| + | // Displays info on LCD | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | // Save the state of previous button to avoid multiple button pushes at once. | ||
| + | // At first is -1 ie none of the buttons is pushed. | ||
| + | int8_t previousButton = -1; | ||
| + | |||
| + | // Endles-loop for communicating between modules | ||
| + | while (true) | ||
| + | { | ||
| + | int8_t button; | ||
| + | |||
| + | // Wait 1 millisecond for button | ||
| + | button = wait_button(1); | ||
| + | |||
| + | // If in last cycle button wasn't pressed but now is | ||
| + | if (previousButton == -1 && button != -1) | ||
| + | { | ||
| + | // Convert button' | ||
| + | // and sent it to other modules | ||
| + | // A is for first button, B for second and so on | ||
| + | usart_send_char(port, | ||
| + | } | ||
| + | |||
| + | // Read from USART | ||
| + | if (usart_has_data(port)) | ||
| + | { | ||
| + | // Read bait, convert to leds array index | ||
| + | //and change the output. | ||
| + | pin_toggle(leds[usart_read_char(port) - ' | ||
| + | } | ||
| + | |||
| + | // remember what button was pressed | ||
| + | previousButton = button; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Wait for button to be pressed for ms milliseconds. | ||
| + | // If button is pressed returns the queue number of the button. | ||
| + | int8_t wait_button(uint16_t ms) | ||
| + | { | ||
| + | // By default -1 means, that no button is pressed. | ||
| + | int8_t button_nr = -1; | ||
| + | uint16_t counter = 0; | ||
| + | do | ||
| + | { | ||
| + | // Check if one of the buttons is pressed | ||
| + | for (uint8_t i=0; i<3; i++) | ||
| + | { | ||
| + | if (!pin_get_value(buttons[i])) | ||
| + | { | ||
| + | button_nr = i; | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Wait for 1 millisecond | ||
| + | hw_delay_ms(1); | ||
| + | |||
| + | // increase millisecond counter | ||
| + | counter++; | ||
| + | } while (button_nr == -1 && (counter < ms)); | ||
| + | |||
| + | return button_nr; | ||
| + | } | ||
| + | |||
| + | void bluetooth_read_string(usart port, char *text, char chr) | ||
| + | { | ||
| + | char buf = 0; | ||
| + | int end = 0; | ||
| + | int lnd = 0; | ||
| + | |||
| + | // Save necessary parts of received string | ||
| + | while (end < 2) | ||
| + | { | ||
| + | // Wait for receiving bait | ||
| + | while (!usart_has_data(port)); | ||
| + | |||
| + | // Load bait | ||
| + | buf = usart_read_char(port); | ||
| + | |||
| + | // If saving is enabled | ||
| + | if (end == 1) | ||
| + | { | ||
| + | // End in case of CR, else save | ||
| + | if (buf == 13) end = 2; | ||
| + | else text[lnd] = buf; | ||
| + | // index counter | ||
| + | | ||
| + | } | ||
| + | // If character is correct then save | ||
| + | if (buf == chr) end = 1; | ||
| + | } | ||
| + | // string terminator at the end | ||
| + | text[lnd-1] = 0; | ||
| + | } | ||
| + | |||
| + | </ | ||