Distributing IoTHub credentials using TPM

I bet, most of the time you have seen Azure IoT demos or most of the time you have programmed an IoT Uwp app yourself, you hard coded device credentials for the IoT hub. Yes, I’m guilty too ūüôā

And this is, of course, a bad practice.

Not only, there is a risk these¬†credentials are shared by checking them in into your version control system¬†(like public Git). But it’s also inconvenient because, for each device running that production code, you will have to alter the credentials in the code and deploy again.

We could use configuration files. But this is still worthless in perspective of distribution.

We would like to pass the credentials to known devices separately, apart from the applications. We want to use a second channel. And this is possible with the current Windows IoT Core infrastructure.

All we need is a TPM. This is a Trusted Platform Module:

Trusted Platform Module (TPM) is an international standard for a secure cryptoprocessor, which is a dedicated microcontroller designed to secure hardware by integrating cryptographic keys into devices. TPM’s technical specification was written by a computer industry consortium called Trusted Computing Group (TCG). International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC) standardized the specification as ISO/IEC 11889 in 2009.[1]

Why do we need it? Microsoft provides a separate mechanism to write credentials into the module which acts like a vault.

In this example, we will look at Windows 10 Core running on a Raspberry Pi. And we will use IoT Hub device credentials stored in a TPM.

Continue reading “Distributing IoTHub credentials using TPM”

Turn your Raspberry Pi into a Personal Assistant using Cortana

Microsoft is constantly updating its¬†latest version of Windows, version 10. For me, as a developer, it’s a wonderful operating system to program for. The UWP apps I build, run on both PC’s, laptops, Windows Surface Hub (up to 84 inches), The Xbox One and even on a Raspberry Pi. Yes, Windows 10 is running on a 35 dollar device.

But before you run to the store to replace your PC, I have to tell you it’s running the core of Windows 10, actually. There is no shell (no menu, no start bar etc.).

So this means you can run one visual (headed) UWP application and multiple background applications. And yes, you will love it!

This is a great interface for kiosk-like devices. And with the latest update (build 15063), it’s easy to add¬†Cortana support.

Cortana is the speech service, available in Windows 10. If you know Siri or Alexa, then you know Cortana. Just ask her a question and she will try to answer it. The answer will be provided by speech or supported by browsers or other visual help.

Let’s take a look on how to enable Cortana on a Raspberry Pi.

Continue reading “Turn your Raspberry Pi into a Personal Assistant using Cortana”

Building a Windows 10 IoT Core background webserver

The RaspberryPi is running the core of Windows 10. This means that everything, not needed for running one app at a time, is left out of Windows 10. And with one app I mean, one visual app.

Until now I have always build a Windows UWP app to run something on the RaspberryPi.  And the fact it has a form which can represent visual elements in XAML, it gives away that it is a visual app. These kind of apps are running in headed mode.

But running one visual app, taking the whole screen occupied in headed mode, does not prevent the OS from running multiple background processes in headless mode.

Today we will build our first simple web server on the Raspberry Pi running Windows 10 IoT Core.

Continue reading “Building a Windows 10 IoT Core background webserver”

Reading IBeacons using a UWP app on your Raspberry Pi

As you probably know, Bluetooth low energy (BT LE)  is a wireless personal area network technology which uses a minimum of power to broadcast messages to receivers nearby.

Bluetooth LE is a common standard but it is most popular under the name of IBeacons. IBeacons is a protocol coming from Apple, so it is just a class of Bluetooth low energy (LE) devices that broadcast their identifier to nearby portable electronic devices.

IBeacons can basically exchange two parts of data: that unique identifier and the signal strength. This makes it possible to figure out the (fixed) position of the IBeacon. And if you receive the signals of multiple beacons you can triangulate your own position between them.

In 2015 Google launched a competing, but similar, beacon standard called Eddystone. It has a richer functionality because it can exchange more information.

Far less known is that Windows 10 also supports the beacon technology, it’s not just Apple¬†and Android which are having fun with it. In Windows, there is¬†this¬†Windows.Devices.Bluetooth.Advertisement¬†library:
“It allows apps to send and receive Bluetooth Low Energy (LE) advertisements.”

