Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:examples:communication:bluetooth [2012/04/16 21:54] – created illoen:examples:communication:bluetooth [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-<note important>On preparation</note> 
- 
 ====== Bluetooth ====== ====== Bluetooth ======
  
 //The necessary knowledge: [HW] [[en:hardware:homelab:controller]], [AVR] [[en:avr:usart]], [LIB] [[en:software:homelab:library:usart]], \\ [LIB] [[en:software:homelab:library:module:lcd_graphic]]// //The necessary knowledge: [HW] [[en:hardware:homelab:controller]], [AVR] [[en:avr:usart]], [LIB] [[en:software:homelab:library:usart]], \\ [LIB] [[en:software:homelab:library:module:lcd_graphic]]//
  
-===== Teooria =====+===== Theory =====
  
-[{{  ::examples:communication:bt.jpg?220|BT module.}}]+[{{  :examples:communication:bluetoothlogo.png?250|Bluetooth logo}}]
  
-Bluetooth is a industry standard for wireless communication between devices. The aim is to remove wire connections between connecting mobile phonescomputersetc  and periphery devices.+Bluetooth is a proprietary open industry standard for wireless communication between devices over a short distanceBluetooth communication uses short-wave radio transmissions, typically  2400–2480 MHz frequency. With a Bluetooth protocol a secure personal area networks (PAN) can be created between fixed and mobile devices. 
 +Bluetooth is managed by the Bluetooth Special Interest Groupwhere most of the world important telecommunicationcomputing, network and electronic companies having a membership. The organization manages the specification, discusses and confirms new developments and holds trademark. If new development wants to mark its device as a Bluetooth device, it must be qualified according to the standard issued by Bluetooth Special Interest Group and get the licence from the organization.
  
-===== 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 =====
 +
 +[{{  ::examples:communication:bt.jpg?220|BT module.}}]
 Bluetooth Bee module fits into "XBee" slot on Communication board and on Combo board. As with the ZigBee also Bluetooth Bee uses for communicating with controller controller's USART interface. By default when module is powered up it goes to command mode. Bluetooth Bee module fits into "XBee" slot on Communication board and on Combo board. As with the ZigBee also Bluetooth Bee uses for communicating with controller controller's USART interface. By default when module is powered up it goes to command mode.
  
-Bluetoot Bee moodul loeb käsu õigeks, kui see on alustatud ja lõpetatud ASCII märkidega CR (13 ehk ’\rja LF (10 ehk ’\n). Moodulit on võimalik testimiseks ühendada ka arvuti jadaliidesesseSuhtlemiseks näiteks Hyper Terminali abil tuleb seal valida //Properties//, //Settings tab//, //ASCII Setup...// ning märkida linnukesega //Send line ends with line feeds// ja //Echo typed characters locally//Enne käsu kirjutamist vajutada korra ENTER klahvile ja kirjutada ainult käsk ilma muude märkideta (näiteks „+INQ=1“) ja veelkord ENTERVale käsu saatmisel vastab moodul „ERRORvastasel juhul „OKKontrollerist moodulile käsu saatmisel on vajalik lisada siiski algusesse ja lõppu ka CR ja LF ehk käsk tuleb saata kujul „\r\n+INQ=1\r\n“ ja selleks koostatakse string esimese elemendiga ’\r\’ jne.+Bluetooth Bee module considers command correct if it starts and ends with ACII characters CR (13 ie '\r'and LF (10 ie '\n'). For testing it is possible to connect it to computer's serial interface. Hyper Terminal can be used for communicating with it. Hyper Terminal should be configure as following - //Properties//, //Settings tab//, //ASCII Setup...// and check boxes  //Send line ends with line feeds// and //Echo typed characters locally//Before writing a command to the terminal press Enter and write command without any other characters (eg „+INQ=1“) and press Enter one more timeIn case of incorrect command the module will response "ERROR"in case of correct command - "OK"Sending commands from the controller it's mandatory to add at the beginning  and to the end of the command CR and LF, thus command must be  sent in a following form „\r\n+INQ=1\r\n“, for that string is compiled with a first character '\r\' etc 
  
-Kui mooduli saab toite ja on jadaliidese pordi kaudu suhtlemiseks saadavalsiis vilgub roheline LED kiirelt kaks korda sekundisSelles vaikimisi olekus ei ole Bluetooth Bee arvutist leitav, kui sealt ümberringi asuvaid Bluetooth seadmeid otsida.+If module is powered up and is available for communication trough serial interfacethen the green LED flashes at a rate of two times per secondBy 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 moodulit saab ühendada arvutiga kahel viisilkas seadistada Bluetooth Bee alluvaks seadmeks ja alustada ühendust arvutist või seadistada see ülemaks seadmeksdetekteerida Bluetooth toega arvuti ja ühendada see endaga.+Bluetooth Bee module can be connected to a computer in two way'sTo configure Bluetooth Bee as a slave and connect with it using the computer or configure it as a masterdetect computer with Bluetooth support and connect with it 
  
-Esmalt tuleb määrata mooduli pin-kood Bluetooth seadmete paarimiseksSee toimub käsuga „\r\n+STPIN=0000\r\n“, kus 0000 on pin-koodina vaikimisi kasutusel. +Firstly module pin must be set to pair Bluetooth devicesThis is done by „\r\n+STPIN=0000\r\n“ commandwhere 0000 is default pin codeCommand „\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 turnsNow its discoverable from computerti's possible to pair and to connect with it using serial interfaceModule asks for PIN code to confirm connectionIn case of active connection green LED flashes once in every two seconds   
-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 leitavselle saab paarida ja luua ühenduse virtuaalse jadaliidese kauduMoodul küsib ühenduse loomisel arvutist ka pin-koodiAktiivse ü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. +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 devicesFound Bluetooth devises addresses are transmitted in the form eg „\r\n+RTINQ=0,11,67,AF,95,7E;LAPTOP\r\n“. Connections can be made with desired devise using found addresses „\r\n+CONN=0,11,67,AF,95,7E\r\n“. 
-„\r\n+INQ=1\r\n“ paneb mooduli teisi seadmeid otsima. +
-Leitud Bluetooth seadmete või arvutite aadressid saadab moodul tagasi näiteks kujul „\r\n+RTINQ=0,11,67,AF,95,7E;LAPTOP\r\n“. +
-Leitud aadresside abil saab luua ühenduse soovitud seadmega „\r\n+CONN=0,11,67,AF,95,7E\r\n“.+
  
-Kui ühendus on loodud virtuaalse jadaliidese abilsiis 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.+If connection is established using serial interfacethen it's possible to send data with Bluetooth Bee from computer to controller using Hyper Terminal etcRising front which is generated whit computer or with controller in Bluetooth Bee PIO0 pin ends the connection.
  
-Järgnev kood selgitab mooduli kasutamist analoogselt ZigBee näiteleSiin seatakse Bluetooth Bee ülemaks seadmeks (seda on vaja teha ainult esimesel korralnagu ka pin-koodimooduli nime jmseadete sisestustja luuakse selle abil ühendus mõne muu seadmega.+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 onceas with setting PIN codechanging name etc.) and connection is made with some other devise.
  
 <code c> <code c>
Line 41: Line 39:
 #include <homelab/pin.h> #include <homelab/pin.h>
  
-//Lisame ZigBee mooduli toe+//Include ZigBee module library 
 #include <homelab/module/zigbee.h> #include <homelab/module/zigbee.h>
  
-// USART liidese määramine+// defing USART interface
 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 Bluetooth aadress+//Bluetooth devise address what is connected
 char btadr[16] = {'0',',','1','1',',','6','7',',','A','F', char btadr[16] = {'0',',','1','1',',','6','7',',','A','F',
    ',','9','5',',','7','E'};    ',','9','5',',','7','E'};
Line 63: Line 61:
     int a = 0;     int a = 0;
   
-    // seadista LEDide ja nuppude I/O pordid+    // Configure LED and button I/O ports
     for (int i=0; i<3; i++)     for (int i=0; i<3; i++)
     {     {
Line 70: Line 68:
     }     }
  
-    // USART liidese seadistamine Bluetooth side jaoks+    // USART configuration for Bluetooth communication
     usart_init_async(port,     usart_init_async(port,
                      USART_DATABITS_8,                      USART_DATABITS_8,
Line 77: Line 75:
                      USART_BAUDRATE_ASYNC(38400));                      USART_BAUDRATE_ASYNC(38400));
  
-    // 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 Bee seadistamine ülemaks seadmeks +    // Configuration of Bluetooth device as a master 
-    // 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,"\r\n+STWMOD=1\r\n");     usart_send_string(port,"\r\n+STWMOD=1\r\n");
-    // loeb kuni õige vastus tuleb+    // reads until correct answer is received
     while (tekst[9] != '1')     while (tekst[9] != '1')
     {     {
-    //loe üks mooduli saadetud string +    //Read one sent string 
         bluetooth_read_string(port,tekst,10);         bluetooth_read_string(port,tekst,10);
-        //kirjuta ekraanile+        //Write to the display
  lcd_gfx_goto_char_xy(0, a>>1);  lcd_gfx_goto_char_xy(0, a>>1);
  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,"\r\n+CONN=");     usart_send_string(port,"\r\n+CONN=");
-    // saada ühendatava seadme aadress+    // Send the address of a device to connect to
     usart_send_string(port,btadr);     usart_send_string(port,btadr);
-    // lõpeta käsk+    // End command
     usart_send_string(port,"\r\n");     usart_send_string(port,"\r\n");
-    //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, 0);     lcd_gfx_goto_char_xy(0, 0);
Line 129: Line 127:
     lcd_gfx_write_string("Nupp2: LED pun");     lcd_gfx_write_string("Nupp2: LED pun");
  
-    // 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 millisekund+        // Wait 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 täht +            // Convert button's index by adding 
-     // 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, 'A' + button);             usart_send_char(port, 'A' + button);
         }         }
  
-        // Loe USART'st+        // Read from USART
         if (usart_has_data(port))         if (usart_has_data(port))
         {         {
-            // loe bait, teisenda leds massiivi indeksiks +            // Read bait, convert to leds array index 
-     //ja muuda väljundit.+     //and change the output.
             pin_toggle(leds[usart_read_char(port) - 'A']);             pin_toggle(leds[usart_read_char(port) - 'A']);
         }         }
  
-        // Jäta meelde mis nupp oli hetkel alla vajutatud+        // remember what button was pressed
         previousButton = button;         previousButton = button;
     }     }
 } }
  
-// Ootab nupu vajutust ms millisekundit+// 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 -1 tähendabet ükski nupp pole vajutatud.+    // By default -1 meansthat no button is pressed.
     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 millisekund+        // Wait for 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 bait+ // 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)
  {  {
-             // lõpeta CR pealemuul juhul salvesta+             // End in case of CR, else save
              if (buf == 13) end = 2;              if (buf == 13) end = 2;
              else text[lnd] = buf;              else text[lnd] = buf;
-      // kohaloendur+      // index counter
       lnd++;       lnd++;
  }  }
- // 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;
 } }
  
 </code> </code>
en/examples/communication/bluetooth.1334613242.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0