Table of Contents
- Table of Contents
- Bill of Materials
- Add the Azure IoT SDK to Your Project
- Define the Azure IoT SDK Components
- Send Ambient Light Measurements Once per Second
- Run the Application
- Send Messages Based on Button Presses
- Run the Application and Get Pushy
- Conclusion & Next Steps
Bill of Materials
What you will need:
- The ThingLabs Thingy™ created in the ‘ThingLabs Thingy’ lab.
- The Visual Studio Project for the Thingy created in the ‘ThingLabs Thingy’ lab.
Add the Azure IoT SDK to Your Project
In a previous lab, you built the ThingLabs Thingy™ for Windows 10 IoT Core. Now it’s time to add the I to your IoT solution. In this lab, you will send messages from the Thingy to the Microsoft Azure IoT Hub you created in the previous lab. There is an Azure IoT SDK for C# that enables all of the connection and communications with your Azure IoT Hub. The SDK is available as a NuGet package that you can easily add to your project.
- Open the Thingy project.
- Click on the Project menu and select Manage NuGet Packages.
- Use the search field to search for Microsoft.Azure.Devices.Client.
- Click on the Install button to install the package.
Define the Azure IoT SDK Components
To use the Azure IoT SDK you will modify the StartupTask.cs file in the Thingy project. You will add the definitions for the SDK components, such as
DeviceClient (the client-side agent), the
Message, and the connection string for your specific device.
- Open the StartupTask.cs file.
- Add the following to the
usingstatements where you have added the other
Next, define a variable to represent the
- Locate the section in the StartupTask.cs file indicated with the comment
/**** Constants and Variables ****/
- Add the following variable definition:
The last step of the previous lab was to copy the device-specific connection string for that device. If the connection string is still in your copy buffer, simply paste it into the IOT_HUB_CONN_STRING field in Step 2 below. If the connection string is no longer in your copy buffer, you can get the device-specific connection string again by selecting it in the DeviceExplorer Devices list by right-clicking and selecting Copy connection string for selected device.
- In the same section of the StartupTask.cs file (indicated with the comment
/**** Constants and Variables ****/) and add the code below, making changes as indicated here:
- Use the device-specific connection string as the value of
- Use the name of the device you created in Azure IoT Hub as the
- Use any string value you’d like as the
Send Ambient Light Measurements Once per Second
The Thingy enables you to collect multiple sensor measurements (ambient light, sound, and LED state which is controlled by a button press).
- In the
Run(IBackgroundTaskInstance taskInstance)locate the code that creates the
deferralinstance (it should be the first line of code in that method).
- Immediately after the creation of the
deferralobject, add the following (the
deferralcode is added here for reference):
- Still in the
Run(IBackgroundTaskInstance taskInstance)method, add the following code after the
timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(200));code.
Throughout this lab you will use a feature in Visual Studio called light bulbs. Light bulbs are a new productivity feature in Visual Studio 2015. They are icons that appear in the Visual Studio editor and that you can click to perform quick actions including refactoring fixing errors. Light bulbs bring error-fixing and refactoring assistance into a single focal point, often right on the line where you are typing. As you write the code in this lab you will add calls to methods that don’t yet exist. The editor will indicate this to you by putting a red “squiggle” underline beneath the method call. When you hover over the offending code a light bulb will appear and you can expand it to see options for generating the missing method.
Use the Visual Studio light bulb feature to create the MessageTimer_Tick() event handler.
Each time the MessageTimer ticks this event handler will invoke the SendMessageToIoTHubAsync() method. Use the Visual Studio light bulb feature to create the SendMessageToIoTHubAsync() method. Modify the method signature to mark it as an async method.
With this method, you construct a JSON message payload once every second for the ambient light measurement and send it to your Azure IoT Hub. The communication with the Azure IoT Hub is managed by the
deviceClient object from the Microsoft.Azure.Devices.Client namespace.
Run the Application
Now you can run the application on your Raspberry Pi 2 and you will see the log of messages being sent to Azure IoT Hub at a rate of once per second.
Send Messages Based on Button Presses
For the ambient light measurement, you are sending a message once per second regardless of whether the light has changed. For the button, we’ll send a message based on an explicit state change (i.e. a button press event or release event).
- Locate the
Timer_Tickmethod (the one from the previous lab that is used to collect sensor data).
- Modify the
Timer_Tickmethod as follows:
If you want to compare your code with the master lab code, you can find it in the ThingLabs - Thingy4Windows Github repo here.
Run the Application and Get Pushy
Run your application again. Once you see the ambient light messages being sent, push the button a few times. You should see LED state messages go by. (You’ll also hear an annoying sound from the buzzer.)
Monitor the Messages Being Received by the Azure IoT Hub
Using the Device Explorer utility for Windows you installed in the previous lab, you can monitor the messages being received in Azure IoT Hub.
- Open the Data tab.
- Select the device from the drop-down list.
- Click Monitor to begin monitoring messages as they come into your Azure IoT Hub.
Conclusion & Next Steps
Congratulations! In this lab, you updated the Thingy application to send messages to Azure IoT Hub. The core concepts you’ve learned are:
- Using the Azure IoT SDK to connect to Azure and send device-to-cloud (D2C) messages.
- Sending messages as a continuous stream (e.g. once per second from a continuously measuring sensor).
- Sending messages based on events (e.g. exceeding measurement thresholds or explicit events like a button push).
At this point, nothing interesting is happening in the cloud with that data you are sending to Azure. It is simply being persisted for a default amount of time (1-day) and then being dropped. In the next lab, you will create a web application to visualize the data.