Back to overview 2.3

Amazon Web Services (AWS) – connect and publish data

Introduction

Cloud monitoring is a very interesting application the Connectino can be used for. Nearly all sensors, shields and accessories you can find for the Arduino can be used on the Connectino too.

In this tutorial you will learn the basics on how to use the Amazon Web Services (AWS). You will have a shadow device in the cloud which represents the current state of the Connectino and which receives the measured sensor data.

Concept

To communicate with AWS, the device uses a virtual representation, the device thing. That includes the device shadow, a JSON string containing the state-variables of your IoT device. To access and change this JSON string, you will need to publish and subscribe topics.

The Connectino needs the permission to publish and subscribe those topics. To get those permissions you need to load a certificate, created in the following tutorial, onto your device. At last you will set the permissions for this certificate and your device will be able to change and read shadow variables in the IoT cloud.

Requirements

There are some basic requirements needed:

  • A Connectino and some data you want to send to the Cloud (in this tutorial the brightness value of the integrated sensor on the Connectino will be used as exemplary data).
  • The correctly set up Arduino IDE as described in our „Getting started“ tutorial.
  • An AWS account. The registration is free and for the first year you get the AWS Free Tier, that should be sufficient for hundreds of projects.

This tutorial was verified to work with the following software versions of the Arduino library and WiFi module firmware:

  • Arduino Library: 1.0
  • Wifi module firmware: 1.0 and 1.1

Setup AWS IoT

The essential setup of AWS is explained in: AWS IoT Getting Started

After finishing the AWS IoT Getting Started guide, you should have:

  • an AWS IoT thing, with a DEVICE-NAME
  • 3 certificate files (*-private.pem.key, *-public.pem.key and *.pem.crt) attached to the thing
  • a policy attached to an activated certificate.

Upload Certificate to Connectino

Use the firmware creation tool (coming soon…) to get a file including the client certificate (*.pem.crt) and the private key (*-private.pem.key).

Now open your webbrowser and connect to the Connectino. If you are not logged in, click on the Login-Icon and log in then click on the Firmware Update Icon, select the previously generated *.fwu file and click Update.

Update the device shadow from your Connectino

With the client certificate loaded into your Connectino it can communicate securely with AWS IoT and has all the necessary access rights. To update the Connectino shadow in the AWS IoT, you have to publish messages on the MQTT shadow update topic. The following step-by-step guide will shw you how to do so.

Have a look at the provided sketch. The function connectMqtt() does the configuration and connection to the MQTT server, you need to provide the URL, port and the device name to the MQTT library function.

Keep in mind, that the MQTT configuration remains even if you restart the Connectino.

To get the log into the AWS IoT-Core-console, click on Manage, choose your device and click on Interact. Under HTTPS is the URL you’re searching for.


The setup() configures the MQTT client and starts it. The connection will be established as soon as the Connectino has access to the internet.

In the loop() the sensor value for the brightness is read out from the sensor and published to the AWS server every 10 seconds

Download Example: AWSBasic.ino

#include <ConnectinoMqtt.h> 
#include <Adafruit_VL6180X.h>

uint8_t val = 0;                 // variable to store the sensor-value
uint8_t sendVal = 0;             // last sended Value
char sendMsg[127];           // variable to store the MQTT-string in

ConnectinoMqtt mqtt = ConnectinoMqtt(StxSerial); // Create Mqtt-instance
Adafruit_VL6180X sensor = Adafruit_VL6180X();

void setup()
{
	// Initiate sensor
	sensor.begin();

	// Initiate Connectino Serial
	StxSerial.begin();
	StxSerial.add(mqtt);
	mqtt.setState(0); // turn off mqtt
	mqtt.setSessionConfiguration( // set MQTT Configuration
		"xxxxxxxxxxxxxx.iot.eu-west-1.amazonaws.com", // serveradress
		8883, // port
		"", // resource (stays empty for AWS)
		"Tutorial_Test"); // clientId / deviceId
	mqtt.setState(1); // turn on mqtt
	for(int i = 0; !mqtt.isConnected(); i++) delay(500); // wait for connection
}

void loop()
{
  /* val = sensor.readRangeStatus();// read the distance value */
	delay(10000);
	val = sensor.readRange();

	if(sendVal != val) // if sensorvalue differs from last sent value
	{
		// build mqtt-JSON - for AWS the structure of {state:{reported:{...}}} is necessary
		sprintf(sendMsg, "{\"state\":{\"reported\":{\"range\":%u}}}", val);
		// publish the JSON on the mqtt-topic
		mqtt.publish("\$aws/things/Tutorial_Test/shadow/update",sendMsg);
		sendVal = val;
	}
}

Check connectivity and device shadow

To check the connection state of your Connectino go to the managing page of your device and click on Activity.


To see, if the shadow of your device was updated correctly, click on Shadow and have a look at the Shadow state.
TUTORIALS