How we built an IoT application that was featured on stage at Gartner Catalyst
How we built an IoT application that was featured on stage at Gartner Catalyst by Nick Ford
Over the last few weeks I’ve had the pleasure of working with Gartner Analyst Kyle Davis on an IoT application that was showcased at the recent Gartner Catalyst Conference in San Diego and its London counterpart happening this week.
The brief was:
Can we make it really simple for non-technical, Citizen Developers to build a connected app, and place it in the hands of end users as a dashboard and mobile app in a few hours?
As many of you are probably aware, the world of IoT is complex with multiple moving parts, physical devices generating streaming data, time series data stores, Big Data, Rules Processing, Analytics and protocols like MQTT for publishing and subscribing to topics for device management.
The introduction of platforms like AWS IoT, IBM Watson IoT and Azure IoT Hub have gone a long way towards democratizing the physical connection, data gathering and streaming analytics required to manage devices and end points on the Edge. But how do you put this information into the hands of users in the form of actionable insight? How do you make it web and mobile-enabled, that can be built without dropping down to node.js, Python or some other scripting environment?
Enter Mendix. Our platform comes out of the box with a whole bunch of connectors to make building IoT applications using our proven visual modeling tools. This further extends the already democratized IoT Platforms into the world of Rapid App Development.
So what was the project about and how did we build it? Well the project itself is pretty simple: we were asked to help Kyle build an app that would connect to a remote control car or simulator that would track temperature, direction and G-Force.
The simulator sensors are hooked up to an Arduino Yun board and connected to AWS IoT. We used MQTT and Device Shadows to quickly build a solution that consumes incoming event shadows, implements some basic logic and represents the output as a simple dashboard of events.
Here’s how we did it:
Getting started with the Mendix AWS IoT connector
This section will outline how you can create a basic Mendix app that integrates with IoT devices managed in the AWS IoT cloud.
Setting up AWS IoT
AWS provides a number of SDKs for IoT devices. The main goal of these SDKs is to easily enable the software on the device to exchange information with AWS IoT. The main protocol for this exchange is MQTT. This is a lightweight publish and subscribe messaging protocol aimed at machine to machine communication. You can find a list of starter kits and device SDKs on this AWS site.
The Arduino Yun is a nice device to get started. There are a large number of sensors that you can easily connect, and using something like a groove board doesn’t require any soldering. Below is an example that has temperature, touch, sound, light and led sensors connected.
The AWS SDK for Arduino Yun can be found on Github accompanied by an installation guide.
You need to configure your device to securely connect to AWS. You can generate and download the required keys from AWS IoT. Next, you need to program the device to do whatever you need it to do: read and control the sensor, and communicate with AWS. Examples for Arduino can easily be found on the internet.
Alternatively, you can start with a finished device. The 1m2m device pictured below is a complete solution to monitor your environment and is used in different smart building settings. This particular one uses LoRa to communicate with the outside world, and can be used in Mendix through the LoRa connector in the App Store.
Every IoT device needs to be registered in the AWS IoT dashboard. Here you can generate certificates for installation on the device. When you have the device connected to AWS, the device can start reporting state to AWS. AWS will remember the last reported state of a device, or Thing, so you can always request it, even if the device is offline.
For every Thing or device known to AWS IoT, you can view the last known status, when it was last updated, and also the endpoints required for MQTT or REST communication. The following is an example screenshot from AWS IoT.
The status of a device is available as a JSON document. The format of this document is described on the Amazon website.
The exact content of it will be dependent on the device, its sensors and how it reports the status of the sensors. Usually it’s easiest to connect your Mendix app to AWS IoT, receive some shadow state message and use this to define your JSON import mappings. We will illustrate this in the next section.
Reading device status into your Mendix App
There are different ways of receiving sensor information in your Mendix applications. You can call the Rest endpoint of a thing shadow, create an AWS IoT rule to provide the state at some API in your Mendix application, or subscribe to an MQTT topic.
Below we will show you how to use MQTT publish and subscribe. This is the easiest way to receive up-to-date sensor data from your device.
Download the AWS IoT connector from the Mendix AppStore into your Mendix application. Create an on appstartup microflow to subscribe to the correct topic.
Configure the subscribe action. The broker information can be found on the Thing details page in AWS IoT. For authentication you need to create a certificate on AWS IoT. This means that to AWS IoT, your application is seen as just another IoT device communicating over MQTT. Save the license files in your resources folder of your Mendix project. In the action, you need to provide the location of these files relative to the resources folder.
Finally, you need to provide a topic that you want to subscribe to, and a microflow to be called when a messages arrives in your application. The names of the topics used by AWS is documented here.
The microflow that will be called upon to receive an IoT message requires two String parameters. One will contain the topic name, the other will contain the actual message contents. The following microflow uses and import mapping to create Mendix objects for the message received. Next, it saves object. Finally, it calls another microflow which posts a tweet about the temperature received. This uses the Mendix Twitter Connector, also available in the Mendix App Store.
The import action requires you to first define the structure of the JSON payload received through MQTT. As mentioned before, it’s often simplest to run your app, log the message received, and copy that into your Mendix project. The screenshot below illustrates this.
Now that you have defined the structure of the JSON message received through MQTT, you can create a JSON import map by selecting the relevant state that you want to map. In our example, we are interested in the actual sensor values, version and timestamp of the message received. The timestamp will allow us to display the newest value in our app, or to create a chart graphing all sensor values received over a period of time.
Here’s the actual mapping to a Mendix entity:
Controlling your device
Next we want to control our IoT device from our app. Again, we use MQTT to communicate with the device through Aws IoT.
We need to turn a lamp connected to a device on and off. To do this we need to send a JSON message to the Thing. As this is a very simple JSON message stating what the status of the lamp should be, we opted to just include the JSON message as a string. For more complex JSON messages, it can be helpful to define an export mapping.
The following microflow uses the publish MQTT message from the AWS IoT connector to send a message to AWS IoT.
Configuration of the publish MQTT message action can be seen below. Again, we need to configure the broker and security details. To send messages to a device, a different topic needs to be specified.
Now that you have your communication between the device and your app, building an app around it takes little effort. Here is a screenshot of the app we created.
This post was co-written with Mendix Product Manager Andrej Koelewijn.