emnify Connectivity integration into AWS IoT Core

09.07.2019
guide-image

When developing IoT applications, enterprises need to integrate several different components for launching a service – cellular connectivity to connect the devices, security, message brokering, firmware updates, data management and analysis, and application logic. Amazon Web Services (AWS) have become a standard for developing IoT applications because all components are offered as easy-to-integrate services that provide fast time-to-market, supported by a scalable, reliable backend.  
 
This article describes how a NB-IoT / CAT-M device with emnify IoT SIMs can be integrated in AWS IoT Core and is accompanied by an example of using a dash button. 

What is AWS IoT Core?

AWS IoT Core is a management platform for IoT devices that uses MQTT (a pub/sub protocol) or Websockets. It provides the entry point for IoT devices to the AWS domain, so that other services can be integrated; such as:

  • Running Lambda functions based on data occurrence or content
  • Service and infrastructure monitoring in CloudWatch
  • Storing data in DynamoDB
  • Using IOT Analytics for real-time surveillance

AWS%20IoT%20Core%20-%20Connect%20and%20Manage.edb43e92d542f4053727eaeda267e3776382fd06

To demonstrate the integration of devices enabled with emnify SIMs into the AWS IoT Core, we used a CAT-M/ NB-IoT prototype device which connects via mobile networks to trigger AWS Lambda functions whenever the user presses a button.  

Setup

development board

Before setting up network communication, we have to first configure hardware and device settings. In this case, we use the STM32 discovery pack by ST Microelectronics. This kit includes a development board and a Quectel BG96 modem with an embedded emnify SIM card. The modem supports connectivity via LTE CAT-M1, LTE CAT-NB1 (NB-IoT) and EGPRS (2G)1.

The development board has a joystick-style button that can be pressed in four directions - this is the button we will use for input to trigger logic. On the software side, we use Espruino, a JavaScript interpreter specifically designed for microcontrollers. This is very useful as development boards may be flashed with an Espruino binary and run JavaScript directly on the hardware.

Networking

For communication with AWS IoT Core, the device has to connect to a mobile network. Using Espruino simplifies this step as libraries for modem communication are included and compatible with the Quectel BG96. The Espruino documentation contains examples for communicating with the BG96 and other Quectel modem modules.

One noteworthy step in this process is the configuration of APN settings. This is a common device configuration step but also a potential stumbling block when getting started. When using emnify SIMs to connect, the APN should be set to “em” while the username and password inputs should be blank (or set to empty values).

AWS IoT Core Configuration

Initial steps

In AWS IoT Core, physical devices are represented as ‘Things’ that can be managed via a GUI. In order to have our device registered and managed in the GUI, create a ‘Thing’ by logging in to AWS, navigate to IoT Core and, under “Manage -> Things”, click Create.

3-iot-dashboard-create-thing

After choosing a name, you will be asked to add a certificate to the device, which is necessary for establishing a TLS encrypted connection to AWS IoT Core. The simplest way to get a certificate is by choosing “One-click certificate creation”. This generates a certificate, a public key and a private key for the device; all of which need to be downloaded.

After that, you need to download a root CA for AWS IoT

Communicating with IoT Core

Finally, the device needs a policy within the AWS IoT Core with the necessary permissions to communicate with the AWS MQTT Message Broker by establish a TLS-encrypted connection to it. Espruino proves useful again, with libraries for handling TLS and MQTT.  The certificate, private key and root CA downloaded from the previous step must then be passed as the options for the TLS session.

The following Espruino snippet shows the connection function and our communication configuration in the options at the beginning (the host value is found in the 'Interact' section of the Thing). In this case, our values are read from a JavaScript file on the development board flash memory (awsIotConfig) and the options will be stored when flashing the device:

Trigger Lambdas at the touch of a button

To have other events triggered on button press, we make use of Espruino’s built-in function setWatch. These ‘watches’ monitor changes in the signal of a pin on development boards (button presses in our case), and execute a callback function2. There are watch functions for each direction that the built-in button can be pressed; when triggered, the device publishes an MQTT message containing the button-press direction.

For testing, IoT Core provides an MQTT client that can be used to:

  • Publish to topics with dummy data in order to mock the MQTT messages sent by our device
  • Subscribe to the topic the device is publishing to and verify that messages are visible in the console

5-mqtt-client

After communication between the device and AWS IoT Core is verified, rules should be created in the ‘Act’ tab in the form of SQL queries:

SELECT * FROM 'cpt-1/invoke' WHERE buttonPress = 'up'

Any number of actions can be attached to this clause, but what we are interested in is forwarding the incoming MQTT message to a Lambda function:

6-lambda-functionWhen pressing the ‘up’ button, a Lambda function to turn on or off any remote lights (that is also managed in AWS IOT Core) will be triggered. Another example would be to send a POST request to the emnify API with the relevant data for a SIM order or extending the Quota Limit. All of the basics are now in place for a customizable cloud button using an emnify SIM and integrated in AWS IoT Core.

Conclusion

Integrating emnify Connectivity and AWS IoT is the first step to allow data and cellular IoT devices to be managed with scalable, reliable AWS services. While just a prototype, the cloud button is already quite powerful due to the capabilities of triggering actions on AWS IoT Core from a cellular device. Possible alternatives to calling a Lambda function include inserting data directly into a DynamoDB table, sending SMS notifications or even triggering CloudWatch alarms. Devices fitted with sensors used to trigger different actions without the need for user input can also be adapted to fit any use case when empowered with cellular connectivity. 

Footnotes:

1) Firmware updates and binaries for device testing can be found at the STM32 C2C portal listed on the device packaging. If you cannot establish a connection to the mobile network using this device, you might want to check out the “BG96 Cellular Sweep Configurator” binary package. In areas with no LTE Cat M1 or NB-IoT available,  it’s recommended to reflash the device to connect only via EGPRS which solved connectivity problems with this device.

2) More information about how to use this feature can be found in the Espruino documentation on Buttons and setWatch.

Get in touch with our IoT experts

Discover how emnify can help you grow your business and talk to one of our IoT consultants today!

Related Posts