This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:examples:communication:zigbee [2012/04/10 13:54] – created illo | en:examples:communication:zigbee [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <note important> | ||
====== ZigBee ====== | ====== ZigBee ====== | ||
Line 7: | Line 6: | ||
===== Theory ===== | ===== Theory ===== | ||
- | [{{ :: | + | [{{ :: |
- | ZigBee is a specification for a suite of high level communication protocols using small, low-power digital radios based on an IEEE 802 standard for personal area networks. | + | ZigBee is a specification for high level communication protocols using small, low-power digital radios based on an IEEE 802.15.4-2003 |
- | ===== In practice ===== | + | Based on ZigBee wireless modules direct connection between two devices or more complicated network can be set up. In figure below a sensor network transferring measurement to user interface is illustrated. |
- | Homelab' | + | ===== Practice ===== |
- | Näiteprogramm otsib esmalt ümbruskonnast teisi ZigBee | + | Robotic HomeLab Communication board has a connector for wireless modules, including |
+ | |||
+ | Example code above is searching surroundings for other Zigbee devices and found device addresses devices are displayed on Homelab User Interface module LCD display. Global address 0000FFFF has a special meaning - info sent to this address is received by all available devices. In the example code, after searching other devices one is connected and devices start to sending a button pressing event to each other by showing the result on the other device | ||
<code c> | <code c> | ||
Line 23: | Line 24: | ||
#include < | #include < | ||
- | //Lisame | + | //Adding |
#include < | #include < | ||
- | // USART liidese määramine | + | // Determination of the USART interface |
usart port = USART(1); | usart port = USART(1); | ||
- | // LEDid ja nupud määrata | + | // LED and button pin setup |
pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | ||
- | // ZigBee | + | // Max numbers of ZigBee |
#define ZIGBEE_MAX_NODES 4 | #define ZIGBEE_MAX_NODES 4 | ||
int8_t wait_button(uint16_t ms); | int8_t wait_button(uint16_t ms); | ||
- | // Teiste leitavate zigbee moodulite aadresside massiiv | + | // Address array of other Zigbee modules found in network |
zigbee_node_t nodelist[ZIGBEE_MAX_NODES]; | zigbee_node_t nodelist[ZIGBEE_MAX_NODES]; | ||
Line 44: | Line 45: | ||
int main(void) | int main(void) | ||
{ | { | ||
- | uint8_t adr = 0; //ühendatava mooduli jrk. nimekirjas | + | uint8_t adr = 0; //Acquired module order list |
- | // seadista LEDide ja nuppude | + | // LED and buttons |
for (int i=0; i<3; i++) | for (int i=0; i<3; i++) | ||
{ | { | ||
Line 53: | Line 54: | ||
} | } | ||
- | // USART liidese seadistamine | + | // Configuration of USART interface for communication whit ZigBee |
usart_init_async(port, | usart_init_async(port, | ||
| | ||
Line 60: | Line 61: | ||
| | ||
- | // LCD ekraani algseadistamine | + | // LCD display initalization |
lcd_gfx_init(); | lcd_gfx_init(); | ||
- | // Ekraani puhastamine | + | // Clear LCD |
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
- | // Taustavalgustuse tööle lülitamine | + | // Turning back light ON |
lcd_gfx_backlight(true); | lcd_gfx_backlight(true); | ||
- | //Oota kuni teisi ZigBee | + | //Wait until other ZigBee |
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
Line 75: | Line 76: | ||
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
- | // Otsib ümbrusest teisiZigBee mooduleid | + | // Searching other ZigBee modules |
- | // täidab | + | // fills nodelist |
zigbee_find_nodes(port, | zigbee_find_nodes(port, | ||
// | // | ||
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
- | // Kuvab ekraanil leitud moodulite nimekirja | + | // Displays the list of found modules on LCD |
- | // (mis reale kirjutab, kust võtab adr, mitu tk max) | + | // (on what line to write, where takes addr., how many max) |
zigbee_lcd_show_nodes(2, | zigbee_lcd_show_nodes(2, | ||
hw_delay_ms(3000); | hw_delay_ms(3000); | ||
lcd_gfx_clear(); | lcd_gfx_clear(); | ||
- | // Kuvab ekraanil ühendamise teate | + | // Displaying connecting on LCD |
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | //kuvab ainult aadressi tagumise osa (8 tähemärki) | + | //Displays only 8 last digit form the address |
lcd_gfx_write_string((nodelist + adr)-> | lcd_gfx_write_string((nodelist + adr)-> | ||
- | // Seadista | + | // Confederate |
- | // antud juhul esimesele | + | // in this case to first [0] |
- | // (mis porti kasutab, kust võtab adr) | + | // (What port is using, where takes the address) |
zigbee_set_destination(port, | zigbee_set_destination(port, | ||
- | // Joonistab ekraanile infot | + | // Displays info on LCD |
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
- | 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 |
- | + | // wait 1 millisecond for button | |
- | // Oota nuppu 1 millisekund | + | |
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 numbri | + | //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 167: | Line 166: | ||
} | } | ||
- | // 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)); | ||
return button_nr; | return button_nr; | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== TH Suvekool ==== | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | //Lisame ZigBee mooduli toe | ||
- | #include < | ||
- | |||
- | // USART liidese määramine ZigBee jaoks | ||
- | usart port = USART(1); | ||
- | |||
- | // LEDid ja nupud määrata | ||
- | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
- | pin buttons[3] = { PIN(C, 2), PIN(C, 1), PIN(C, 0) }; | ||
- | |||
- | // Teiste leitavate zigbee moodulite aadresside massiiv | ||
- | zigbee_node_t nodelist[6]; | ||
- | |||
- | |||
- | int main(void) | ||
- | { | ||
- | | ||
- | |||
- | // seadista LEDide ja nuppude I/O pordid | ||
- | for (int i=0; i<3; i++) | ||
- | { | ||
- | pin_setup_output(leds[i]); | ||
- | pin_setup_input(buttons[i]); | ||
- | } | ||
- | |||
- | // USART liidese seadistamine ZigBee side jaoks | ||
- | usart_init_async(port, | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | |||
- | |||
- | // Otsib ümbrusest teisiZigBee mooduleid | ||
- | // täidab nodelist massiivi leitud moodulite aadressidega | ||
- | pin_set(leds[0]); | ||
- | |||
- | zigbee_find_nodes(port, | ||
- | // võtab aega | ||
- | pin_set(leds[1]); | ||
- | // Seadista ZigBee saatma infot valitud ZigBee nodele, | ||
- | // (mis porti kasutab, kust võtab adr) | ||
- | zigbee_set_destination(port, | ||
- | // | ||
- | |||
- | //Kustuta LED-id -> ühendus olemas | ||
- | pin_set(leds[2]); | ||
- | |||
- | // Lõputu tsükkel moodulite andmetevahetuse näitamiseks | ||
- | while (true) | ||
- | { | ||
- | // vaata kas mõni nupp on alla vajutatud | ||
- | for (uint8_t i=0; i<3; i++) if (!pin_get_value(buttons[i])) button = i; | ||
- | |||
- | // kui nuppu on vajutatud | ||
- | if (button != -1) | ||
- | { | ||
- | //////////////////// | ||
- | } | ||
- | } | ||
} | } | ||
</ | </ |