Things used in this project

Hardware components:
R8326274 01
Raspberry Pi 2 Model B
×1
ZigBee S2 Modem
×2
ZigBee Adapter Kit
×2
FTDI Serial TTL-232 USB cable
×1
USB to UART TTL conversion module
×1
R8326274 01
Raspberry Pi 2 Model B
×1
Hand tools and fabrication machines:
Computer

Schematics

HelloZigBee
No schematic necessary, explained in post

Code

Serial Communication Capability Plain text
In the solution explorer, right click and select to View Code of the Package.appxmanifest file. Add the following capability to the Capabilities element in this file, this will allow us to use Serial communications in this project:
<DeviceCapability Name="serialcommunication">
 <Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
Open MainWindow.xaml,Plain text
and add the following markup within the Grid element (I’ve changed my designer to preview the application as a 10” tablet (1024×768 at 100%):
<StackPanel Orientation="Vertical">
 <TextBlock FontSize="50" x:Name="txtHeader" >HELLO ZigBee</TextBlock>
 <Button x:Name="btnConnect" Click="btnConnect_Click" 
 Margin="0,0,0,15" Width="78" Height="80" >Connect</Button>
 <TextBox Height="50" x:Name="txtToSend" FontSize="25"></TextBox>
 <Button x:Name="btnSend" Click="btnSend_Click" 
 Margin="0,15" Height="60" Width="60">Send</Button>
 <Button x:Name="btnRead" Click="btnRead_Click" 
 Width="60" Height="60">Read</Button>
</StackPanel>
Open MainWindow.xaml.cs, and add a few private fields to the MainPage class:Plain text
 //using Windows.Devices.SerialCommunication; 
 private SerialDevice _zbCoordinator = null;
 //using Windows.Storage.Streams;
 private DataWriter _dataWriter = null;
 private DataReader _dataReader = null;
Next let’s initialize our desired startup state of the MainPage form.Plain text
Add the following code after the InitializeComponent call in the MainPage constructor:
btnConnect.IsEnabled = true;
btnSend.IsEnabled = false;
btnRead.IsEnabled = false;
Now let’s deal with how to connect to the ZigBee modem from a program running on the Pi board. Plain text
ZigBee modems communicate as a serial device. So we will need to find all serial devices attached to the board. This is made easy with the functions available in the Windows.Devices.SerialCommunication namespace. Add the following button click event handler to the MainPage class to handle connecting to the ZigBee modem:
private async void btnConnect_Click(object sender, RoutedEventArgs e)
{
 string deviceQuery = SerialDevice.GetDeviceSelector();

 //using Windows.Devices.Enumeration
 var deviceInfo = await DeviceInformation.FindAllAsync(deviceQuery);

 if (deviceInfo != null && deviceInfo.Count > 0)
 {
//your board name may differ
//introspect the return value of deviceInfo to 
//determine the serial hardware you have your modem attached to
var serialBoardName = "CP2102 USB to UART Bridge Controller";
var zbInfo = deviceInfo.Where(x => x.Name.Equals(serialBoardName).First();
_zbCoordinator = await SerialDevice.FromIdAsync(zbInfo.Id);

// Configure serial settings
_zbCoordinator.WriteTimeout = TimeSpan.FromMilliseconds(1000);
_zbCoordinator.ReadTimeout = TimeSpan.FromMilliseconds(1000);
_zbCoordinator.BaudRate = 9600;
_zbCoordinator.Parity = SerialParity.None;
_zbCoordinator.StopBits = SerialStopBitCount.One;
_zbCoordinator.DataBits = 8;

btnConnect.IsEnabled = false;
btnSend.IsEnabled = true;
btnRead.IsEnabled = true;
}
else
{
this.txtHeader.Text = "Something went wrong :( -- Device not found";
}
}
Next we will add methods to our class that deal with reading and writing to the ZigBee modem. Plain text
First let’s create an internal class to help us encapsulate a return value based on the read/write operations. Add the following internal class to the MainPage class:
internal class ZigBeeCommResult
{
public bool IsSuccessful { get; set; }
public string TextResult { get; set; }

public ZigBeeCommResult()
{
this.IsSuccessful = false;
this.TextResult = "";
}
}
Next, let’s define the serial read and write methods. Plain text
Add the following code to the MainPage class:
//using System.Threading.Tasks
private async Task<ZigBeeCommResult> Read()
{
var retvalue = new ZigBeeCommResult();
try
{
_dataReader = new DataReader(_zbCoordinator.InputStream);
var numBytesRecvd = await _dataReader.LoadAsync(1024);
retvalue.IsSuccessful = true;
if (numBytesRecvd > 0)
{
retvalue.TextResult = _dataReader.ReadString(numBytesRecvd).Trim();
}
}
catch (Exception ex)
{
retvalue.IsSuccessful = false;
retvalue.TextResult = ex.Message;
}
finally
{
if (_dataReader != null)
{
_dataReader.DetachStream();
_dataReader = null;
}
}
return retvalue;
}

private async Task<ZigBeeCommResult> Write()
{
ZigBeeCommResult retvalue = new ZigBeeCommResult();
try
{
_dataWriter = new DataWriter(_zbCoordinator.OutputStream);
//send the message
var numBytesWritten = _dataWriter.WriteString(txtToSend.Text);
await _dataWriter.StoreAsync();
retvalue.IsSuccessful = true;
retvalue.TextResult = "Text has been successfully sent";
}
catch (Exception ex)
{
retvalue.IsSuccessful = false;
retvalue.TextResult = ex.Message;
}
finally
{
if (_dataWriter != null)
{
_dataWriter.DetachStream();
_dataWriter = null;
}
}
return retvalue;
}
Now let’s wire up the button click event handlers for the read and write operations,Plain text
add the following code to your MainPage class:

Send and Read button event handlers
private async void btnSend_Click(object sender, RoutedEventArgs e)
{
var writeResult = await Write();
txtHeader.Text = writeResult.TextResult;
}

private async void btnRead_Click(object sender, RoutedEventArgs e)
{
var readResult = await Read();
txtHeader.Text = readResult.TextResult;
}
HelloZigBee
ZigBee Communication with Windows IoT Core

Credits

Photo
Carey Payette

Sr. Software Engineer at Falafel Software

Thanks to Falafel Software.

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

WalabotEye - Object Tracker with Haptic Feedback
Intermediate
  • 997
  • 9

Full instructions

For the hard of sight, use this to have a better understanding of the world around you.

End-Effector and Control Logic for Robot
Intermediate
  • 3,201
  • 24

For my thesis, I have developed the control system and end-effector for a robot, easily implementable and economic.

Theremino Adapter for CNC.
Intermediate
  • 287
  • 7

Full instructions

Attention: This adapter is used to replace the parallel port with the USB, and not to operate Mach3 CNC or Linux.

Smart Mirror (with Optional Alexa)
Intermediate
  • 18,772
  • 92

Full instructions

A low cost and easy to build smart mirror with off the shelf parts.

Theremino Logger
Intermediate
  • 163
  • 4

Datalogger is simple to use but with great performance. The base, concise and intuitive.

Theremino - Access to Slots “WriteSlot”  “ReadSlot”.
Intermediate
  • 44
  • 3

Full instructions

A description of how to communicate with the slots of Theremino System by simply calling the functions: “WriteSlot” and “ReadSlot”.

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback