How to send data from device to AWS IoT

AWS Jan 6, 2022

Hello People. This article discusses about how to send data from device to AWS IoT. We should send data securely from Arduino to AWS IoT core using MQTT protocol and then we can do many things. For example, hook lambda function with data and save it to DynamoDB or S3.  

Pre-requisites

Arduino with ESP8266 or ESP32, Arduino IDE, A Computer(PC) and a Cable between PC and Arduino, AWS account. Even if you don’t have arduino device, you can imitate Arduino request in AWS IoT.

Steps:

Create a device certificate

You need to create a certificate to each device for security. Without certificates, attackers can send tons of requests to your AWS IoT core. You can create a certificate in AWS console. Go to AWS console and search IoT core. IoT core page. Go to Secure > Certificates tab. Please click Create button at the top right and proceed to One-click certificate creation.

After that, you need to do these things. To use device certificate and private key, please download cert.pem and private.key . Click Activate button. Attach policy to allow device to write and read IoT services.

Policy name can be anything. For Action, because all IoT actions should be allowed, please type iot* . And those actions should be allowed to all resources. That’s why we type Resouce ARN is * .

After creating a certificate and policy, please go to Secure > Certificates tab again. You can see what you have created. But your policy is not attached to the certificate yet. So attach the policy.

How to send data from device to AWS IoT

Create a sketch

To connect Arduino and AWS IoT core using MQTT, you should usepubsubclient . If you know Arduino well, you can create your own sketch. But there are boiler plates for pubsubclient. Almost all configuration ofpubsubclient and WiFi is available on boiler plates. Please choose a proper boilerplate and copy and paste it to your sketch.

However you must change some things in the boiler plate. They are ssid and passwordof WiFi, awsEndpoint, certificate_pem_crt and private_pem_key.

SSID and password of WiFi is simple. You just type your WiFi configuration. awsEndpoint is a AWS IoT core endpoint. You can check it in AWS console. Please go to IoT core > Settings. You can copy and paste the Endpoint. Because endpoint prefix is account specific, it’s hidden below. If you have aws cli, you can type $ aws iot describe-endpoint .

Now coming to certificate_pem_crt and private_pem_key . You have already downloaded them from AWS IoT core page. You must use your own device certificate and private key.

So please upload the sketch to your Arduino and run it. After running it, you open a serial monitor. Data sending has started. You send msg.c_str() to outTopic . outTopic is a topic name in AWS IoT core. If you change msg.c_str() to json, you can send json data.

Check data you sent

Go back to AWS IoT core. In the Monitor tab, you can find successful connections. Go to Test tab. Because you publish data from Arduino, AWS IoT core must subscribe it. Please type outTopic to Subscription topic. It’s the name of topic you used. And if you click Subscribe button. You can see the results.

Hope this article on How to send data from device to AWS IoT is useful to you. Please read Node Red with AWS IOT



Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.