Getting started with PowerShell IoT on Raspbian (Raspberry Pi)

During a cleanup over the weekend I found a Raspberry Pi that wasn’t doing anything. I figured now that PowerShell Core had got to version 6.2.1 I should power it up and have a play around with maybe some sensors and outputs using PowerShell IoT. I ran into a couple of gotcha’s that took me some searching and head scratching to figure out, so I’m documenting them here for next time.

Installing PowerShell Core was straight forward. I followed the Microsoft Guide here and pretty quickly I was up and running with PowerShell Core 6.2.1.

PowerShell Core 6.2.1 on Raspbian.png

Enabling SPI and I2C Pins

By default on the latest Raspbian Stretch Lite build (that I installed – April 2019) you have to go into Preferences => Raspberry Pi Configuration and enable SPI and I2C if you are using displays etc.

GPIO Enable on Raspberry Pi for PowerShell IoT.png

GPIO Pins

Having built a number of projects with IoT Devices I thought I knew what GPIO Pins were and how to interact with them. However I quickly failed when trying to flip the outputs on GPIO pins using the Set-GPIOPin cmdlet from the Microsoft.PowerShell.IoT PowerShell Module. It turns out that the Microsoft PowerShell IoT Module utilises the Wiring Pi scheme for Pin numbering. Obviously having not done any IoT with a Raspberry Pi before I was not familiar with this underlying C Library. Therefore whilst I was trying to use the GPIO number 8 for example, what I really needed to be addressing was Pin 10.

The schematic below shows the 40 Pin GPIO connector with the WiringPi Pin numbers.

68747470733a2f2f6f63772e63732e7075622e726f2f636f75727365732f5f6d656469612f696f742f6c6162732f70696e732d72617370626572727970692e706e673f773d37303026746f6b3d333563383032.png

With that knowledge and a few Red Green Blue (RGB) LED’s wired to multiple GPIO outputs held high you can quickly get some nice colours.

IMG-0285.JPG

With the SSD1306 library you can quickly and easily output some text to an OLED display.

Note: After connecting the display you will need to reboot your Raspberry Pi to get the display showing your text. I didn’t spend too much time on it but I did notice that it didn’t have many capabilities like the SSD1306 library from Adafruit does for Python like fonts, text size, text position etc.

New-OledDisplay | Set-OledText -Value "pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1      pwsh Core 6.2.1"

IMG-0287.JPG

Summary

The basics are there to set GPIO pins high and low which in turn could then trigger a relay that activates something else. The available libraries and examples only extend to a few other sensors and output devices. It was fun to have a quick mess around and understand the capabilities, and I’m sure it would be possible to develop some interesting projects with it. With Winter now here, maybe there will be a rainy weekend soon and I’ll be inspired to build something.

CIRCUITPY Volume not visible for an Adafruit Circuit Playground Express on Windows 10 1803

I’ve just returned from the US and whilst there I picked myself up an Adafruit Circuit Playground Express. I plugged it into my computer and by all reports it should just appear as a removable drive on Windows 10. Alas that didn’t happen for me. I could get it into BOOT mode by pressing the Reset switch, but no matter what I did I couldn’t get the expected CIRCUITPY volume to be visible in File Explorer. Here’s how I got it working along with configuring VSCode as the editor.

Prerequisites

  • Visual Studio Code
    • Python Extension (from Microsoft) for VSCode. Install through VSCode Extensions

Environment Configuration

    1. Install the Arduino IDE from the Microsoft Store
    2. Open VSCode and install the Arduino Extension (from Microsoft)
      • go to the Command Palette and open Preferences: Open User Settings
      • under Arduino: Pathadd the path to Arduino. It should be something like C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt depending on the version you have
      • under Arduino: Additional Urlsselect edit in settings.json and add the following line and save the file
        • “https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
      • Open the Command Palette and select Arduino Board Config. Select Adafruit Circuit Playground
    3. Verify that the Adafruit Circuit Playground board is showing in the VSCode system tray along with the COM port is using on your computer.
    4. Update the Bootloader
      • Currently the latest bootloader is available from here https://github.com/adafruit/uf2-samdx1/releases/latest
        • for Circuit Playground Express you are looking for the update-bootloader-circuitplay_m0-v2.0.0-adafruit.VERSION.uf2 file
      • Click the reset button (in the middle of the board) and you should see the CPLAYBOOT drive volume in your computer File Explorer
      • Drop the bootloader uf2 file onto the drive
    5. Update to the latest Circuit Python Release
      • That is available from here https://circuitpython.org/board/circuitplayground_express/
        • Currently that is v3.1.2
      • Drop the updated Circuit Python .u2f file onto the CPLAYBOOT volume
    6. Restore the Demo Python Script
      • The demo is available from here https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/master/Introducing_CircuitPlaygroundExpress/CPXDemoContent.zip
      • Unzip the download and drop the demo script (main.py and *.wav files)  onto the CIRCUITPY volume
      • It will restart and go through the neopixel demo (with startup sound)
    7. Using VSCode open the main.py file from the CIRCUITPY volume
      • update the following line from False to True and save the file
        • TOUCH_PIANO =True

         

