Azure IoT Central makes it easy to connect, monitor, and manage your IoT devices at scale. With the IoT Central V3 connector, you can trigger workflows when a rule has fired, and take actions by executing commands, updating properties, getting telemetry from devices, and more. Use this connector with your Azure IoT Central V3 application.
Due to the dynamic nature of Azure IoT Central, using the connector is not quite straight-forward. To use the Azure IoT Central connector in Canvas apps you need to enable two features in the settings of the application.
These are:
Enhanced formula bar
Dynamic Schema
Enabling these two features allows Power Apps to understand the resulting schema from a call to IoT central. “Capture Schema” will be enabled on the formula bar.
Power Apps Canvas Settings: Enhanced Formula BarPower Apps Canvas Settings: Dynamic SchemaFormula bar: Schema Capture
Power Automate
Power Automate is a lot simpler to get to work. It automatically understand the dynamic schemas of IoT Central.
I control a few devices from the Internet, but I don’t want all devices exposed and connected to IoT Central. One solution is to build a gateway locally which routes request messages to switch things on or off to the correct device locally.
In this post we will look at how to send commands from IoT Central and route them using Node-RED to the automation device via MQTT.
Home Automation Gateway Architecture
This architure uses Node-RED as a gateway, and then uses Eclipse Mosquito as a MQTT server. The Grow Light and Air Conditioner in the diagram is an off the shelf Sonoff POW-R2 device flashed with Tasmota.
With Node-RED you can visually map the flow / sequence of events. Node-RED supports Azure IoT Hubs and Azure IoT Central, so it’s perfect for this job to do quickly and easily without having to write code.
Gateway device created in IoT Central’s Command Capabilities
In Azure IoT Central we have a single device linked with a command. The command accepts a parameter, which will be the device name / topic name on the local MQTT server.
Executing a command on IoT Central
The diagram above shows the command with the parameter.
Defined Methods to IoT Central Commands
The Node-RED Azure component supports Direct Methods. These are commands that you can directly invoke on a device. In this case, the device is the gateway itself created within Node-RED.
It supports two methods (as per Azure IoT Central template)
turngatewaydeviceon
turngatewaydeviceoff
Switch to determine flow based on Direct Method Invoked from IoT Central
With a Node-Red switch statement we can controll the flow with whichever method name was invoked from IoT Central.
Switch Home Automation On flow
If it’s the “DeviceOn” flow, then the device parameter name (payload) is used to build up the topic for the Sonoff device via MQTT. The payload of the message will be “on”.
Switch Home Automation Device Off Flow
If it’s the “DeviceOff” flow, then the device parameter name (payload) is used to build up the topic for the Sonoff device via MQTT. The payload of the message will be “off”.
The Sonoff devices are great, but I prefer to use everything within my home and home automation setup to be fully under my control. Tasmota is a great way of doing just that.
I found the easiest way of flashing devices with Tasmota is using a raspberry pi. I’ve done a quick writeup on how to do this.
Choose Interfacing OptionsConfigure SerialDisable login shell via serialEnable serial port hardwareSerial setup complete
Connect the Raspberry Pi to the Sonoff Device
Raspberry Pi Pinouts (Serial)
The TXD (Transmit) and RXD (Receive) pins are marked on the raspberry pi. We can use these to communicate serially with the Sonoff Device to flash it with the Tasmota firmware.
Sonoff POW R2 Device
Each Sonoff device has pins allocated to reflash the device. These boards usually don’t have headers attached. Here I soldered one to the board.
The important pins are VDD, TX, RX and GND.
Flashing the device with a Raspberry Pi
Connect VDD to an external power supply. The Raspberry Pi will not be able to power the device off it’s own pins. I used an external bench power supply. Ensure the voltage is 3.3V.
Connect the GND on the power supply to both the Raspberry Pi Ground Pins and the Sonoff device’s GND pin
Connect the TXD on the Raspberry Pi to the RX Sonoff pin and connect the RXD on the Raspberry Pi to the TX Sonoff pin.
Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.
It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.
Running Node-RED is easiest in a Docker container. This is really easy to set up.
Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.
The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.
Running Mosquitto is easiest in a Docker container. This is really easy to set up.
There has been some nice progress on the pure c# library for the EV3 Lego Mindstorm Brick from Lego and the BrickPi3 from Dexter Industries. The BrickPi is great as it adds loads of extra processing power and more importantly memory for the EV3 kit using a Raspberry Pi.
The library is for both Xamarin.IoT and Android Things. As soon as it’s ready I’ll post the source to GitHub. If there is interest, I can also spend some time and port the Android Things implementation to Java or Kotlin for Android Studio.
The ultimate plan is to use this in a Lego based robot as a play project, but the experimentation will form basis of an actual larger artificial intelligence project. It will also be hooked up to Azure IoT for the telemetry.
There’s a lot of fun to be had here! Xamarin Iot deploying to a Lego Mindstorm EV3 from Visual Studio, isn’t that awesome?
Visual Studio 2017 Preview 5 – Xamarin Iot EV3
LCD Update via Xamarin Iot
I’m now off to make it do something more useful. I will write a full blog post about it afterwards, including instructions how to set this up yourself.
I was inspired enough to dust off one of my older projects which included .NET MF and the Xamarin Monkey Robotics project. I then decided to consolidate and rearrange it with some of the newer stuff I’m playing with in the “Iot” space. It’s also an example of how Visual Studio can handle projects of all types, held together and fully “debuggable” within one solution. This seems like a good time to do this especially with the recent talks at the Xamarin User Groups in Cape Town (http://explorationspace.co.za/2017/07/20/cross-platform-iot-at-ctxug/) and Johannesburg (http://explorationspace.co.za/2017/07/12/cross-platform-iot-at-gxugsa/) on the topic.
Visual Studio Controller of Things Solution
I do still have to port all this to Visual Studio 15.3 preview 3 so I can have Xamarin Iot working in the solution as well.
Still a lot of on the list to do today, so I’ll end off here.