Skip to content

Instantly share code, notes, and snippets.

@XavierGeerinck
Last active May 2, 2019 01:38
Show Gist options
  • Save XavierGeerinck/81d79ec8fdb0e0d5ac36d558a56ab9d3 to your computer and use it in GitHub Desktop.
Save XavierGeerinck/81d79ec8fdb0e0d5ac36d558a56ab9d3 to your computer and use it in GitHub Desktop.
Hackleague_IOT

Jan Tielens - Tech Evangelist at Microsoft (IoT Coding Battle) - @jantielens

Running IoT on Windows

WinIOT

  • Windows version made to run on IoT devices
  • Example: Raspberyy pis, ...

How to get started with Win10 on Raspberry PI

You can connect the Raspberry PI to the WiFi and get the IP-Address. Through this IP-Address it is possible to connect to the Windows version that is installed on the SD-Card by using the WebBrowser.

How to connect?

  1. Open browser and navigate to the IP address
  2. Credentials: administrator : <see raspberry>
  3. Here we can see some statistics
  4. If you go to "remote" --> You can enable a IoT Remote Server that allows for a remote desktop connection
  5. Open client application, and enter IP address and Connect (Windows IoT Remote Client) --> This shows HDMI port

Windows 10 IoT does not have built in applications or a start screen. It is to create an application and the only one that will be showed

How to create an application?

  1. Install full version of Visual Studio and Azure SDK in the update tab
  2. File -> new project -> new Universal Application

How to deploy?

  1. Select built for ARM
  2. Select deploy to Remote Machine and enter the address that we found on the IoT device
  3. Watch the Remote Client and it should change

IoT Patterns

IoT Hub is our event bus (Cloud Gateway), this can handle millions of events / second compared to REST APIs

Through using a Field Gateway, we can adapt the protocol used by the sensor to convert this protocol to the Cloud Gateway protocol

Example: When we can not have SSL, we will let the device communicate with the Field Gateway, which will talk with SSL to the Cloud Gateway

For the hackaton:

We have a device that is connected to a cloud gateway, that we will stream through the hot path analysis tool to show it as a presentation

In azure:

  1. Create IOT hub (that will receive the data)
  2. We have a streaming job that has a query:
SELECT
	Sensor as sensor, 
  system.timestamp as timestamp,
avg(Value) as avgvalue,
min(Value) as minvalue,
max(Value) as maxvalue,
cast(cout(Value) as float) as nrofvalues
INTO
  outputdb
FROM inputevents
GROUP BY Sensor, TumblingWindow(second, 5)

This tumbling window says that every 5 seconds we will do this select statement

  1. This result will go into a database that gets shown on a website
  2. iotcodingbattle.azurewebsites.net

How to write code that will push this data to the stream?

  1. Go to https://github.com/Azure/azure-iot-sdks
  2. I picked "node", and went into the device/samples directory and copied simple_sample_device.js which contains everything you need.
  3. Now just install the npm packages (npm install azure-iot-device azure-iot-device-amqp). And start the program with node index.js or however you called the file
  4. The transmitting begins! To change the packet format being sent, change line 37.

How to find boilerplate code easily?

Note: I tried this and it didn't work because the documentation is outdated, the node example was using a wrong constructor for the Http part which was not included in the npm package anymore. See the previous part to use this.

  1. azure.com --> Go to Azure IoT Hub documentation (under products)
  2. --> Get your IoT starter kit
  3. --> Select device and other stuff --> See code snippet that contains structure to send to the IoT hub
  4. --> Do not forget NuGet package (Microsoft.Azure.Devices.Client)
  5. --> Change the DeviceConnectionString to the IoT hub (HostName=...;SharedAccessKey...
  6. --> SendEvent, change the dataBuffer to what we want to send, by creating a small dataclass
  7. Create a Small POCO class in your code:
public class SensorValue {
    public string Sensor {get; set; }
    public double Value {get; set; }
}
  1. Create an object with some demo data such as Sensor: 'TestSensor' and Value: 123 (new SensorValue() { Sensor: 'TestSensor', Value: 123 })
  2. Serialize it with: JsonConvert.SerializeObject(sv);
  3. on the website through the ?sensor=dummy we can see these messages coming in

This is easily explained, but how it works behind the screens is that we are sending messages to a 'Event Bus' that is basically a queue system containing all the messages. In this queue system we wrote the 'Producer' that will create the data and produce it into this event bus, so to read data from it through a webpage, we need a consumer. This consumer can be created in Azure and will with our query, extract the relevant data from our input queue and put it into a database for further extraction. Through the use of 'EasyTables' we can then generate an API that allows us to access this data through a rest api.

Get connection string from your own Event Hub Device

  1. Get the tool Device Explorer here: https://github.com/Azure/azure-iot-sdks/blob/master/doc/manage_iot_hub.md
  2. Find your connectionstring for the eventhub on azure and copy it.
  3. Now go to configuration and paste it in there and click update
  4. Go to Management and create a new device, once this is done, right click the entry and click Copy connection string for selected device

References

  • windowsondevices.com has more information about it
  • iotcodingbattle.azurewebsites.net
  • docs.com/jan-tielens-1/4492/iot-coding-battle

FAQ

Q: Can it run any library? A: Yes, any library that can run on UWP apps

Q: Can it learn iteratively? A: Yes azure can do this, for more information see their website

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment