This demo shows you how XBee library can be used with FEZ Hydra. Hardware that was used in this example:
Below is the connection diagram generated by Visual Studio:
And here is a picture of how it all looks in reality:
Here is a list of steps performed by the demo application:
using System.Collections; using System.Text; using Microsoft.SPOT; using NETMF.OpenSource.XBee.Api; using NETMF.OpenSource.XBee.Api.Common; using XBee = NETMF.OpenSource.XBee.Api.XBee; namespace Gadgeteer.Tester { public partial class Program { private const string Ping = "PING"; private const string Pong = "PONG"; private ArrayList _nodes; void ProgramStarted() { lED7R.TurnLightOn(7, true); coordinator.Configure(); coordinator.Api.DataReceived += OnDataReceived; endDevice.Configure(); endDevice.Api.DataReceived += OnDataReceived; router.Configure(); router.Api.StatusChanged += (x, s) => OnStatusChanged(s); router.Api.DataReceived += OnDataReceived; Debug.Print("Coordinator config: " + coordinator.Api.Config); Debug.Print("Router config: " + router.Api.Config); Debug.Print("End device config: " + endDevice.Api.Config); } private void OnStatusChanged(ModemStatus status) { if (status == ModemStatus.Associated) DiscoverNodes(); } private void DiscoverNodes() { _nodes = new ArrayList(); router.Api.DiscoverNodes(node => { var nodeInfo = node.NodeInfo; if (_nodes.Contains(nodeInfo)) return; _nodes.Add(nodeInfo); PrintNode(_nodes.Count, node); // depending on XBee settings discovery may return local node info if (nodeInfo.SerialNumber != router.Api.Config.SerialNumber) router.Api.Send(Ping).To(nodeInfo).NoResponse(); }); } private void PrintNode(int nodeNumber, DiscoverResult info) { Debug.Print("#" + nodeNumber + " - " + info); lED7R.TurnLightOn(nodeNumber); } private void OnDataReceived(XBee receiver, byte[] data, XBeeAddress sender) { var dataStr = new string(Encoding.UTF8.GetChars(data)); switch (dataStr) { case Ping: receiver.Send(Pong).To(sender).NoResponse(); break; case Pong: Debug.Print("Received Pong from " + sender); break; } } } }
Generated output:
Using mainboard GHIElectronics-FEZHydra version 1.0 Coordinator config: ApiMode: EnabledWithEscaped, HardwareVersion: Series 2, Firmware: 21A0, SerialNumber: 0013A20040692938, NodeIdentifier: ' ' Router config: ApiMode: EnabledWithEscaped, HardwareVersion: Series 2, Firmware: 23A7, SerialNumber: 0013A2004086AD32, NodeIdentifier: ' ' End device config: ApiMode: EnabledWithEscaped, HardwareVersion: Series 2 Pro, Firmware: 29A7, SerialNumber: 0013A200406E92A6, NodeIdentifier: ' ' #1 - Router, address=173F, serial=0013A2004086AD32, id=, parent=FFFE #2 - End device, address=1B0F, serial=0013A2004086AD25, id=, parent=C3C9 #3 - Coordinator, address=0000, serial=0013A20040692938, id=, parent=FFFE Received Pong from 0013A20040692938 #4 - End device, address=7D81, serial=0013A200406E92A6, id=, parent=C3C9 Received Pong from 0013A200406E92A6 #5 - Router, address=C3C9, serial=0013A20040476061, id=CONNECTPORTX4, parent=FFFE #6 - Router, address=C306, serial=0013A2004060F30B, id=WALLROUTER, parent=FFFE