This is an attempt to easily perform basic tasks for aws iot core - things. It uses aws-iot-device-sdk-v2 . For more details check aws-sdk
Use git to clone aws_iot_js_simple_template and npm to install dependencies.
git clone https://github.com/mishrasatyam/aws-iot-js-template.git
cd aws-iot-js-template && npm install
Make sure your policy allows node js sdk and topics used by you.eg(topic is sensor here):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Receive"
],
"Resource": [
"arn:aws:iot:ap-south-1:77********36:client/sdk-nodejs-*",
"arn:aws:iot:ap-south-1:77********36:topic/sensor"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:ap-south-1:77********36:client/sdk-nodejs-*",
"arn:aws:iot:ap-south-1:77********36:topicfilter/sensor"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:ap-south-1:77********36:client/sdk-nodejs-*"
]
}
]
}
Assuming your thing name is thing1.
You will get files like root-CA.Crt , thing1.cert.pem, thing1.private.pem, thing1.public.key and start.sh .
Put all above files(except start.sh) in files folder.
Edit a config.json mentioning below fields(easily find endpoint and client_id in start.sh) and file paths.eg:
{
"endpoint":"a*************-ats.iot.ap-*****-1.amazonaws.com",
"root_ca_cert":"files/root-CA.crt",
"pem_cert": "files/thing1.cert.pem",
"private_key":"files/thing1.private.key",
"client_id":"sdk-nodejs-b*******-4***-4***-a***-1**********b"
}
node index.js
It will publish a message sending {Temperature:23.65}. You cn edit index.js to customise data as per your needs.