ZB 03: Send packets
This program shows how to send packets to a gateway indicating the MAC address of the receiving XBee module.

Required Materials

1 x Waspmote 1 x Battery 1 x MiniUSB wire 1 x XBee-ZigBee module

Notes

- The coordinator must be turned on - The battery has to be connected. - This example can be executed in Waspmote v12

Code

1
/*
2
* ------ [ZB_03] - send packets to a gateway --------
3
*
4
* Explanation: This program shows how to send packets to a gateway
5
* indicating the MAC address of the receiving XBee module.
6
*
7
* Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see .
22
*
23
* Version: 0.2
24
* Design: David Gascón
25
* Implementation: Yuri Carmona
26
*/
27
28
#include <WaspXBeeZB.h>
29
#include <WaspFrame.h>
30
31
// Destination MAC address
32
//////////////////////////////////////////
33
char RX_ADDRESS[] = "0013A200403A2A49";
34
//////////////////////////////////////////
35
36
// Define the Waspmote ID
37
char WASPMOTE_ID[] = "node_01";
38
39
40
// define variable
41
uint8_t error;
42
43
44
45
void setup()
46
{
47
// init USB port
48
USB.ON();
49
USB.println(F("Sending packets example"));
50
51
// store Waspmote identifier in EEPROM memory
52
frame.setID( WASPMOTE_ID );
53
54
// init XBee
55
xbeeZB.ON();
56
57
delay(3000);
58
59
//////////////////////////
60
// 2. check XBee's network parameters
61
//////////////////////////
62
checkNetworkParams();
63
64
}
65
66
67
void loop()
68
{
69
///////////////////////////////////////////
70
// 1. Create ASCII frame
71
///////////////////////////////////////////
72
73
// create new frame
74
frame.createFrame(ASCII);
75
76
// add frame fields
77
frame.addSensor(SENSOR_STR, "new_sensor_frame");
78
frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
79
80
81
///////////////////////////////////////////
82
// 2. Send packet
83
///////////////////////////////////////////
84
85
// send XBee packet
86
error = xbeeZB.send( RX_ADDRESS, frame.buffer, frame.length );
87
88
// check TX flag
89
if( error == 0 )
90
{
91
USB.println(F("send ok"));
92
93
// blink green LED
94
Utils.blinkGreenLED();
95
96
}
97
else
98
{
99
USB.println(F("send error"));
100
101
// blink red LED
102
Utils.blinkRedLED();
103
}
104
105
// wait for five seconds
106
delay(5000);
107
}
108
109
110
111
/*******************************************
112
*
113
* checkNetworkParams - Check operating
114
* network parameters in the XBee module
115
*
116
*******************************************/
117
void checkNetworkParams()
118
{
119
// 1. get operating 64-b PAN ID
120
xbeeZB.getOperating64PAN();
121
122
// 2. wait for association indication
123
xbeeZB.getAssociationIndication();
124
125
while( xbeeZB.associationIndication != 0 )
126
{
127
delay(2000);
128
129
// get operating 64-b PAN ID
130
xbeeZB.getOperating64PAN();
131
132
USB.print(F("operating 64-b PAN ID: "));
133
USB.printHex(xbeeZB.operating64PAN[0]);
134
USB.printHex(xbeeZB.operating64PAN[1]);
135
USB.printHex(xbeeZB.operating64PAN[2]);
136
USB.printHex(xbeeZB.operating64PAN[3]);
137
USB.printHex(xbeeZB.operating64PAN[4]);
138
USB.printHex(xbeeZB.operating64PAN[5]);
139
USB.printHex(xbeeZB.operating64PAN[6]);
140
USB.printHex(xbeeZB.operating64PAN[7]);
141
USB.println();
142
143
xbeeZB.getAssociationIndication();
144
}
145
146
USB.println(F("\nJoined a network!"));
147
148
// 3. get network parameters
149
xbeeZB.getOperating16PAN();
150
xbeeZB.getOperating64PAN();
151
xbeeZB.getChannel();
152
153
USB.print(F("operating 16-b PAN ID: "));
154
USB.printHex(xbeeZB.operating16PAN[0]);
155
USB.printHex(xbeeZB.operating16PAN[1]);
156
USB.println();
157
158
USB.print(F("operating 64-b PAN ID: "));
159
USB.printHex(xbeeZB.operating64PAN[0]);
160
USB.printHex(xbeeZB.operating64PAN[1]);
161
USB.printHex(xbeeZB.operating64PAN[2]);
162
USB.printHex(xbeeZB.operating64PAN[3]);
163
USB.printHex(xbeeZB.operating64PAN[4]);
164
USB.printHex(xbeeZB.operating64PAN[5]);
165
USB.printHex(xbeeZB.operating64PAN[6]);
166
USB.printHex(xbeeZB.operating64PAN[7]);
167
USB.println();
168
169
USB.print(F("channel: "));
170
USB.printHex(xbeeZB.channel);
171
USB.println();
172
173
}
Copied!

Output

1
E#
2
Sending packets example
3
4
Joined a network!
5
operating 16-b PAN ID: D27C
6
operating 64-b PAN ID: 84D87D34C4AE4CB1
7
channel: 0B
8
send ok
9
send ok
10
send ok
11
...
Copied!
Last modified 2yr ago