Background
Do you ever get into a situation that whenever you trying to look for something but you spend very long time to look for it and still unable to find it? By leveraging RFID technology, using RFID reader and multiple RFID tags attached to the item, we can easily locate the item we stored. Whenever we bought a new item simply paste a RFID tag onto it and register item details into system. Once we need to look for the item but can’t recall where it is, simply make a search on mobile application and you will know it is currently at which room of your house.
The whole idea
The components of this project will consist of a UHF RFID reader module, a UHF RFID antenna, an Arduino UNO, a Raspberry Pi2 running Windows10 IoT core ( you may refer here http://ms-iot.github.io/content/en-US/GetStarted.htm if you're not sure how to install Windows10 IoT core), a mobile application and Azure web service for data storage. There will have multiple UHF RFID module in the house, and each UHF RFID module will be connected to Arduino UNO with Arduino will then connected to Raspberry Pi2. Whenever a item which would like to be tracked by system, a RFID tag will be attached. The detail of the item and the RFID value are required to register into system. The data of every record will be stored into Azure SQL database through Azure Web API. Whenever user is going to search an item at home, but he/she is not sure the item will be located in which room of his/her house. He/she can turn on their mobile application. The mobile application will send the search request to Raspberry Pi2 through Wifi network. Once Raspberry Pi2 received the search request, it will send the request over to all Arduino Uno connected to it. Arduino UNO will send a read command over to UHF RFID module. Once any of the UHF RFID reader at different location manage to capture the required RFID, it will return a value back to Arduino UNO. Arduino UNO will return the RFID reader location back to Raspberry Pi2 and RaspberryPi2 will further process the search result and return to mobile application.
if you
How to do?
I'm going to develop a background app running on Windows 10 IoT core, we may need to know the IP address of Raspberry Pi 2. It isn't necessary to have a display. Thanks to the almighty Windows IoT Core Watcher. All you need is to connect your Raspberry Pi2 model B as shown below:
By connecting PC and Raspberry PI2 over a network cable, the almighty Windows IoT Core Watcher will be able to tell you what's the IP address of your Raspberry Pi2:
Create a project in Visual Studio 2015
Then you can start developing using Visual studio 2015, simply download the codes that i shared here, and run it using Visual studio 2015.
Download the code from github and you will be able run the project directly. If you are trying to create a new project, you may try this way:
1. Go to File -> New->Project
2. Under Visual C# templates, look for "Background Application (IoT)". Key in your project name your project is created.
3. Once project has been created, next is to configure debug setting. Right click on project and select "Properties"
4. Under project's properties window, click on debug tab and make the following setting:
- Platform : ARM
- Target Device: Remote Machine
- Remote machine: <your ip address displayed in Iot Core Watcher>
- Uncheck "Use authentication"
Save the configuration and you can now start coding into StartupTask.cs file inder your newly created project.
Debugging in Visual Studio 2015
Once you'd completed coding and would like to start debugging, you may follow the following steps and screenshots:
1. Click on left hand side grey bar to set breakpoint at the line of code which intend to investigate:
2. Next click on the green "Remote machine" button to start debug:
3. Once entered debug mode, the green button "Remote Machine" will be changed to "Continue" at the same time step buttons will be enabled:
- Next -- Show next statement
- Step Into -- Step into function and look into codes inside function
- Step Over -- Step over current line and look into the next line of code
- Step Out -- Exit from the function and continue debugging with the previous code.
Continue button with "Next", "Step Into","Step Over" and Step Out"
While running debug , you may either rollover mouse cursor to inspect certain value of the variable or hightlight and right click on the variable and select "Add Watch" in order to add the variable into watch list:
Create Web API onto Azure to serve IoT and mobile app transactions
Create Asp.NET Web Application Project
1. Create an ASP.NET Web Application, key in project name and click OK button at bottom
2. On the next screen click Choose MVC under ASP.NET 4.5.2 template (if you are adventurous, feel free to try ASP.NET 5 preview Template with MVC6 framework. The step will be same)
3. Next set the authentication method to "No authentication"
4. Also note that if you wish to host this Web API onto a cloud server, you may check the "Host in the cloud" option as showed below:
If you don't have a microsoft azure account, you may sign up for free trial and enjoy a $200 free credit. Simply click here: https://azure.microsoft.com/en-us/pricing/free-trial/
5. If you choose to host your web API onto Azure Web App, once "OK" is clicked, the next screen will need you to login to Microsoft Azure.
6. Once logged in successfully, simply enter your web app name and subsequent detials as showed in the form:
once filled in all information, click OK and you wait for awhile for VS2015 to create web app and database on your Azure account.
Upon both local and cloud Web App are created successfully, next you will probably wish to create a database for your app. Thanks yo Entity framework on MVC, we can now setup our database for our Web App with a few simple steps.
Using Entity Framework
I am going to use NuGet Package Manager Console for the installation. So firstly you will need to lauch your Nuget Package Manager as below: Tools->NuGet Package Manager->Package Manager Console
2. Next type command "Install-Package EntityFramework" into your Package manager console below:
3. Next you will need to create a Model class to describe your database table and fields. Simply right click on Model folder and click "Add"->"Class.."
Once the new class is created, add the following lines:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
Upon creation of model class, next step is to create your WebAPI controllers together with action using Entity framework. Follow the print screen below, right click on Controllers folder and select "Add"->"New Scaffolded Item":
Select "Web API 2 Controller with actions, using Entity Framework" under Web API category on the next screen:
Once "Add" button is clicked, the following screen will be showed:
Click on the '+' button at Data context class field to create a new data context class:
Click Add in the next screen to create:
once all the fields are filled, click "Add" to generate the scaffolded item:
Upon successful generated scaffolded item, you will be able to see the controller and data context class drop into Controllers folder and Models folder:
Next, run the Enable-Migrations command in Package Manager Console:
After Enable-Migrations command, follow by Add-Migration <Name> command:
Finally Update-Database command will create a database base on the migration script generated from Models. You will be able to find the newly created database under Add_Data table:
Publish your WEB API
Once database has setup successfully, the web api is considered done (Scaffolded controller will generated basic function for RESTful call). Just to ensure those GetMything(GET), PostMything(POST), PutMything(PUT) and Deletemything(DELETE) functions are exist in MyThingsController.
Once Web API project has completed. Next you may publish web API onto Azure Web APP with the following steps:
1. Select Build menu > Publish MyThingAPI as following screenshot.:
2. The following pop up will show and allow you to Select a publish target. You may select Microsoft Azure API Apps(Preview) if you are using ASP.NET5 MVC 6 template for your Web API:
3. Once Microsoft Azure Web Apps has clicked, the following screen will show and that will need you to login your azure account:
Key in your azure credentials and login:
4. Upon successful login, you will be able to retrieve your existing web app hosted in azure (it should be created earlier while creating this project in VS2015):
5. Once web Apps has been selected, and clicked "OK", the below screen will show:
The connection setting will be propolated, simply click next to proceed
6. Under setting tab you will see the database connection string, other info will be populated automatically but make sure both checkboxes "Use this connection string at runtime" and "Execute Code First Migration" are checked:
6. Once all the settings are filled, click Publish and Visual studio will launch the Azure Web App immediately upon successful publish:
Once web api is published, you may test the REST API. I am using Chrome's extension "Advance REST Client" for testing:
You may try the below json string for POST call:
{
id:1,
status: "0",
rfid: "1009622005390623",
image: "",
name: "some tool",
type: "tool",
location: "bedroom",
createdAt: "2015-10-13T10:26:01.14",
lastUpdatedAt: "2015-10-13T10:26:01.14"
}
Create Windows Universal App
Since Iot Background app and REST API are ready, next is to build a Windows Universal App as client. Universal App allow us to use one set of codes for several devices. I create a Universal App for Tablet as an example here (you can simply change the layout setting for mobile phone or laptop).
To create a Universal App, simply click File>New>Project in your Visual Studio 2015, then select universal app as follow:
Once created new solution you may replace the cs scripts and .xaml files with MyThingApp which i shared in GitHub.
Debugging in Windows universal App:
Windows universal app provided several debug configuration includes ARM, x64 and x86 platform. If you are running debugging in your local PC which is running Windows 64 bit, you will need to chooose x64 and click the green button (Select "local machine")
if your app is running without error, it should look something like this:
Setup Notification Hub and UWP push notification
1. Once Web API and Universal App are done, you may login here
https://dev.windows.com/en-us/dashboard/Application/New to reserve an App name.
2. Once app name has been created, go back to MyThingApp and right-click on your project. Select Store>Associate App with the Store
for creation of notification hub please refer here:
3. next add the following function into App.xaml.cs
private async void InitNotificationsAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var hub = new NotificationHub("notification", "Endpoint=sb://smartwardrobe.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=MlHoVfn1Ub/c2ThUha+Crn7hHSGmqofrW3cJ6IK89Ok=");
var result = await hub.RegisterNativeAsync(channel.Uri);
}
also call this new function inside OnLaunched function:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
InitNotificationsAsync();
Finally, add this line of code into MainPage.cs, MainPage() Function:
this.NavigationCacheMode = NavigationCacheMode.Required;
4. Finally, setup a threading task inside MyThing Web API > MyThingController with the following codes:
public class NotificationClass {
internal async System.Threading.Tasks.Task<ThreadStart> SendNotificationAsync(string rfid)
{
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString("<your connection string>", "<hub name>");
var toast = @"<toast launch=""" + rfid + @"""><visual><binding template=""ToastText01""><text id=""1"">New Mything need to update</text></binding></visual></toast>";//<param>/MainPage.xaml?value=test</param>
await hub.SendWindowsNativeNotificationAsync(toast);
return null;
}
}
and the below codes upon successful creation of new record:
NotificationClass notClass = new NotificationClass();
Thread notificationThread = new Thread(async x => { await notClass.SendNotificationAsync(myThing.rfid); });
notificationThread.Start();
5. Upon successful setup, a toast notification as below will be received upon successful created record:
6. click on the notification and it will launch MyThingApp which installed in your PC:
clicked "OK" and you will see the new RFID is added into entry form. Fill in the rest of infomation and update.
REFERENCE LINKS:
1.Step by step guide to setup Windows10 Iot Core:- http://ms-iot.github.io/content/en-US/GetStarted.htm
2.Setting up Visual Studio 2015 on your PC:- http://ms-iot.github.io/content/en-US/win10/SetupPCRPI.htm
3.Step by step instruction to setup Windows10 IoT Core on Raspberry Pi2:- http://ms-iot.github.io/content/en-US/win10/SetupRPI.htm
4.How to develop IoT application (Blinky sample):- http://ms-iot.github.io/content/en-US/win10/samples/Blinky.htm
5.Windows IoT serial sample:- http://ms-iot.github.io/content/en-US/win10/samples/SerialSample.htm
6.Ms-IoT GitHub repositories:- https://github.com/ms-iot
7.Windows on GitHub:- http://microsoft.github.io/windows/
8.Arduino Software Serial Example(using UART) :- https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
9.SparkFun serial communication tutorial: - https://learn.sparkfun.com/tutorials/serial-communication
10.Get started with Azure Notification Hub:- https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-get-started/
11.Add push notifications to your Mobile Services app:- https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-android-get-started-push/
12.Code First Migrations:- https://msdn.microsoft.com/en-sg/data/jj591621.aspx
13.Publish to an Azure Web App using Visual Studio:- http://docs.asp.net/en/latest/tutorials/publish-to-azure-webapp-using-vs.html
14.Publish the App to Azure Azure App Service:- http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-10
Comments