With that all done I was up and running.

Hopefully that helps someone else get quickly starting and avoid all the troubleshooting I went through to get up and running.

Update your Azure Sphere OS to 18.11 before Jan 15 2019

Azure Sphere

Update your Azure Sphere OS from 4.2.1 to 18.11 before January 15, 2019

Azure Sphere development kits became available in September of 2018. I had pre-ordered one and received it very quickly from Seeed Studio. I even wrote up my first impressions of it here.

On November 5 Microsoft announced that there was an OS update coming for the Azure Sphere development board the week of November 12.

After the release of 18.11, we encourage you to upgrade your device OS and SDK as soon as possible. After the release, devices that run the TP 4.2.1 release won’t receive any OTA updates for either device or application software. However, device authentication and attestation will continue to work to authenticate to Azure IoT Hub. TP 4.2.1 will continue to be supported until January 15, 2019. Thereafter, devices that are running the TP 4.2.1 OS won’t be able to authenticate to Azure IoT Hub.

Along with the change to the OS version naming, new Features were detailed here in the release notes;

    • Real-time clock (RTC). A Beta API enables applications to set and use the internal clock and leverages support for using a coin-cell battery to ensure the RTC continues to keep time when power is lost.
    • Mutable storage. A Beta API provides access to a maximum of 64k for storage of persistent read/write data.
    • External MCU update. A reference solution shows how your application can update the firmware of additional connected MCUs.
    • Private Ethernet. The MT3620 now supports connecting to a private, 10 Mbps network via the Microchip Ethernet part over a serial peripheral interface (SPI). This functionality allows an application running on the A7 chip to communicate with devices on a private network via standard Transmission Control Protocol (TCP) or User Datagram Protocol (UDP) networking. Stay tuned to the Azure Updates website for more information about this capability.
    • Beta API targeting. Beta APIs are still in development and may change in or be removed from a later release. Starting with this release, we make them available for testing and feedback so that you can get a head start on using new features. You can target applications for either the production APIs or the production and Beta APIs.

Upgrading Azure Sphere OS to 18.11

The upgrade process is manual from 4.2.1 to 18.11. The details are described in the update section of the release notes here. Essentially the process is;

  • Update your Azure Sphere SDK
  • Move the Azure Sphere device to a System Software Only Group
  • Upgrade the Azure Sphere OS
  • Verify the devices status

With the updated SDK installed, running the commands was quick and successful.

Move Device to System Only Group
Move Device to System Only Group
Azure Sphere OS Updated
Azure Sphere OS Updated

Summary

You’ve got 10 days left to update your Azure Sphere OS before it will no longer be able to connect to Azure IoT Hub. The update is quick and painless (unless you have dozens to update).

Azure Sphere – Initial Setup, Configuration and First Impressions

Azure Sphere

In April this year, Microsoft announced Azure Sphere. This was the same week as I’d be preparing for a presentation I was giving on Azure IoT at the Sydney location for the Global Azure Bootcamp. When pre-orders became available from Seeed Studio I naturally signed up as I’ve previously bought many IoT related pieces of hardware from Seeed Studio.

Fast forward to this week and the Azure Sphere MT3620 device shipped. It’s a long weekend here in Sydney Australia and delivery wasn’t due until after the long weekend, but by some miracle the packaged was delivered on the Friday by DHL after only leaving China 3-4 days earlier.

What a great opportunity then to un-box it, get it configured and build the sample “Hello World” (Blinky) project.

Getting Started

Following the “Get Started Guide” here I was straight away perplexed as to why Visual Studio was required, when I’ve made the complete transition to Visual Studio Code.

