developer maker musician

Sunday, June 12, 2016

How to Monitor Azure IoT Hub Traffic

You have a couple different options to monitor your incoming and outgoing Azure IoT Hub traffic.

Desktop App

On Windows machines, you can use the Azure IoT Hub Device Explorer utility. Under the Data tab, select your device, click Monitor and you’ll start to see messages in the Event Hub Data

image

Node

You can also use the iothub-explorer npm package.

npm install -g iothub-explorer@latest

and then

iothub-explorer hubconnectionstring monitor-events deviceid


image


Jon

Thursday, June 9, 2016

Fix for Azure Stream Analytics–Duplicate property names are not allowed

If you see the error “Duplicate property names are not allowed ‘avg’ in your Stream Analytics query it is likely because you aren’t setting up an alias for that property.

Here’s what you may see:

imageimage

Here’s how to fix it:  Just as an alias for each.

image


Jon

Tuesday, June 7, 2016

How to Read All Accelerometer, Gyroscope, Barometer and Compass Data from the GY801, GY80 aka 9-Axis Inertial Navigation Module for Arduino aka IMU 10DOF L3G4200D+ADXL345+HMC5883L+BMP180

I’m hacking on project that requires an accelerometer.  I picked up this 9-Axis Inertial Navigation Module for Arduino from my local Vetco. That board is actually a GY801 (the newer version of the GY80). It has a gyroscope (L3G4200D), accelerometer (ADXL345), compass (HMC5883L) and a barometer (BMP180) (BMP085 on the GY80) all on one board.

image


Hooking this up to an arduino is simple, just connect:

  • 3V to 3.3V
  • GND to GND
  • SDA to SDA
  • SCL to SCL


I searched around a bit for some sample code to get all the data from each component and found this i2cdevlib GitHub repo that has sample sketches for every component included in the GY801.  I combined all of the components included in the GY80 and GY801 boards into one sample sketch, which can be found here https://github.com/jrowberg/i2cdevlib/pull/244/files.  (I’ll update links if my PR is merged)


To get started quickly, just clone my fork, copy all the folders in the Arduino folder to your arduino/libraries folder and open up the GY80_raw.ino file and deploy.  After you do that you should see output in your serial monitor like this:

image

Jon

Saturday, June 4, 2016

How To Build a Real-Time IoT Dashboard with Azure IoT Hub, Azure Stream Analytics and Power BI

Azure provides a set of services that allow you to visualize IoT device data in real-time.  The data pipeline is: IoT Device –> Azure IoT Hub –> Azure Stream Analytics –> Power BI.  Follow along with this post if you want to build out a simple version to get your started. Please leave a comment if you have any issues along the way.

Here’s the end result…trust me, it updates in real-time once you build this out.

image



Azure IoT Hub

Follow my “How to Get Started with Azure IoT Hub and a UWP App” to get the IoT Hub setup and the code that will run on your emulated IoT device.  When you setup the IoT Hub, choose the S1 option instead of free if you want to write more than 8k messages a day.


After following that post you should have UWP IoT Device <–> Azure IoTHub working.


Now, let’s modify the device code to write more than a simple string to the IoT Hub.


IoT Device Code

This is the code that will run on the emulated IoT Device.  It will write random JSON Serialized messages to your IoT Hub. The data model is simple a single “Participant” class with “Position” and “Count” properties.

public class Participant
{
     public int Position { get; set; }
     public int Count { get; set; }
}

1. Clone this repo: https://github.com/jonbgallant/UWPIoTDevice

2. Open the DeviceKey.txt file and paste in your device connection string from the Azure Portal.  (I don’t do much UWP dev and couldn’t quickly figure out how they want us to store config settings like this, so I just dropped it in a text file.  LMK if you know a better way to manage config settings)

3. Compile and move onto next step.


Azure Stream Analytics

Stream Analytics will take messages from IoT Hub and push them to Power BI.  There are three things you need to configure: input, query and output.

1. Go to the old Azure Portal: https://manage.windowsazure.com 

2. Create a new Stream Analytics

image

image

3. Connect the “input” to the IoT Hub you created earlier.  Choose “Data stream”, “IoT Hub”, “JSON” and “UTF8”

image

4. Connect the “output” to Power BI. You will need to authenticate to Power BI.

image

image

5. Write a query that connects your IoT Hub input to your Power BI output.

6. Click the Save button.

7. Start the Stream Analytics job.




Power BI

Stream Analytics will automatically create the Power BI dataset after you send the first message to it. 


1. Open up the UWPIoTDevice project you cloned earlier and send a single message.

2. Go to http://app.powerbi.com. You should see that the Power BI dataset has been automatically created.


image

3. Click on the dataset, add the position and count fields.

image

You should now see something like this:

image

4. Go back to the UWP app and send some more messages, then come back to Power BI and click Refresh.  You should now see something like this:

image

Modify the appearance however you’d like.  I just added data labels to mine

image

5. Click “Save” in the upper right hand corner and give the report a name:


VERY IMPORTANT: Save the report before pinning the visual to the dashboard.


imageimage

6. Click on the “Pin” icon on that visual and pin it to a dashboard.

imageimage

7. Open your dashboard

image

8. Open the UWPIoTDevice app and send some more messages

image

You should now see the dashboard being updated in real-time as you send more messages to it.


Jon

Friday, May 27, 2016

How to Get Started with Azure IoT Hub and a UWP App

Here’s a quick post on how to get setup with Azure IoT Hub with a UWP App.  These steps and code taken from Olivier Bloch’s Build Talk:

1. Create a new Azure IoT Hub – Free Tier – in Azure Portal http://portal.azure.com

image

Once that is created, grab the “iothubowner” connection string – primary key value.  (Click Settings –> Shared Access Policies –> iothubowner)

image

2. Install the Azure IoT Hub Device Explorer Windows app – you’ll use this to view and send messages that come and go through the IoT Hub.

image

Enter the iothubowner connection string from the last step and click update.

image


3. In VS2015, create a new UWP app

image

Click OK here:

image

Enable Developer Mode if prompted to do so.

image

image

3. Add a connection to your IoT Hub via References –> Add Connected Service

image

Click “Find more services…” at the bottom.

image

Find “Connected Service for Azure IoT Hub” and install it.

image

You should now see “Azure IoT Hub”

image

Sign-in and find your IoT Hub instance you created earlier.

image

Add a new Device.

image

Once you make that connection, you will now see the device in the Device Explorer Management tab and the Azure Portal Devices blade.

imageimage


The Connected Service will add an AzureIoTHub file to your project.  It has helper methods for sending and receiving IoT Hub messages.

image

Open up MainPage.xml and drop a button and textblock onto the design surface.

image

Open MainPage.xml.cs and add this code – receive and send messages to the IoT Hub vai the AzureIoTHub helper.

--

public MainPage()
{
     this.InitializeComponent();

    Task.Run(
         async () => {
             while (true) {
                 var message = await AzureIoTHub.ReceiveCloudToDeviceMessageAsync();
                 await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => {
                     textBlock.Text += Environment.NewLine + message;
                 });
             }
         }
         );
}

private void button_Click(object sender, RoutedEventArgs e)
{
     Task.Run(async () => { await AzureIoTHub.SendDeviceToCloudMessageAsync(); });
}

--


Compile and Run the VS Project.


Open the Data tab in Device Explorer and click the Monitor button

image

When you click the Button in your UWP app, you’ll see data in the Device Explorer Data tab

image


When you want to send a message to the device, go to the Device Explorer, Messages To Devices tab, enter your message and click Send.

image


That should get you going with a very basic connection between a UWP and IoT Hub.


Jon