MegunoLink supports XBee networks with a coordinator plugged into your computer. The coordinator is the root of a XBee mesh network providing a gateway for many nodes to communicate with MegunoLink.
MegunoLink supports communication with a coordinator node in API 2 mode. API mode provides more robust communication with individual nodes than transparent serial mode. It helps ensure the payload from individual nodes doesn’t get mixed up. You can send data for time-plots, XY-plots, tables or any MegunoLink visualizer simultaneously from many nodes without it getting lost or garbled.
Although XBee coordinators use a serial port for communicating with the host computer, you need to create a XBee connection, not a serial connection, so MegunoLink can apply the special decoding required to retrieve the payload for XBee networks running in API mode. If your XBee network is running in transparent mode, you should use a normal serial connection. In transparent mode, serial data from different nodes can be mixed together preventing MegunoLink decoding messages properly.
To create a XBee connection:
- Open a connection manager visualizer
- Choose XBee Series 2 Pro from the Add Connection drop-down menu
MegunoLink will create a configuration panel for the new XBee connection:
The title box (top-left corner) lets you give the connection a name. This is useful to keep track when you have multiple sources: serial connections to each node as well as the coordinator connection, for example. The name you enter here will be displayed on the Connect button shown in visualizers.
The Port, Baud rate, Advanced and Reset controls let you configure and control the serial port that that coordinator is connected to. See Serial/ USB Connections for more information.
The Node Disc button initiates node discovery. This instructs the coordinator query the nodes attached to the network. Each active node responds to node discovery with its address. MegunoLink queries each node for its node identifier (
NI register). You can change the node identifier using the XBee manager visualizer.
The address and identifier of nodes that respond are added to the drop-down list next to the node-discovery button. Any messages you send with MegunoLink, from the monitor or interface panel visualizers, for example, are sent to the node selected in the drop-down list. MegunoLink will receive and process messages from any node.
Click the Connect button to open the connection and begin receiving data. Only one program can use the port at any time so MegunoLink will show an error if another program (XCTU, for example) already has the port open.
Sending Data through an XBee Network
This sketch demonstrates sending for plotting through a XBee network. It is setup for two nodes to generate and send sinewaves to the coordinator for plotting in MegunoLink. The sinewaves are just convienent test data; you could use measurements collected from a sensor or any other data that you’d like to see plotted.
The test network contained two nodes —
Node 1 and
Node 2 each configured as a router — and a coordinator node, which was plugged into the computer running MegunoLink. Each node generates a sine wave and sends values through the XBee network every 200 ms for MegunoLink to plot. Node 1 generates a sinewave with a frequency of 0.4 Hz; node 2’s sinewave is at 0.5 Hz.
Each time a node sends a message through the XBee channel, it also writes to the serial port. This is often useful for debugging multi-node networks. So I’ve also opened a serial connection to each node and added a monitor visualizer for each node so we can see the debugging information.
The MegunoLink interface built for this example has (working clockwise):
- a connection manager with:
- 1 XBee Series 2 Pro source to a coordinator. This connection receives the data for plotting sent over the XBee network
- 2 standard serial connections, one for each node. This lets us see diagnostic information from the nodes that can help troubleshooting wireless
- A monitor showing the data received by the XBee coordinator
- A plot showing data received from the XBee two nodes (blue for node 1, red for node 2)
- Two monitor windows, one for each of the XBee nodes. These are connected to the serial ports for debugging and show the number of messages each node has sent
This example sketch demonstrates sending data from two nodes over a XBee network, for plotting. The same approach can be used to send debug messages, table data etc. To run this example you’ll need the following libraries:
- xbee-arduino, for communicating with XBee radios in API mode by Andrew Rapp
- Software Serial, to use pins D2 and D3 for serial communication with the XBee
- PString, a light-weight string formating library by Mikal Hart
- MegunoLink Arduino Library, to format the message for plotting data
You’ll need to make sure your XBee radios are running in API mode 2 (mode 2 escapes special bytes) at 9600 baud. Change the
#define NODE 1 to
#define NODE 2 for the second node. This configures the nodes to use different series names for the graphs and generate different frequency sine waves to make things more interesting.
const int XBeeRxPin = 2;
const int XBeeTxPin = 3;
#define NODE 1
#if NODE == 1
const char * NodeName = "Node 1";
const float Frequency = 0.04; // Sine frequency [Hz]
#elif NODE == 2
const char * NodeName = "Node 2";
const float Frequency = 0.05; // Sine frequency [Hz]
XBeeAddress64 DestinationAddress(0,0); // 0,0 => Coordinator.
SoftwareSerial XBeeSerial(XBeeRxPin, XBeeTxPin);
int MessageCounter = 1;
Serial.println("XBee Communication Demo");
float seconds = (float)millis()/1000;
double dY = sin(2 * 3.141 * Frequency * seconds);
PString Payload(PayloadBuffer, sizeof(PayloadBuffer));
TimePlot Plot("", Payload);
ZBTxRequest TxMsg(DestinationAddress, (uint8_t*)PayloadBuffer, Payload.length());