It seems there isn’t support in the IoT Workbench Extension in VS Code for the MT3620 yet.

Azure IoT Workbench.PNG

After patching and updating my now out-of-date Visual Studio installation I was finally able to install the VS Tools for Azure Sphere.

Azure Sphere VS Tools.PNG

which also comes with the TAP Driver for communicating with the device via the USB port, which is necessary for setup.

TAP Driver.PNG

With that all done it needs to be connected to Azure Active Directory. For that I created a new user for use with Azure Sphere in my Azure AD Tenant and then proceeded to login to Azure AD with that account.

azsphere login

azsphere login.PNG

Permissions.PNG

Successfully logged in (if you try with a Microsoft Account you’ll get a message indicating Azure AD is required), it prompts you to create an Azure Sphere Tenant.

Create Tenant

NOTE: Claiming the Device

Claiming the Device.PNG

With the Azure Sphere Device connected the Windows 10 computer you are executing the command from, as this is the first time setup an Azure Sphere Tenant needs to be created and the device claimed.

azsphere tenant create --name 
azsphere device claim

Claim Device.PNG

Connecting to Wifi

With the Azure Sphere Tenant created and the device claimed its time to connect it to Wifi.

azsphere device wifi show-status
azsphere device wifi add --ssid  --key

Connect Azure Sphere to Wifi.PNG

Checking the Wifi Connection Status after connecting provides the device connection status.

azsphere device wifi show-status

Azure Sphere Wifi Status.PNG

Checking the Azure Sphere OS Version against what is available shows it’s on the latest.

azsphere device show-ota-status

Azure Sphere OS Version.PNG

Blink Example Project

With the device now configured it was time to try out the sample project. Again following the instructions I first Enabled Debugging.

azsphere device prep-debug

Enable Azure Sphere Debugging.PNG

Following the example as per the Getting Started Guide I built the Blink Example project.

New Project Azure Sphere Blink Example.PNG

and ran it. It all worked as per the instructions. Pressing the A button with debugging enabled allow the state of the device (button) to be read and output.

Blink Example with Debugging.PNG

Summary

The setup was very quick, completely painless and just worked. So initial impressions are positive. My only gripe is that the Azure IoT Workbench Extension for VS Code doesn’t support the hardware. I’m hoping that comes soon.

Now to build something with it. What to build ……..

Using Azure Cognitive Services to Empower the IT Service Desk

Tonight (29 August 2018) I presented the following presentation on using Azure Cognitive Services to Empower the IT Service/build Business Applications to the Sydney Azure User Group.

I walked through how to leverage the following Azure Cognitive API Services;

  • Speech to Text
  • Text to Speech
  • Language Understanding Intelligent Service (LUIS)
  • Text Language Translation

For each I show a working example and demo.

I then walked through my Voice Assistant for Microsoft Identity Manager and how I integrated three of those services along with Azure IoT, Azure Serverless and Azure PaaS Services.

GitPitch Presents: github/darrenjrobinson/SydAzureUG-CognitiveServices

The Fastest Way from Idea to Presentation for everyone on GitHub, GitLab, and Bitbucket.

If you weren’t present I hope this presentation can still give you a start to integrating and leveraging Azure Services.

Find me on Twitter @darrenjrobinson

A Voice Assistant for Microsoft Identity Manager

This is the third and final post in my series around using your voice to query/search Microsoft Identity Manager or as I’m now calling it, the Voice Assistant for Microsoft Identity Manager.

The two previous posts in this series detail some of my steps and processes in developing and fleshing out this Voice Assistant for Microsoft Identity Manager concept. The first post detailed the majority of the base functionality whilst the second post detailed the auditing and reporting aspects into Table Storage and Power BI.

My final architecture is depicted below.

Identity Manager integration with Cognitive Services and IoT Hub 4x3
Voice Assistant for Microsoft Identity Manager Architecture

I’ve put together more of an overview in a presentation format embedded here.

GitPitch Presents: github/darrenjrobinson/MIM-VoiceAssistant/presentation

The Fastest Way from Idea to Presentation for everyone on GitHub, GitLab, and Bitbucket.

If you’re interested in building the solution checkout the Github Repo here which includes the Respeaker Python Script, Azure Function etc.

Let me know how you go @darrenjrobinson

Using your Voice to Search Microsoft Identity Manager – Part 2

