Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Carey Payette
Published © GPL3+

ZigBee communication with the Pi 2 and Windows IoT Core

This project shows you how to implement ZigBee communication on Windows IoT Core

IntermediateFull instructions provided15,453
ZigBee communication with the Pi 2 and Windows IoT Core

Things used in this project

Hardware components

ZigBee S2 Modem
×2
ZigBee Adapter Kit
×2
FTDI Serial TTL-232 USB cable
×1
USB to UART TTL conversion module
×1
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1

Hand tools and fabrication machines

Computer

Story

Read more

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

Carey Payette

Carey Payette

13 projects • 134 followers
Sr. Software Engineer at Independent
Thanks to Falafel Software.

Comments