Continue reading “Reading IBeacons using a UWP app on your Raspberry Pi”

Use Bluetooth between Win 10 UWP and Arduino

This is part 3 of a series of blogs about device communication between Arduino, RaspberryPi etc:

  • Part 1: Using I2C to connect two Arduino Nano‚Äôs
  • Part 2: Using I2C between Rpi Master and Arduino Slave
  • Part 3: Use Bluetooth between Win 10 UWP and Arduino
  • Part 4: Add virtual Arduino ports to your UWP app
  • Part 5: Custom Firmata function called by Windows 10 IoT¬†Core
  • Part 6: Better long running custom Firmata functions
  • Part 7: Custom servo usage in Firmata
  • Part 8: Using NRF24L01+ modules¬†between Arduino’s
  • Part 9: Cheap Arduino mesh using RF24 radio¬†modules

In previous blogs, I have written about combining one or more Arduino Nano’s¬†with a RaspberryPi using the I2C protocol. Although this is very useful, the communication is limited to the length of the cables. This is not very practical for a local gateway.

fatfight3 Get it on Windows 10

So I looked at communication using Bluetooth. So I bought some Bluetooth modules. These little components are relatively cheap and reliable. They come with four and six pins (for now, it has just two extra pins not needed, the inner four are the same with the four pins version) but the four pin variant is used in this example.

Continue reading “Use Bluetooth between Win 10 UWP and Arduino”

Add a 5 inch LCD display to your Windows 10 IoT Core Rpi

Working with a Raspberry Pi is fun. Working with Windows 10 IoT Core is even more fun, combined with that Rpi. But it’s a bit annoying that we have to drag a big LCD monitor along when we want to do some visual XAML stuff on it.

Should it not be nice to use one of those tiny LCD screens on your Rpi? You can buy them for approx. 25 dollars so these are really interesting. They have HDMI, they are powered by USB and it’s even possible to put some touch to them ūüôā

So I ordered one. It came in a nice package and connecting it to the Rpi was easy, just connect an HDMI cable and plug in the micro USB connector for power.

fatfight3 Get it on Windows 10

But then I got scared, this is what was shown on the screen:

WP_20160105_10_21_40_Rich_LI

It should be clear that the vertical black bar was not to be shown there!

After the Shock or Disbelief, the Denial, the Anger, etc. there was Acceptance and Hope. A good friend pointed out that the Rpi could use some configuration to handle these small screen resolutions.

So I looked at the config.txt which can be found in the root of the image on the MicroSD card on the Rpi.

gpu_mem=32                  # set ARM to 480Mb DRAM, VC to 32Mb DRAM
framebuffer_ignore_alpha=1  # Ignore the alpha channel for Windows.
framebuffer_swap=1          # Set the frame buffer to be Windows BGR compatible.
disable_overscan=1          # Disable overscan
init_uart_clock=16000000    # Set UART clock to 16Mhz
hdmi_group=2                # Use VESA Display Mode Timing over CEA
arm_freq=900
arm_freq_min=900
force_turbo=1

Note: ignore wrapping if shown. Comments should only fill one line. Just compare it with your own config.txt

So after some searching, I found the resolution:

...
hdmi_group=2                 # Use VESA Display Mode Timing over CEA
hdmi_mode=1                  # 5 inch screen
hdmi_mode=87                 # 5 inch screen
hdmi_cvt 800 480 60 6 0 0 0  # 5 inch screen
...

Just add these extra three lines and you are back in business. You can just do that on your laptop the same way as you created the image: using an SD card adapter.

Note: make a backup first!

Now look at this screen:

WP_20160105_10_13_19_Rich_LI

As you can see we even have a scrollbar.

So now you have a small transportable screen which works just fine when you are developing for Windows 10 IoT Core.

Pro tip

If you are a Raspbian or Raspbian Jessie light user, this will work for you took. Just open /boot/config.txt and add the same four lines. And reboot afterwards.

Using I2C between RPi Master and Arduino Slave

