Things used in this project

Hardware components:
R8326274 01
Raspberry Pi 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 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

Contact
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

PHPoc Blue to Control Door Knob by MQTT Protocol and Cortana
Intermediate
  • 106
  • 7

Programming a PHPoc Blue board to control a servo motor to rotate the knob of a door using MQTT Protocol through voice command.

Face Detection Using OpenCV With Raspberry Pi
Intermediate
  • 3,369
  • 32

Protip

In this project, we are going to detect faces using OpenCV with Raspberry Pi.

Creating an IoT Server with Home Assistant and MQTT
Intermediate
  • 3,783
  • 21

Protip

This guide will help you set up Home Assistant. Access it from any web browser and automations.

Automated GPS Controlled Photo Taker
Intermediate
  • 1,786
  • 20

For this build, I put together a backpack to take pictures when I am close to places that I like; especially in London.

DIY Smart Assistant Speaker/Lamp (Google Home or Alexa)
Intermediate
  • 2,016
  • 5

Full instructions

DIY build using 3D printed enclosure and parts, a recycled Bluetooth speaker, a Raspberry Pi, and a Philips Hue light bulb.

Cat Door with Pet Recognition
Intermediate
  • 4,694
  • 22

Full instructions

Use object detection to let your cat in and out of the house with a motion-activated pet door.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login