Introduction

Last month I wrote this post that detailed using your voice to search/query Microsoft Identity Manager. That post demonstrated a working solution (GitHub repository coming next month) but was still incomplete if it was to be used in production within an Enterprise. I hinted then that there were additional enhancements I was looking to make. One is an Auditing/Reporting aspect and that is what I cover in this post.

Overview

The one element of the solution that has visibility of each search scenario is the IoT Device. As a potential future enhancement this could also be a Bot. For each request I wanted to log/audit;

  • Device the query was initiated from (it is possible to have many IoT devices; physical or bot leveraging this function)
  • The query
  • The response
  • Date and Time of the event
  • User the query targeted

To achieve this my solution is to;

  • On my IoT Device the query, target user and date/time is held during the query event
  • At the completion of the query the response along with the earlier information is sent to the IoT Hub using the IoT Hub REST API
  • The event is consumed from the IoT Hub by an Azure Event Hub
  • The message containing the information is processed by Stream Analytics and put into Azure Table Storage and Power BI.

Azure Table Storage provides the logging/auditing trail of what requests have been made and the responses.  Power BI provides the reporting aspect. These two services provide visibility into what requests have been made, against who, when etc. The graphic below shows this in the bottom portion of the image.

Auditing Reporting Searching MIM with Speech.png
Voice Search for Microsoft Identity Manager Auditing and Reporting

Sending IoT Device Events to IoT Hub

I covered this piece in a previous post here in PowerShell. I converted it from PowerShell to Python to run on my device. In PowerShell though for initial end-to-end testing when developing the solution the body of the message being sent and sending it looks like this;

[string]$datetime = get-date
$datetime = $datetime.Replace("/","-")
$body = @{
 deviceId = $deviceID
 messageId = $datetime
 messageString = "$($deviceID)-to-Cloud-$($datetime)"
 MIMQuery = "Does the user Jerry Seinfeld have an Active Directory Account"
 MIMResponse = "Yes. Their LoginID is jerry.seinfeld"
 User = "Jerry Seinfeld"
}

$body = $body | ConvertTo-Json
Invoke-RestMethod -Uri $iotHubRestURI -Headers $Headers -Method Post -Body $body

Event Hub and IoT Hub Configuration

First I created an Event Hub. Then on my IoT Hub I added an Event Subscription and pointed it to my Event Hub.

IoTHub Event Hub.PNG
Azure IoT Hub Events

Streaming Analytics

I then created a Stream Analytics Job. I configured two Inputs. One each from my IoT Hub and from my Event Hub.

Stream Analytics Inputs.PNG
Azure Stream Analytics Inputs

I then created two Outputs. One for Table Storage for which I used an existing Storage Group for my solution, and the other for Power BI using an existing Workspace but creating a new Dataset. For the Table storage I specified deviceId for Partition key and messageId for Row key.

Stream Analytics Outputs.PNG
Azure Stream Analytics Outputs

Finally as I’m keeping all the data simple in what I’m sending, my query is basically copying from the Inputs to the Outputs. One is to get the events to Table Storage and the other to get it to Power BI. Therefore the query looks like this.

Stream Analytics Query.PNG
Azure Stream Analytics Query

Events in Table Storage

After sending through some events I could see rows being added to Table Storage. When I added an additional column to the data the schema-less Table Storage obliged and dynamically added another column to the table.

Table Storage.PNG
Table Storage Events

A full record looks like this.

Full Record.PNG
Voice Search Table Storage Audit Record

Events in Power BI

Just like in Table Storage, in Power BI I could see the dataset and the table with the event data. I could create a report with some nice visuals just as you would with any other dataset. When I added an additional field to the event being sent from the IoT Device it magically showed up in the Power BI Dataset Table.

PowerBI.PNG
PowerBI Voice Search Analytics

Summary

Using the Azure IoT Hub REST API I can easily send information from my IoT Device and then have it processed through Stream Analytics into Table Storage and Power BI. Instant auditing and reporting functionality.

Let me know what you think on twitter @darrenjrobinson

Sending Events from IoT Devices to Azure IoT Hub using HTTPS and REST

Overview

Different IoT Devices have different capabilities. Whether it is a Micro-controller or Single Board Computer your options will vary. In this post I detailed using MQTT to send messages from an IoT Device to an Azure IoT Hub as well as using the AzureIoT PowerShell Module.

