In the last article , the main features of ZigBee wireless technology were discussed. In this part we will talk about how to quickly start working with this technology in practice. For this purpose, the ETRX357 modules were selected, which have built-in firmware that allows you to work with network functions and control analog and digital peripherals using a set of AT commands. Also, the article will discuss in more detail the issues related to the types of devices in the ZigBee network and network security. At the end, we will collect a data collection network that will receive temperature information from several wireless devices.
Let's get acquainted with the selected hardware platform. The ETRX357 modules are based on the EM357 chip from Silicon Labs. This microcircuit is a system-on-a-chip — the Cortex-M3 processor core — a RF transceiver operating at 2.4 GHz.
ETRX357 modules are available in 4 versions:
Experimental measurements of the communication range for the ETRX357 and ETRX357-LRS modules in direct visibility conditions:
Below is a summary table of the main characteristics of the module:
Specifications | Description |
---|---|
CPU core | ARM Cortex-M3 |
RF transceiver | 2.4 GHz |
Flash / RAM volume, Kb | 192/12 |
power usage | TX: 42 mA @ +8 dBm |
RX: 26.5 mA | |
"Dream": 400 nA | |
Working voltage range | 2.1 - 3.6 V |
Operating temperature range | -40 ... + 85 ° C |
More detailed information on the technical characteristics can be obtained in the documentation for the modules [ 1 ].
All modules come with firmware from the manufacturer, and to get started, just power up the module, connect the TxD and RxD lines of the UART serial interface, through which the module communicates with AT commands. All teams can be divided into 2 types:
Also, the standard firmware includes a set of functions, which allow in some cases to do without an external microcontroller or control device. A complete list of commands can be found in the manual [ 2 ], only the most frequently used ones are listed below. We will need some of the commands in this list to create our data collection system.
Team | Description |
---|---|
AT + PANSCAN | Scan the air for available networks |
AT + EN | Networking |
AT + JN | Connect to any available network |
AT + DASSL | Leave current network |
AT + DASSR: <device address> | Send a command to the remote device to exit the network |
Team | Description |
---|---|
AT + UCAST: <device address> = <message> | Sending an address message |
AT + MCAST: <max. the number of relays>, <group ID>, <message> | Networking |
AT + BCAST: <max. number of retransmissions>, <message> | Connect to any available network |
AT + SCAST: <message> | Send data to Sink-site |
Maximum message length : 72 bytes.
Important : multicast messages are a special case of broadcast messages, so do not send them with a high repetition rate (the documentation indicates no more than 8 multicast / broadcast messages within 8 seconds). In addition, it is worth paying attention to the fact that multicast messages are not accepted by sleeping end devices.
Team | Description |
---|---|
ATSXX? | Reading the contents of the SXX register |
ATSXX = YYYY | Writing to the SXX register value YYYY |
ATREMS: <device address>, XX? | Reading the contents of the remote node's SXX register |
ATREMS: <device address>, XX = YYYY | Writing to the SXX register of the remote node the value YYYY |
Bitwise access to embedded S-registers is possible. To do this, the read / write command, after XX, you need to specify the number of the bit to be addressed. For example:
ATS001? # 1 S00 ATS001=1 # 1 1 S00 ATS1812? # 18 S18 ATREMS:0000,0AE? # 14 S0A 0000
We now turn to a more detailed consideration of the process of building a ZigBee network. We will work with the debug boards on which the mezzanine radio module and the ZigBee USB gateway are installed. There are several analog sensors on the debug boards, which will be discussed later. We will take readings from them and send them to the central data collection center, where all this will be processed. Step by step, we will create our data collection system and analyze the main difficulties that may arise in the process.
We describe the future data collection system:
Above, the AT + EN command was mentioned, which allows you to organize a network. The device to which this command was sent over the serial interface becomes the network coordinator. By default, the coordinator independently selects the most suitable frequency channel, generates a short and long network address.
AT+EN JPAN:24,949D,56370DE37E0DC0FF # 24 - # 949D - # 56370DE37E0DC0FF -
Below is a list of registers, the value of which affects the process of creating a ZigBee network:
Channel Mask - Register S00 . This mask determines which channels can be used. Recall, the ZigBee technology specification defines 16 frequency channels (numbering from 11 to 26). The register is 2 byte, so by setting 0/1, you can enable / disable the use of the corresponding channel. This setting is valid both for the coordinator when creating a network, and for all other devices that want to join an available network.
ATS00=0001 # 11 ATS00=FFFF #
Transceiver output power - register S01 . This parameter applies to the radio when creating a network or connecting to an available network, and is valid for the entire time the device is on the network. In the process of working in the ZigBee network, this parameter cannot be changed. The only way to do this is to leave the network, change the value in the register to a new one, leave the network and reconnect. All possible values that can be written to the register S01 are listed in the documentation [2].
ATS01=8 # +8 # ( ETRX357. LRS- # )
There is one important point for the coordinator: if you try to create a network with predefined identifiers, there is already a network with the same parameters, then matching identifiers will be generated automatically.
In ZigBee networks, a special node, the Trust Center, is responsible for security. The default coordinator is the Trust Center. A trust center can also be any router (FFD device), if the network was created in the mode of a distributed trust center (this will be discussed at the end of this chapter).
List of responsibilities of the Trust Center:
ZigBee networks use two keys:
To prevent the potential compromise of data transmitted over the network, you can use several options:
In this article we will look at the option with the Link Key preinstalled. The disadvantage of this approach is the need to write the appropriate configuration to all modules. But for our small network it is not critical. In the standard firmware of the modules, the ETRX357 Link Key can be set by writing the corresponding value to register S09 . In addition, the following bits must be set to 1 in the S0A register:
ATS09=ABCDEFFEDCBACBAFEDDEFABCBAFEDCFF:password # Link Key # :password - ATS0A8=1 ATS0A4=1 ATS0A2=1
Setting bits 2 and 4 on all nodes except the coordinator is an optional procedure. Advantages of this approach:
Let us point out another interesting possibility: in the S0A register, 9 bits are responsible for enabling a special function — create a network in the mode of a distributed Trust Center. In this mode of operation, any router (FFD device) can allow new devices to connect to the current network. Distributed Trust Center mode is used when deploying large networks to avoid delays in connecting all wireless nodes. After the network is deployed, a Trust Center is selected by AT + BECOMETC , which will manage security policy. To ensure the safety of the network in this mode of operation, it is also necessary to take care of the pre-installation / acquisition of the Link Key. Otherwise, until the Trust Center is selected, any ZigBee device will potentially be able to seamlessly connect to the network.
In the previous article , the main types of ZigBee devices were considered. Recall the main features and dwell on the types of end devices:
coordinator (COO) - the device that organized the network. Participates in the process of routing network traffic, performs the duties of the Trust Center (except in the case of the distributed Trust Center mode). Does not go into sleep mode, so this device must have a stationary power source
The listed devices act as parent nodes for the end devices. The maximum number of child nodes at a router or coordinator can reach 32. Parent devices are responsible for receiving and storing (*) messages for end devices that are connected to them. The end devices, in turn, communicate with the network through parents. Each time a new end device connects to the network or when the old one reconnects, a parent is defined for it, which makes an entry in a special table of child devices. This table stores the short and long address of the child node and its type. Consider the available types of end devices:
end device (ZED) - a device that does not participate in the network's work in routing traffic, connecting new devices, etc. For this type, the ZigBee technology specification does not provide for a sleep mode. Therefore, when describing a parent (*) it meant that they do not store messages for ZED devices. It is assumed that the ZED device always includes the receiver and the parent node sends it to the addressee immediately after receiving the address message. So these devices must also have a stationary power source.
Sleeping End Device (SED) - Network duties for this type of device are similar to the previous type. The only difference is in the support for sleep mode, which is announced in the ZigBee technology specification. Such devices spend most of their time in sleep mode and can operate on a battery-powered power supply for a long time. The definition of “long time” here depends on the application, but if we consider the example of a wireless ZigBee switch, the lifetime of such a device from one set of disk batteries can be 2-3 years [3]. Such devices come out of sleep mode only by some external event (by a timer interrupt, in order to digitize the sensor readings; by pressing a button, etc.) or when it is time to send a data packet. In order to receive messages from the parent node, sleeping nodes use the Polling mechanism . We will talk about it a bit later.
ZigBee Network Example
End devices are always connected via an FFD device, which at the time of connection had the best indicator of the quality of communication. In the process of work, a situation may arise when the router has failed or it has been taken for service. In this case, after several attempts to reconnect with the parent, the end device will start searching for a new parent node. In the figure, the connection between the mobile node and the parent is indicated by a dotted line. After a certain period of time, the MED device can join the network through another parent.
This mechanism is used for several purposes:
Mobile and end devices can thus receive messages that are stored for them in the parent node.
According to the ZigBee specification for sleeping end devices, address messages are stored by the parent ~ 7.68 seconds. Moreover, if the frequency of following address messages for SED / MED devices is higher than the specified value, then the messages will be overwritten. Therefore, to ensure reliable data delivery, the sleeping endpoint device must poll the parent node frequently enough. In this regard, there are two polling intervals for sleeping end devices: Short Poll and Long Poll intervals.
The Short Poll interval is used when waiting for confirmation of a successful transaction, for example, when sending a packet with data on sensor readings. Usually this interval is set to 1 second. After confirmation is received, the device goes into sleep mode and then the Long Poll interval is used.
Long poll interval is usually set in a fairly large - from several minutes to several days. This allows the sleeping end device to save space in the child table of the parent. A parent can delete the end device from its table if it has not been contacted in time. In this case, the end device that slept through its time must be reconnected to the network.
For more information about the Polling mechanism, see the ZigBee stack manual from Silicon Labs. In the standard firmware for ETRX357 modules, the parent node is polled by a timer / counter interrupt 0.
The basic theory is told, let's move on to practice. A module in Python will be used to work with radio modules ETRX357. All recommendations for its use are given in the accompanying README file.
We write in each node the appropriate configuration. All three devices — a USB gateway and two debug boards — were connected to a computer and each was configured accordingly:
#!/usr/bin/env python3 # coding=utf-8 import rfconf import serial.tools.list_ports def main(): # sensor_nwk.xml configurator = rfconf.ModuleConfigReader("sensor_nwk.xml") supp_nodes = configurator.get_avail_nodes() supp_nodes = dict(enumerate(supp_nodes)) if len(supp_nodes) > 0: print("Configuration XML file support {} nodes:".format(len(supp_nodes))) print(supp_nodes) # "Telegesis" for com in serial.tools.list_ports.comports(): if com.description.split()[0] == "Telegesis": tgmodule = rfconf.ModuleInterface(com.device) usr_choice = int(input("How would you like to configure that node: ")) tgmodule.set_node_type(supp_nodes[usr_choice]) tgmodule.write_config(configurator) else: print("There is nothing to do") if __name__ == "__main__": main()
<rfconfig> <node type="COO"> <!-- Link Key --> <reg name="S09" password="password" overwrite="y" type="string"> 12341234123412341234123412341234 </reg> <!-- --> <reg name="S0A" password="password" overwrite="n" type="hex"> 0114 </reg> <!-- --> <reg name="S01" overwrite="y" type="int"> 3 </reg> <!-- Sink- --> <reg name="S10" overwrite="n" type="hex"> 10 </reg> </node> <node type="SED"> <!-- Link Key --> <reg name="S09" password="password" overwrite="y" type="string"> 12341234123412341234123412341234 </reg> <!-- --> <reg name="S0A" password="password" overwrite="n" type="hex"> 0114 </reg> <!-- --> <reg name="S01" overwrite="y" type="int"> 3 </reg> <!-- Sink- --> <reg name="S10" overwrite="n" type="hex"> 100 </reg> <!-- PB5 --> <reg name="S15" overwrite="n" type="hex"> 2000 </reg> <!-- / --> <reg name="S13" overwrite="y" type="hex"> 00FF7EFB </reg> <reg name="S16" overwrite="y" type="hex"> 00008204 </reg> <!-- S16, --> <reg name="S17" overwrite="y" type="hex"> 00008204 </reg> <!-- / --> <reg name="S18" overwrite="y" type="hex"> 00FD58E7 </reg> <!-- S18, --> <reg name="S19" overwrite="y" type="hex"> 00FD58E7 </reg> <!-- / 7 --> <reg name="S37" overwrite="y" type="int"> 40 </reg> <reg name="S38" overwrite="y" type="hex"> 8110 </reg> <!-- --> <!-- 2 --> <reg name="S28" overwrite="y" type="int"> 0003 </reg> <!-- 0 1 (PA0) --> <reg name="S23" overwrite="y" type="hex"> 0001 </reg> <!-- 2 2 (PA1) --> <reg name="S24" overwrite="y" type="hex"> 0003 </reg> </node> </rfconfig>
A few comments about setting up sleeping end devices. In order to minimize the power consumption of the radio module, you must turn off all unused peripherals: the LED display and the power of the light sensor. In addition, all unused pins must be configured to be configured with a pull-up or exit. Several 32-bit registers are responsible for configuring I / O ports:
/ AT-[2]. , , , . , S15 13 1 ( PB5). LM61 .
, , , . . , . / 7 10 . / 7 S37. :
/ 7 S38 . 0110, /, Sink-. , , (S38F) S38 1. , – 8110. [3]. , Sink-.
ZigBee (PANID). 0000 . , , , (UART, SPI ..) - . – , ETRX357, , . Disadvantages of this approach:
, , ETRX357 — Sink- — . Sink-, 4 S10 . , , Sink, Sink, 8 S10 AT+SSINK Sink-. Sink- , , . , Sink-, . , Sink-, , . , , Sink- .
Sink AT+SCAST :
# Sink AT+SCAST:Hello # ( ) AT+UCAST:CAFE=Hello AT+UCAST:000D6F00024CBCCC=Hello
0, . - .
2 ( S28 ). , , 2-4, , UART. 0 , , . , UART ( . S11). 4 , . 1 2 ( PA0 PA1). :
# , 0 - 0001 # , 2 - 0003 # 2 ATS28=0003 # 0 1 ATS23=0001 # 2 2 ATS24=0003
:
, 1 , Polling-. , , . 1 . Polling- , .
, , . GitHub , examples , sensor_nwk.py . , . . . - , .
#!/usr/bin/env python3 # coding=utf-8 import rfconf import serial.threaded class TemperatureReader(rfconf.ETRXModuleReader): TEMP_POS = 2 NODEID_POS = 0 def handle_line(self, data): if not data: # return # : # SDATA: <Long ID>,<GPIO state>,<ADC0>,<ADC1>,<SeqNO>,<VCC> info, *payload = rfconf.response_split(data) if info == "SDATA": nodeid = payload[self.NODEID_POS] temp = int(payload[self.TEMP_POS], 16) # # LM61 temp = (temp / 10 - 600) / 10 print("Node: {} - Temperature: {:.2f}".format(nodeid, temp)) def leave_network(node): LEAVE_NWK_CMD = "AT+DASSL" node.write_command(LEAVE_NWK_CMD) resp = node.read_resp() # AT+DASSL "OK" # , "LeftPAN" if resp[-1] == "OK": msg = "" while not msg: msg = node.readline() print(msg) else: print(resp[-1]) def get_network_info(coordinator): NWK_INFO_POS = 1 GET_NWK_INFO_CMD = "AT+N" coordinator.write_command(GET_NWK_INFO_CMD) resp = coordinator.read_resp() if resp[-1] == "OK": nwk_info = resp[NWK_INFO_POS] *nused, ch, txpower, panid, longid = rfconf.response_split(nwk_info) print("network already created - ", end="") print("ch: {}, PANID: {}, EUI64: {}".format(ch, panid, longid)) def create_network(coordinator): NWK_INFO_POS = 1 NWK_PARAMS_POS = 1 CREATE_NWK_CMD = "AT+EN" coordinator.write_command(CREATE_NWK_CMD) resp = coordinator.read_resp() if resp[-1] == "OK": # nwk_info = resp[NWK_INFO_POS] info, ch, sid, lid = rfconf.response_split(nwk_info) print("network is created - ", end="") print("ch: {}, PANID: {}, EUI64: {}".format(ch, sid, lid)) else: # - , # get_network_info(coordinator) def main(): coo_node = rfconf.ETRXModule("<COMXX (Windows) /dev/<ttyUSBXX> (Linux)>", node_type="COO") reader = TemperatureReader(coo_node) # create_network(coo_node) ser = coo_node.get_serial_interface() reader_thread = serial.threaded.ReaderThread(ser, reader) # with reader_thread as protocol: reader_thread.join() if __name__ == "__main__": main()
, . ZigBee- . , Polling- ~250 . 1 , 4000 (~160 ). , . , .
PS Energy Profiler, Simplicity Studio , EFM32GG-STK3700 Advanced Energy Monitor. Application Note Silicon Labs. , STK3700, . SWO .
Source: https://habr.com/ru/post/306062/
All Articles