[Archive] AWS IoT Core
This library has been archived and is no longer supported. The latest version of the AWS IoT Core Library can be found here.
The IOT_CORE library can be used to interact with the AWS IoT Core service to publish messages to the Message Broker's topic structure. For instance, using a temperature/humidity sensor, a user can publish the resulting readings straight from the sensor to a topic on IoT Core. The following information from the user is required to successfully publish to an IoT Core topic:
- Certificate: A secure certificate created on AWS IoT core used for authorization
- Key: The RSA private key for AWS IoT Core, used for authorization
- Host: The custom endpoint from AWS IoT Settings page, to connect to AWS IoT
- Topic: A topic to publish to on AWS IoT Core
After the initial setup of the IoT core settings on AWS and the obtainment of the required information, the user can perform two main operations using the IOT_CORE library: publish and post.
When creating the IOT_CORE object the following parameters must be set; cert
, key
, host
, and topic
.
These parameters can be set inside a typical JavaScript object as {key:value} pairs of type string, as follows:
Options
var iotOptions = {
cert: "CERTIFICATE",
key: "RSA_PRIVATE_KEY",
host: "host.to.iot.region.amazonaws.com",
topic: "topicToPublishTo",
};
Creation
Once the options are set, they can be passed in straight to IOT_CORE during creation:
var iotCore = new IOT_CORE(iotOptions);
Parameter Functions
After creating the iotCore
object, each of the aforementioned parameters may be changed by the corresponding function call with a passed in {value}
of String
type:
cert
:setCert({value})
key
:setKey({value})
host
:setHost({value})
topic
:setTopic({value})
If setting multiple parameters at the same time, the parameters can be put into a typical JavaScript type object and passed to the setParameters({parametersObject})
function, or each parameter set function can be chained, e.g. iotCore.setCert("NEW_CERTIFICATE").setKey("RSA_KEY").setHost("awsHost.com")
.
After creating the IOT_CORE library object and setting the initial parameters, the user is ready to publish to the set topic using either publish()
or post()
.
Main IoT Core Functions
POST
When calling the post({payload})
function, the user is required to send the accompanying payload object, which should either be a JSON object containing data (recommended) or a String
. The post
function will then use the set parameters to publish the payload to AWS IoT Core at the set topic. Post makes a post request over HTTPs.
PUBLISH
When calling the publish({payload})
function, the user is required to send the accompanying payload of type String
to publish to the AWS IoT core topic. The publish
function will then use the set parameters to publish the payload the set topic. Publish makes an mqtt publish call to publish data to the topic on IoT Core.
Example Script
The following script is an example of a user script after importing the IOT_CORE library along with an HTS221 sensor library.
The user activates interfaces on the NEQTO IO board and create a new I2C device object to interact with the HTS221 sensor.
The user then sets the initial IoT Core settings and creates an iot object that will be ready to publish to the set topic.
The user intends to measure and send 10 readings from the temperature/humidity HTS221 sensor at 10 second intervals to the IoT Core topic. A while loop is specified so that the readings are provided to the variable called tempHumi
. The tempHumi
reading result is a typical object in a JSON like format of {temp: <value>, humi <value>}
which is finally passed along as the payload to the iot.post()
method.
// IMPORTED LIBRARIES
// - HTS221
// - IOT_CORE
// Logging setup
log.setLevel(2); //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2); //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
log.clear();
//Use standard interface of NEQTO I/O board
nqEx.enI2CS(true);
nqEx.enI2CL(true);
//I2C Config
var baudrate = 400000;
var i2c = new I2C(1);
i2c.open(baudrate);
var hts = new HTS221(i2c);
hts.init();
var NUM_READINGS = 10;
var iteration = 0;
// Set IoT Core user options
var iotOptions = {
cert: "-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n",
key: "-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----\n",
host: "host.to.iot.core.amazonaws.com",
topic: "topicToPublishTo"
}
var iot = new IOT_CORE(iotOptions);
// Main Loop
// Read temperature and humidity and publish to iot core topic every 10 seconds
while (iteration < NUM_READINGS) {
// HTS returns an object like { temp: <value>, humi: <value> }
var tempHumi = hts.getResult();
iot.post(tempHumi);
setTimeout(10000).wait();
iteration += 1;
}