For a current project I needed to send the events from an IoT Device that runs Linux and had Python support. The Azure IoT Hub includes an HTTPS REST endpoint. For this particular application using the HTTPS REST endpoint is going to be much easier than compiling the Azure SDK for the particular flavour of Linux running on my device.

Python isn’t my language of choice so first I got it working in PowerShell then converted it to Python. I detail both scripts here as a guide for anyone else trying to do something similar but also for myself as I know I’m going to need these snippets in the future.

Prerequisites

You’ll need to have configured an;

Follow this post to get started.

PowerShell Device to Cloud Events using HTTPS and REST Script

Here is the PowerShell version of the script. Update Line 3 for your DeviceID, Line 5 for your IoT Hub Name and LIne 11 for your SAS Token.

Using Device Explorer to Monitor the Device on the associated IoT Hub I can see that the message is received.

Device Explorer

Python Device to Cloud Events using HTTPS and REST Script

Here is my Python version of the same script. Again update Line 5 for your IoT DeviceID, Line 7 for your IoT Hub and Line 12 for the SAS Token.

And in Device Explorer we can see the message is received.

Device Explorer Python

Summary

When you have a device that has the ability to run Python you can use the IoT Hub HTTPS REST API to send messages from the Client to Cloud negating the need to build and compile the Azure IoT SDK to generate client libraries.

Exporting IoT Device Information from Azure IoT Hub(s) using PowerShell

Introduction

I have a number of Azure IoT Hubs each with a number of devices configured on them. I wanted to export the details for each IoT Device. This can’t be done via the Azure Portal (May 2018) so I looked to leverage the Azure.IoTHub New-AzureRmIotHubExportDevices cmdlet.

Now the documentation for New-AzureRmIotHubExportDevices is a little light on. When I was running the New-AzureRmIotHubExportDevices I kept getting the error ‘Operation returned an invalid status code ‘InternalServerError’.

After many attempts (over weeks) I finally was able to export my IoT devices using PowerShell. The key was to generate the SAS Storage Token for the Container rather than creating a blob file to export to and generating a SAS Token for the file. Simply specify the Storage Container to export too.

Overview

My sample script below uses the latest (as of May 2018) version of the Azure.IoTHub Module (v3.1.3). It;

  • enumerates all Resource Groups in an Azure Subscription and looks for IoT Hubs and puts them into a collection
  • then iterates through each IoT Hub, creates an associated Storage Account (if one doesn’t exist)
  • Exports the IoT Devices associated with the IoT Hub to Azure Storage
  • Downloads the IoT Devices Blob File, opens it and displays through PowerShell console output the IoT Device Names and Status

Export IoT Devices 640px

To use the script you will just need to;

  • change your Subscription Name in Line 4
  • The location where you want to download the blob files too in Line 31
  • if you want to display additional info on each device or do something else with the info change line 71 accordingly

The exported file can be found using Azure Storage Explorer as shown below.

Output Blob File.PNG

And the script outputs the status to the PowerShell console as shown below.

Exported IoT Devices.PNG

The exported object contains all the details for each IoT Device as shown below in the IoT Device PSObject.

IoT Device PSObj.PNG

Summary

It is obvious once you work out how the cmdlet works. Hopefully this working example will save someone else a few hours of head scratching.

Global Azure Bootcamp 2018 – Creating the Internet of YOUR Things

Today is the 6th Global Azure Bootcamp and I presented at the Sydney Microsoft Office on the Creating the Internet of YOUR Things.

In my session I gave an overview on where IoT is going and some of the amazing things we can look forward to (maybe). I then covered a number of IoT devices that you can buy now that can enrich your life.

I then moved on to building IoT devices and leveraging Azure, the focus of my presentation. How to get started quickly with devices, integration and automation. I provided a working example based off previous my previous posts Integrating Azure IoT Devices with MongooseOS MQTT and PowerShellBuilding a Teenager Notification Service using Azure IoT an Azure Function, Microsoft Flow, Mongoose OS and a Micro Controller, and Adding a Display to the Teenager Notification Service Azure IoT Device

I provided enough information and hopefully inspiration to get you started.

Here is my presentation.

Creating the Internet of YOUR Things. Global Azure Bootcamp 2018

Getting started creating the Internet of YOUR Things leveraging Microsoft Azure IoT and Mongoose OS