AWS IoT Core is a managed cloud platform that enables connected devices to easily and securely interact with cloud applications and other devices. It supports billions of devices and trillions of messages, processing and routing these messages to AWS endpoints and other devices reliably and securely. This functionality allows applications to continuously track and communicate with all your devices, even when they aren’t connected.
The service provides a secure and efficient connection between devices and the AWS cloud without the need to provision or manage servers. Devices connected to AWS IoT Core can automatically set up secure communications using mutual authentication and encryption at all points of connection, ensuring that data is never exchanged without proven identity.
Key features of AWS IoT Core include:
Message Broker for MQTT: a low-latency, publish/subscribe messaging transport protocol ideal for connecting remote devices to each other and to applications.
Rules Engine: transforms and routes the data to other AWS services such as AWS Lambda, Amazon Kinesis, Amazon S3, and more for processing, storage, or analytics.
Device Shadow: provides a persistent virtual version, or “shadow”, of each device that includes the device’s latest state so that applications can read messages and interact with the device even when it is offline.
Security and Identity Service: offers mutual authentication and encryption at all points of connection, ensuring that data is never exchanged between devices and AWS IoT Core without a proven identity.
With AWS IoT Core, businesses and developers can build IoT applications that gather, process, analyze, and act on data generated by connected devices without managing any infrastructure. This robust integration not only simplifies the management of IoT devices but also enhances the capabilities to perform real-time analytics, create new applications, and improve decision-making processes.
In this learning path, you create an application that emulates the weather station and sends hypothesized sensor readings such as temperature and humidity to the AWS cloud.
Before you start, ensure you have installed and prepared the following tools:
You now create a Node.js project. Open the Command Prompt and type the following commands:
mkdir AWS.WeatherStation && cd AWS.WeatherStation
Next, create a new Node.js application by typing:
npm init -y
This will create the package.json with default values. Open this file and modify it as follows:
{
"name": "aws.weatherstation",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"aws-iot-device-sdk": "^2.2.13"
},
"keywords": [],
"author": "",
"license": "ISC"
}
To continue, implement the weather station emulator. Within the AWS.WeatherStation folder, create a new file named emulator.js. Then, modify the file using the following code snippet:
const getRandomValue = (min, max, digits) => {
const randomNumber = Math.random() * (max - min) + min;
return randomNumber.toFixed(digits);
};
const randomSensorReadings = (deviceId) => ({
deviceId,
timestamp: new Date(),
temperature: getRandomValue(20, 40, 2),
humidity: getRandomValue(50, 60, 0),
pressure: getRandomValue(1000, 1050, 1)
});
exports.randomSensorReadings = randomSensorReadings;
This code defines two functions, getRandomValue and randomSensorReadings, which are used for generating simulated sensor readings. Additionally, this code is designed to be part of a Node.js application, as indicated by the use of exports to make the randomSensorReadings function available to other parts of the application.
The getRandomValue(min, max, digits) function generates a random floating-point number within a specified range (min to max) and formats it to a specified number of decimal places. Conversely, the randomSensorReadings(deviceId) function creates an object representing sensor readings for a device, including a device identifier, a timestamp, and readings for temperature, humidity, and pressure.
AWS IoT Core utilizes MQTT (Message Queuing Telemetry Transport), a lightweight messaging protocol designed for devices with limited network bandwidth and resources, making it highly suitable for many Internet of Things (IoT) applications. MQTT operates on a publish/subscribe model that facilitates efficient message exchange between devices without the need for complex network programming.
To prepare the application for using MQTT with AWS IoT Core, implement the common.js file in the same directory, AWS.WeatherStation:
const rootCertDir = '<TYPE_YOUR_DIRECTORY_HERE>';
const awsConnectionInfo = {
keyPath: `${rootCertDir}WeatherEmulator.private.key`,
certPath: `${rootCertDir}WeatherEmulator.cert.pem`,
caPath: `${rootCertDir}AmazonRootCA1.pem`,
host: '',
clientId: 'WeatherStationEmulator'
};
const msDelayTime = 1000;
const topicSensorReadings = 'Emulators/Weather/SensorReadings';
const topicTelemetryControl = 'Emulators/Weather/Control';
exports.awsConnectionInfo = awsConnectionInfo;
exports.msDelayTime = msDelayTime;
exports.topicSensorReadings = topicSensorReadings;
exports.topicTelemetryControl = topicTelemetryControl;
This code snippet is a JavaScript module designed to set up and export configuration settings for a weather station emulator using AWS IoT Core services. Specifically, rootCertDir holds the directory path where the AWS IoT certificate files are stored. The placeholder <TYPE_YOUR_DIRECTORY_HERE> is meant to be replaced with the actual directory path on your system. You will obtain the certificates in the next “how-to” section.
awsConnectionInfo is an object that stores essential information required to connect to AWS IoT Core, including:
We have three constants used to control messaging and timing:
Finally, the module exports several settings and configurations using Node.js module exports, making awsConnectionInfo, msDelayTime, topicSensorReadings, and topicTelemetryControl accessible to other parts of the application. These details are crucial for connecting to, and interacting with, AWS IoT Core.
In an application, these settings enable a device or emulator to establish a connection to AWS IoT Core, allowing it to securely send and receive data. The defined MQTT topics are essential for the pub/sub mechanism, permitting the device to publish its sensor data and subscribe to commands that can alter its operation, such as turning on and off or adjusting parameters.
You now implement the code to simulate a weather station that interacts with AWS IoT Core for device management and data transmission. This demonstrates a typical IoT application scenario where device states are remotely controlled and monitored. To begin, create a new file named app.js under the AWS.WeatherStation directory:
'use strict';
// Dependencies
const common = require('./common.js');
const emulator = require('./emulator.js');
const awsIot = require('aws-iot-device-sdk');
// Connect to AWS
const device = awsIot.device(common.awsConnectionInfo);
// Variables
let telemetryIntervalHandle;
let isTelemetryActive = true;
// Device module event handlers
device.on('connect', () => {
console.log('Connected to AWS IoT');
device.subscribe(common.topicTelemetryControl);
telemetryIntervalHandle = setInterval(() => {
if (isTelemetryActive) {
// Synthesize telemetry data
const telemetryEntry = JSON.stringify(emulator.randomSensorReadings('WeatherStationEmulator'));
// Publish data to the cloud
device.publish(common.topicSensorReadings, telemetryEntry);
// Log current data to the console
console.log(telemetryEntry);
} else {
console.log('Telemetry is inactive');
}
}, common.msDelayTime);
});
device.on('close', (err) => {
console.log('Connection closed');
clearInterval(telemetryIntervalHandle);
});
device.on('error', (err) => {
console.log('Error', err);
});
device.on('message', (_topic, payload) => {
const messageObject = JSON.parse(payload.toString());
if (messageObject.isTelemetryActive !== undefined) {
isTelemetryActive = messageObject.isTelemetryActive;
} else {
console.log('Unexpected structure of the control command');
}
});
This module integrates with AWS IoT using the AWS IoT Device SDK. It is designed to connect a device emulator, handle device communications, and manage telemetry data for a simulated weather station.
The code imports necessary modules: common.js, emulator.js, and aws-iot-device-sdk. The first two modules have been already described here. The latter, aws-iot-device-sdk, is a library that enables devices to interact with AWS IoT services.
The module creates the AWS IoT device connection, which establishes a connection to AWS IoT by creating a device instance with configuration settings provided by common.awsConnectionInfo.
The module has two variables:
The module includes four event handlers:
Finally, the module manages telemetry data. It periodically generates and publishes telemetry data including weather conditions like temperature, humidity, and pressure, using the emulator module. This data is serialized to JSON format and logged in the console for monitoring.
You have successfully developed an application that can interact with AWS IoT Core, designed to simulate a weather station. This setup enables the application to manage device communications effectively and send telemetry data for remote monitoring. In the next steps, you register this application within AWS IoT Core to establish its credentials and connections settings formally. Once registered, you launch the application to begin streaming telemetry data to the cloud, allowing us to monitor and control the device states in real-time from AWS IoT Core’s platform. This will complete the setup for a fully functional IoT application that leverages AWS’s powerful cloud capabilities.