Let's start with a question, do you know what this picture represents?
It's a very nice visualisation of a very bad thing we experience - global temperature changes over last 150 years. If you want to see more pictures like that, check this blog post http://www.climate-lab-book.ac.uk/2018/warming-stripes/ or for UK specific results https://www.climate-lab-book.ac.uk/2018/climate-stripes-for-the-uk/
I'm sure you are not surprised by it, we just had a very warm Summer in Europe. While you may think, it's nice to have a bit more warmth, it is not good at all for our planet. Massive wildfires, cities like London being flooded, by raising sea level, are only some of threats, we may face over next couple of years. That's why a lot of people is looking for solutions, to save the Earth.
Renewable energy
Electricity is awesome. We don't have to worry about darkness, our food will stay fresh for longer, and hospitals have lots of electric devices which may save your life. The problem is how we generate power. Fossil fuel is still the main way to do it. If you want to see real time information about UK National Grid, have a look here https://www.gridwatch.templar.co.uk/
The interesting fact is, energy we receive from Sun in 88 minutes should be enough to power all electric devices for a year, all of them. That sounds cool, let's just use it. Unfortunately it's not so simple, you probably heard that Denmark can generate 140% required energy from wind farms, they exported it to other countries, but when there is no demand, you have to stop generation. This is a big problem, we can't control when the Sun will shine, when wind will blow, or rain will power hydroelectric power plants. What we have to do, is to generate demand, when it's possible to generate power. That sounds difficult, for a long time it was impossible. Fortunately the cost of computers dropped and now you can have a small computer, controlling devices without high cost. IoT (Internet of things) enables us, to turn on heaters, car chargers and home battery chargers when there is some renewable energy available. It means, that we consume power, when it's best for our planet and reduce your bills as additional benefit. With batteries it's even possible to release the power to the grid, when there is not enough renewable power available. You can read about our vehicle to grid project here https://www.ovoenergy.com/electric-cars/vehicle-to-grid-charger
Getting started with IoT Hub
You may now be thinking, that working with IoT requires knowledge of C/C++ and asm, while it's still true for devices, there is a need for developers to build platform, which will control them. The language you use, for a platform, can be really anything, pick what you use for your normal job or any language you fancy.
Cloud providers make it easier to communicate with devices, so you can focus on your system behaviour. You may still have to develop your own IoT platform, but to get it up and running quickly, available solutions should be enough. If you want just to play with IoT, you can run it for free on Azure, if you send less than 8000 messages per day, it's not enough for production, but if you want to measure temperature and humidity in your flat, you can send a message every 11 seconds and not pay a single penny.
What you will need is Azure account and Visual Studio Code, once you have both, you can move to main part of our exercise. Let's run some simulation. To get it working, you will need 5 minutes and that includes time for making a tea or a coffee, or even both. On Azure portal you can create your first IoT Hub.
Make sure you pick free tier when doing it, on Size and scale tab, you are allowed to have only one free IoT Hub.
While your IoT Hub is deploying, you can go to Visual Studio Code and install an Azure IoT Toolkit plugin. https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit
When it's installed, and your IoT Hub is ready, you can select IoT Hub.
You should see a drop down with Subscriptions and then with your IoT Hub.
If everything went well, you should see an information, that you have no devices. You can either add them with the plugin or do it on the Azure portal. Let's head to the portal. Pick IoT devices section and press blue plus button.
Now set the name for your device.
Once you added a device, you can head back to VS Code, next to the plugin name you can see refresh button, pressing it will refresh list of devices.
Now you can right click on the device name and select "Start monitoring D2C Message". You should see console output.
Subscription selected: Pay-As-You-Go
IoT Hub selected: ovoenergy-adventcalendar
[IoTHubMonitor] Start monitoring D2C message for [FirstDevice] ...
[IoTHubMonitor] Created partition receiver [1] for consumerGroup [$Default]
[IoTHubMonitor] Created partition receiver [0] for consumerGroup [$Default]
It's time to send our first message, right click on device again and select "Send D2C Message to IoT Hub", type your message and press Enter.
You should see the message on the console.
[D2CMessage] Sending message to [IoT Hub] ...
[D2CMessage] [Success] Message sent to [IoT Hub]
[IoTHubMonitor] [2:04:35 PM] Message received from [FirstDevice]:
"Hello World!"
Well done, you just communicated with IoT Hub, but now we would like to have the application, which will do it for us. Once again, right click on the device and pick "Generate Code", you have couple of languages to pick from, they will have different code samples, you can check what is available for which language. I will pick F# with Send device to cloud message example.
It should generate a project for you, with device's connection string. It will create a client first.
let deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt)
And in the loop, it will generate random temperature and humidity, serialise it to a string, based on temperature add a message property "temperatureAlert" and send it to IoT Hub.
let currentTemperature: double = minTemperature + rand.NextDouble() * 15.0
let currentHumidity: double = minHumidity + rand.NextDouble() * 20.0
let messageString: string =
{ Temperature = currentTemperature; Humidity = currentHumidity }
|> serialize
let message = new Message(Encoding.ASCII.GetBytes(messageString));
// Add a custom application property to the message.
// An IoT hub can filter on these properties without access to the message body.
message.Properties.Add("temperatureAlert", if currentTemperature > 30.0 then "true" else "false");
// Send the telemetry message
do! deviceClient.SendEventAsync(message).ContinueWith (fun t -> ()) |> Async.AwaitTask
Start your application, and see in VS Code, if you are receiving messages every second.
[IoTHubMonitor] [2:32:07 PM] Message received from [FirstDevice]:
{
"body": {
"Temperature": 21.595403187254167,
"Humidity": 78.33476856273356
},
"applicationProperties": {
"temperatureAlert": "false"
}
}
[IoTHubMonitor] [2:32:08 PM] Message received from [FirstDevice]:
{
"body": {
"Temperature": 33.25389704585722,
"Humidity": 78.88846343331433
},
"applicationProperties": {
"temperatureAlert": "true"
}
}
You now have an application, which you can run on your device and it will send messages to the cloud. Grab Raspberry Pi, you haven't used for months, connect temperature and humidity sensor, change slightly the code to read real values and you will have real time monitoring.
If you would like to read more about IoT Hub, check the documentation
https://docs.microsoft.com/en-us/azure/iot-hub/
Summary
Today we learned how to create Azure IoT Hub, add new device, send the message to IoT Hub both from VS Code and code. Hopefully you will now explore the IoT possibilities, while having fun writing code.