This shows you the differences between two versions of the page.
| pt:examples:communication:zigbee [2015/12/09 17:10] – Criação deste novo documento. artica | pt:examples:communication:zigbee [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== ZigBee ====== | ||
| + | |||
| + | // | ||
| + | |||
| + | ===== Teoria ===== | ||
| + | |||
| + | [{{ :: | ||
| + | |||
| + | ZigBee é uma especificação para protocolos de comunicação de alto nível usando sinais de rádio digital de baixa potência baseados na norma IEEE 802.15.4-2003 para redes de área pessoal. ZigBee foi desenhado para dispositivos que requerem redes sem fios simples que não necessitem de alta taxa de transferência de dados. Estes dispositivos podem ser, por exemplo redes de sensores, displays de informação, | ||
| + | |||
| + | Baseado em módulos sem fio ZigBee podem ser configuradas conexões directas entre dois dispositivos ou redes mais complicadas. Na figura seguinte uma rede de sensores de medição para a transferência de interface do utilizador é ilustrada. | ||
| + | |||
| + | ===== Prática ===== | ||
| + | |||
| + | A placa de comunicação do Robotic HomeLab tem um conector para módulos sem fio, incluindo módulo ZigBee XBee da Maxtream. A comunicação entre o módulo e o controlador é realizada através da interface UART. Em primeiro, o dispositivo é configurado para o modo de API enviando o comando " | ||
| + | |||
| + | O código de exemplo em cima está à procura de outros dispositivos ZigBee nas próximidades e encontrou dispositivos de endereços de dispositivo que são exibidos no visor do módulo LCD User Interface Homelab. O endereço global 0000FFFF tem um significado especial - informação enviada para este endereço é recebida por todos os dispositivos disponíveis. No código de exemplo, depois de pesquisar outros dispositivos um é ligado e os dispositivos iniciam o envio entre eles de eventos de premir botão, mostrando o resultado no LED do outro dispositivo. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | //Adding ZigBee module support | ||
| + | #include < | ||
| + | |||
| + | // Determination of the USART interface | ||
| + | usart port = USART(1); | ||
| + | |||
| + | // LED and button pin setup | ||
| + | pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; | ||
| + | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | ||
| + | |||
| + | // Max numbers of ZigBee modules in network + broadcast address +1 | ||
| + | #define ZIGBEE_MAX_NODES 4 | ||
| + | |||
| + | int8_t wait_button(uint16_t ms); | ||
| + | |||
| + | // Address array of other Zigbee modules found in network | ||
| + | zigbee_node_t nodelist[ZIGBEE_MAX_NODES]; | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | uint8_t adr = 0; | ||
| + | |||
| + | // LED and buttons I/O ports configuration | ||
| + | for (int i=0; i<3; i++) | ||
| + | { | ||
| + | pin_setup_output(leds[i]); | ||
| + | pin_setup_input(buttons[i]); | ||
| + | } | ||
| + | |||
| + | // Configuration of USART interface for communication whit ZigBee module | ||
| + | usart_init_async(port, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | // LCD display initalization | ||
| + | lcd_gfx_init(); | ||
| + | |||
| + | // Clear LCD | ||
| + | lcd_gfx_clear(); | ||
| + | |||
| + | // Turning back light ON | ||
| + | lcd_gfx_backlight(true); | ||
| + | |||
| + | //Wait until other ZigBee modules are searched from area near by | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | // Searching other ZigBee modules | ||
| + | // fills nodelist array with found modules info | ||
| + | zigbee_find_nodes(port, | ||
| + | |||
| + | // | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | // Displays the list of found modules on LCD | ||
| + | // (on what line to write, where takes addr., how many max) | ||
| + | zigbee_lcd_show_nodes(2, | ||
| + | hw_delay_ms(3000); | ||
| + | lcd_gfx_clear(); | ||
| + | |||
| + | // Displaying connecting on LCD | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | //Displays only 8 last digit form the address | ||
| + | lcd_gfx_write_string((nodelist + adr)-> | ||
| + | |||
| + | // Confederate ZigBee to send info for chosen ZigBee module' | ||
| + | // in this case to first [0] | ||
| + | // (What port is using, where takes the address) | ||
| + | zigbee_set_destination(port, | ||
| + | |||
| + | // Displays info on LCD | ||
| + | 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; | ||
| + | } | ||
| + | </ | ||