This is an old revision of the document!


Table of Contents

On preparation

ZigBee

The necessary knowledge: [HW] Controller module, [AVR] USART, [LIB] Serial Interface,
[LIB] Graphic LCD

Theory

XBee moodul.

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. It's designed for devices which require simple wireless networking and don't need high data transfer rate. Does devices might be for example information displays, home- and industrial automatic systems. The selforganise network from ZigBee the power demand compared with different Bluetooth application is quit efficient, modules goes quickly form sleep to awake, the awake time is minimized. ZigBee works radio band at 2.4 GHz.

In practice

Homelab's communication board has a place for connecting ZigBee's module called “Xbee”. Communication between it and the controller takes place using controller's UART interface. The device's mode is changed with the command “+++” to AT mode, further configuration is done with standard AT command. If communication between two modules has been established, then is returned to data transfer.

This example code above is designed to search the surroundings for other Zigbee devices and with a help of a sub function the address's of these other devices are displayed on Homelab user interface display. Global address 0000FFFF has a special properties- info sent to this address is available for all devices which are available on the network. For example connection is made with a desired device after which can be control each other LED-s state by pressing buttons each other.

#include <homelab/usart.h>
#include <homelab/module/lcd_gfx.h>
#include <homelab/delay.h>
#include <homelab/pin.h>
 
//Adding ZigBee module support
#include <homelab/module/zigbee.h>
 
// 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;	//Acquired module order list
 
    // 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,
                     USART_DATABITS_8,
                     USART_STOPBITS_ONE,
                     USART_PARITY_NONE,
                     USART_BAUDRATE_ASYNC(9600));
 
    // 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, 0);
    lcd_gfx_write_string("  ZigBee demo");
 
    lcd_gfx_goto_char_xy(0, 1);
    lcd_gfx_write_string("Searching...");
 
    // Searching other ZigBee modules
    // fills nodelist array with found modules info
    zigbee_find_nodes(port, nodelist, ZIGBEE_MAX_NODES);
 
    //lcd_gfx_clear();
    lcd_gfx_goto_char_xy(0, 1);
    lcd_gfx_write_string("Found:  ");
 
    // Displays the list of found modules on LCD
    // (on what line to write, where takes addr., how many max)
    zigbee_lcd_show_nodes(2, nodelist, ZIGBEE_MAX_NODES);	
    hw_delay_ms(3000);
    lcd_gfx_clear();
 
    // Displaying connecting on LCD
    lcd_gfx_goto_char_xy(0, 0);
    lcd_gfx_write_string("Connecting...");
    lcd_gfx_goto_char_xy(0, 1);
    //Displays only 8 last digit form the address
    lcd_gfx_write_string((nodelist + adr)->address64l);
 
    // Confederate ZigBee to send info for chosen ZigBee module's node,
    // in this case to first [0]
    // (What port is using, where takes the address)
    zigbee_set_destination(port, &nodelist[adr]);
 
    // Displays info on LCD
    lcd_gfx_goto_char_xy(0, 0);
    lcd_gfx_write_string("  ZigBee demo");
 
    lcd_gfx_goto_char_xy(0, 1);
    lcd_gfx_write_string("Button1: LED1");
 
    lcd_gfx_goto_char_xy(0, 2);
    lcd_gfx_write_string("Button2: LED2");
 
    // Hoia meeles eelmist nuppu selleks, et vältida olukorda,
    // kus nuppu all hoides tuleb sada nupuvajutusi järjest.
    // Algatuseks olgu see -1 ehk ükski nupp pole all.
    int8_t previousButton = -1;
 
    // Lõputu tsükkel moodulite andmetevahetuse näitamiseks
    while (true)
    {
        int8_t button;	//muutuja nupuvajutuse registreerimiseks
 
        // Oota nuppu 1 millisekund
        button = wait_button(1);
 
        // Kui eelmises tsüklis oli nupp üleval ja nüüd on all
        if (previousButton == -1 && button != -1)
        {
            // teisenda nupu indeks liites A täht
	    // ja saada teisele moodulile
            // A täht on esimene nupp, B täht teine nupp jne
            usart_send_char(port, 'A' + button);
        }
 
        // Loe USART'st
        if (usart_has_data(port))
        {
            // loe bait, teisenda leds massiivi indeksiks
	    //ja muuda väljundit.
            pin_toggle(leds[usart_read_char(port) - 'A']);
        }
 
        // Jäta meelde mis nupp oli hetkel alla vajutatud
        previousButton = button;
    }
}
 
// Ootab nupu vajutust ms millisekundit.
//Kui tuleb nupu vajutus, tagastab vastava nupu järjekorra numbri
int8_t wait_button(uint16_t ms)
{
    // Vaikimisi -1 tähendab, et ükski nupp pole vajutatud.
    int8_t button_nr = -1;
    uint16_t counter = 0;
    do
    {
        // vaata kas mõni nupp on all
        for (uint8_t i=0; i<3; i++)
        {
            if (!pin_get_value(buttons[i]))
            {
                button_nr = i;
                break;
            }
        }
 
        // oota 1 millisekund
        hw_delay_ms(1);
 
        // suurenda millisekundi loendurit
        counter++;
    } while (button_nr == -1 && (counter < ms));
 
    return button_nr;
}
en/examples/communication/zigbee.1334585578.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