This is part 2 of a serie of blogs about device communication between Arduino, RaspberryPi etc:

  • Part 1: Using I2C to connect two Arduino Nano‚Äôs
  • Part 2: Using I2C between RPi Master and Arduino Slave
  • Part 3: Use Bluetooth between Win 10 UWP and Arduino
  • Part 4: Add virtual Arduino ports to your UWP app
  • Part 5: Custom Firmata function called by Windows 10 IoT
  • Part 6: Better long running custom Firmata functions
  • Part 7: Custom servo usage in Firmata
  • Part 8: Using NRF24L01+ modules¬†between arduino’s
  • Part 9: Cheap Arduino mesh using RF24 radio¬†modules

In my¬†previous post I connected two Arduino’s using the I2C protocol. This was a good test for me to understand the I2C communication.

My initial goal was to connect a Raspberry Pi to one or more Arduino’s. Why?

Plain Arduino’s lack the ability to connect to the Internet. But they are terrific in reading sensors. They can read them in near real-time and convert raw data into something meaningful.

A Raspberry Pi lacks the ability to communicate using analog ports and it is considered slow (having a full OS like Raspbian or Windows 10 means that it just has too much on it’s head). But it¬†is good in communication to the internet (read: Cloud; read: Microsoft Azure) and it has lots of memory.

So it is a very interesting architecture to attach multiple Arduino slave to the same Raspberry Pi master. We create our own gateway. This way the Arduino’s can collect interesting data using low cost sensors and the data is represented in a nice Windows 10 IoT Core dashboard app (The RPi has a HDMI connection after all)¬†or sent the data¬†to a Azure Iot Hub.

In my previous post I showed that it is possible to send a question (‘T’ for temperature and ‘H’¬†for humidity) to a certain Arduino (a slave address was provided) to receive the sensor data.

WP_20151028_13_56_23_Pro

This time I have taken the same slave but attached a Raspberry PI as master. There is one catch! I2C communicates at using the same voltage the master is using to function on. So watch out when attaching a 5 volt master to a 3.3 volt slave… In that case you need a I2C Logic Level Converter. But for now we are safe ūüôā

public sealed partial class MainPage : Page
{
  public MainPage()
  {
    InitializeComponent();
  }

  private async void btnHumidity_Click(
      object sender,
      RoutedEventArgs e)
  {
    char request = 'H';

    var buffer = System.Text.Encoding.
                ASCII.GetBytes(new char[] { request });

    var response = await ReadI2CBus(18, buffer, 2);

    if (string.IsNullOrEmpty(response.State))
    {
      tbSent.Text = string.Format(
          "Humidity: {0}.{1}%",
          response.Response[0],
          response.Response[1]);
    }
    else
    {
      tbSent.Text = response.State;
    }
  }

  private async void btnTemperature_Click(
      object sender,
      RoutedEventArgs e)
  {
    char request = 'T';

    var buffer = System.Text.Encoding.
         ASCII.GetBytes(new char[] { request });

    var response = await ReadI2CBus(18, buffer, 2);

    if (string.IsNullOrEmpty(response.State))
    {
      tbSent.Text = string.Format(
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†"Temperature:¬†{0}.{1}¬įC",
            response.Response[0],
            response.Response[1]);
    }
    else
    {
      tbSent.Text = response.State;
    }
  }

  private async Task<I2CResponse> ReadI2CBus(
      int slaveAddress,
      byte[] buffer,
      int responseLength)
  {
    try
    {
      string aqs = I2cDevice.GetDeviceSelector("I2C1");

      var dis = await DeviceInformation.FindAllAsync(aqs);
      if (dis.Count == 0)
      {
        return new I2CResponse
        {
          State = "No I2C bus found"
        };
      }

      var settings =
            new I2cConnectionSettings(slaveAddress);

      using (var device = await
              I2cDevice.FromIdAsync(dis[0].Id, settings))
      {
         device.Write(buffer);

         var responseToFill = new byte[responseLength];

         device.Read(responseToFill);

         return new I2CResponse
         {
           Response = responseToFill
         };
      }
    }
    catch (Exception ex)
    {
      return new I2CResponse
      {
        State = ex.Message
      };
    }
  }

  private class I2CResponse
  {
    public byte[] Response { get; set; }

    public string State { get; set; }
  }
}

The I2C communication in written into a fairly simple method. This way it is very easy so send a message to a slave and receiving the response.