The UWP app allows you to read the data of Azure with Windows Internet Of Things services like: Azure IoT Hub, Azure IoT Suite, Azure Notification Hub, ...
How it worksFirst, we will look at the ASP.NET app first. You use the Postman app which allows you to submit the data from the app to the ASP.NET.
NOTE: Publish your app before using it and set a breakpoint at the Post code. It can find in Post() Task and look like this:
var collectionLink = UrlFactory.CreateDocumentCollectionUrl(databaseName, collection)
Go back to Postman and enter your web address as you publish it to Azure and choose Post => Builder => Body => Text => raw and enter this command:
{
"EventType": true,
"EventTime": "<DATE>T<TIME>"
}
Replace <DATE> at the code above to a date write like: yyyy-mm-dd and the time is hh:mm:ss.
NOTE: For month, date, hour, minute and second from 1 to 9, enter 0 before then the number.
Run your app and send the code above to your ASP.NET app. You will see the breakpoint is hit and you make sure the data it read before the breakpoint stopped the app is correct. If you see the data is like: "{{"EventType":true "EventTime": "<DATE>T<TIME>"}}" , this means your app is working property. You also see the notification: "Someone has entered the store".
NOTE: The <DATE> and <TIME> data depending of your code you write on Postman.
Second, we will look at the UWP app.
First, we still attach the service have your ASP.NET app.
Set the Sensors to Input because it will send the data to it, not the Raspberry Pi send it's data to the sensors and the LEDs to Output because LEDs cannot have any data to send back to your Raspberry Pi.
In the ExitSensor_ ValueChanged()
and EntrySensor_ValueChanged()
, set a breakpoint at throw new NotImplementedException();
code. When it's have a data is Someone entered or Someone exited, you will see the breakpoint is hit and your app will stop. Modify the code to make sure it's like this (For exit):
private void ExitSensor_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
{
if(args.Edge == GpioPinEdge.FallingEdge)
{
exitLight.Write(GpioPinValue.Low);
exit = true;
if(enter)
{
CustomerHasEntered(true);
exit = false;
enter = false;
return;
}
}
else
{
exitLight.Write(GpioPinValue.High);
}
}
NOTE: Make sure you removed the breakpoint.
Add the code CustomerHasEntered()
like this:
private void CustomerHasEntered(bool v)
{
throw new NotImplementedException();
}
Copy the code above to the EntrySensor_ValueChanged()
.
Modify the code of the EntrySensor_ValueChanged()
to make sure it's correct.
private void EntrySensor_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
{
if(args.Edge == GpioPinEdge.FallingEdge)
{
entryLight.Write(GpioPinValue.Low);
enter = true;
if(exit)
{
CustomerHasEntered(false);
exit = false;
enter = false;
return;
}
}
else
{
entryLight.Write(GpioPinValue.High);
}
}
Remove the throw new NotImplementedException()
from the CustomerHasEntered()
and modify the code like this:
private void CustomerHasEntered(bool v)
{
}
Set a breakpoint at the { place of CustomerHasEntered()
to make sure it's working property.
Deploy the app and then cover the Entry sensor to the Exit and you will see the breakpoint is hit, the value of v is false and if you do the Exit to Entry, you will see v is true.
Now remove the breakpoint and do somethings interest with this application! I will tell you at the end of this writing.
Modify the code like this:
private void CustomerHasEntered(bool v)
{
TriggeredEvent tEvent = new TriggeredEvent
{
EventTime = DateTime.Now,
EventType = v
};
HttpContent content = new StringContent(tEvent.ToString(), Encoding.UTF8, "application/json");
client.PostAsync("api/event", content);
}
Come back to Postman and choose JSON (application/json) in the body.
Set the breakpoint in the { place for reading status and } place for showing the message and deploy it.
If v is true, the breakpoint will hit and then show it's true, continue the debugger. You will see "Someone has entered the store"
Go to your event document to see the time and type.
Go to Azure Dashboard and make changes to Type and Time and then click Save.
Hit Ctrl + F5 to deploy the code to your Pi without debugging your app.
Now look at the DataClient project and you will see the design of MainPage.xaml.
Go ahead if you want and deploy this code to your PC.
When it's deployed, smaller the design until it's like a phone. Scroll down to see all.
Now restart to see what's bad.
Find a element like NumPeopleInStore type 1 RT.
Find NumPeopleInStore and then add x:DeferLoadStratery="Lazy"
to the chart and stop it. Go to MainPage.xaml.cs and then make sure it's have Element NumPeopleInStore. Replace the code this.FindName("CurrentPeopleInStore")
to this.FindName("NumPeopleInStore")
in the MainPage_Loaded()
.
You will shout: "I NEVER EVER WANTED!" if you see the numbers on the screen. HATE THAT!
Stop this app and deploy again. If you go Entry or Exit, it will show how many people in the shop.
Video
Comments