Introduction: Arduino Using AWS IoT Serivce
Have you ever wanted to control your IoT device through cloud? There are many ways to achieve such operations, we'll show you a simple way that uses Amazon Web Services (AWS) to control a single LED connected to Arduino device.
AWS IoT is a platform that enables you to connect devices to AWS Services and other devices, secure data and interactions, process and act upon device data, and enable applications to interact with devices even when they are offline. Please refer to the link for basic AWS information: https://aws.amazon.com/iot/how-it-works/. Onc can also refer to http://docs.aws.amazon.com/iot/latest/developergui... for all the detail operstions.
We'll only need one Arduino compatible board plus one LED for this project. The board we are using is Ameba Arduino board which can be found in eBay. The reason we use this board is Ameba Arduino already have 802.11 wireless built-in plus their SDK already have MQTT library that can be called to communicate with AWS.
Step 1: AWS Registration
We first need to create an AWS account and sign in to it. Please go to https://aws.amazon.com/ to create an account then log in to the Amazon Management Console and click “AWS IoT” to get to AWS IoT Web page. Once you get to AWS IoT page, please select the region that close to you at the upper right corner so you can have best service quality. After you have chosen the nearby region for your location, click on "Get Started" to start.
Step 2: Create a Thing
After you clicked "Get Started", you will enter a page default to “Create a thing”. Fill in the name “ameba” and click “Add attributes” on the lower left side. Attributes represent the status of ameba. The value of the attributes can be updated directly by ameba or by the control side. For the latter case, the control side request ameba to set the attribute to desired value. Here we add a attribute named “led” with value “0”, and click “Create”.
After create a thing and its attribute, we need to create a policy. Policy is used to restrict the functions that a “thing” can do. Click on “Create a Policy”. Here we do not place policy on ameba. Fill in “amebaPolicy” in the Name field, “iot:*” in Action field and “*” in Resources field. Then check “Allow”. Finally, click “Add statement”. You can add some statements if you so wish. Click “Create” to finish.
Step 3: Certifications
After the above step, you should see two items, “thing” and “policy”, appear in AWS IoT page. Next, we have to set up the TLS certificate so we can used to authenticate the device. Click “Create a certificate”. You can choose to use user-defined certificate or generate a certificate by AWS IoT. In this example, we chose the easy way and click on “1-Click certificate create” to generate TLS certificates. Now you should see three links. Please click each of the three links to download “public key”, “private key” and “certificate”.
Step 4: Attach to Certificate
At this time, you should see three square items, “certificate”, “policy” and “thing”, appear in left bottom of AWS IoT page. Next, we need to attach the policy and the thing to the certificate. Click “certificate” (marked "INACTIVE"), then click “Attach a policy” in the “Actions” drop down menu (which is located on the right side). Fill in the name of the policy we created in the previous step, i.e., “amebaPolicy”, and click “Attach”. Same operation for the "thing". Click “Attach a thing” in the “Actions” drop down menu. Fill in the name of the thing we created in the previous step, i.e., “ameba”, and click “Attach”. Refresh the page, and click “certificate” again. You can see two sqaure item “ameba” and “amebaPolicy” listed at the bottom of information area on the right hand side. Let's activate the certificate now. Click "certificate", and click “Activate” in the “Actions” drop down menu.
Step 5: Finished Look
Now we have finished all the basic requirements for AWS IoT registration. Click “ameba” and look at the information appear at the right side:
- REST API endpoint: In the value “https://a2zweh2b7yb784.iot.ap-southeast-1.amazonaws.com/things/ameba/shadow”, the part “a2zweh2b7yb784.iot.ap-southeast-1.amazonaws.com” is the MQTT Broker server address.
- MQTT topic:The value “$aws/things/ameba/shadow/update” represents the MQTT topic we will use in the AWS IoT Shadow service (if we use MQTT only, without AWS IoT Shadow service, then we can specify other topic name). It is recommended to use “$aws/things/ameba/shadow/update” here.
Step 6: Ameba Applications
You will need to install at least Ameba SDK version 1.1.1 to have this AWS sketch. Open “File” -> “Examples” -> “AmebaMQTTClient” -> “amazon_awsiot_basic”. There are five places you need to change according to your setup:
- SSID: Fill in the correct SSID of your AP
- Password: Fill in the correct password of your AP
- MQTT Server: Please fill in the MQTT Broker server address that we got in the previous step.
- Certificate: Please use the certificate file, certificate.pem.crt, that we download before to fill in this space.
- Private Key: Please use the private key file, private.pem.key, that we download before to fill in this space.
When applying the download key to the sketch, please edit its format as used in the sketch:
- Add the new line character “\n” at the end of each line.
- Add double-quote at the beginning and the end of each line.
- To concatenate each line as a string, add “\” at the end of each line.
- The last line ends with semicolon.
The root CA certificate is already done for you. You can however download it and verify: https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
Step 7: Modify Status Through Cloud
After all the required modification is done, compile and run and upload the sample code to Ameba and press the reset button to start running the new code. If you open the serial monitor, you can see Ameba first will try to connect with AP, then authenticate with AWS server. You can see the LED is on by default (please attach the longer lead to digital pin 10 and shorter lead to ground). Let's go back to AWS IoT page. Now, check the thing information of “ameba” shown in AWS IoT console. You should see the published message in “Shadow status” which the led status is 1. Let's try to turn off the led. Click “Update Shadow”, you can see a “Shadow state” text field with default content. Modify the content: { "desired": { "led": 1 } }, change the value from 1 to 0, then click “Update shadow”. Voila, you can see the led in the Ameba board has been turned off, through cloud!
15 Comments
Question 1 year ago
Can I use Arduino UNO R2 instead?
2 years ago
Hi,
ESP32-Mqtt was not connected using Azure SSL Certificate
I am using the below code for connecting with ssl certificate was not working.
#include <WiFiClientSecure.h>
const char* ssid = "xxx"; // your network SSID (name of wifi network)
const char* password = "xxx"; // your network password
const char* server = "xxx.azure-devices.net"; // Server URL
const char* test_root_ca=
"-----BEGIN CERTIFICATE-----\n"
"-----END CERTIFICATE-----\n";
// You can use x.509 client certificates if you want
const char* test_client_key =
"-----BEGIN RSA PRIVATE KEY-----\n"
"-----END RSA PRIVATE KEY-----\n"; //to verify the client
const char* test_client_cert =
"-----BEGIN CERTIFICATE-----\n"
"-----END CERTIFICATE-----\n"; //to verify the client
WiFiClientSecure client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}
Serial.print("Connected to ");
Serial.println(ssid);
client.setCACert(test_root_ca);
client.setCertificate(test_client_cert); // for client verification
client.setPrivateKey(test_client_key); // for client verification
Serial.println("\nStarting connection to server...");
if (!client.connect(server, 8883)){
Serial.println("Connection failed!");
}
else {
Serial.println("Connected to server!");
}
}
void loop() {
// do nothing
}
2 years ago
Can I use this SDK and library to send message from U-blox SARA module used as GSM to AWS cloud?
Or anyone have any other library to send data over AWS from above GSM module.
Thank you
6 years ago
can I get the code link here
Reply 4 years ago
You can find the arduino code here:
https://raw.githubusercontent.com/Ameba8195/Arduino/master/hardware_v2/libraries/MQTTClient/examples/amazon_awsiot_basic/amazon_awsiot_basic.ino
Question 4 years ago on Introduction
*Which* Ameba board is this? RTL8195 or RTL8170 (looks like the 8195, but don't want to spend $ on wrong thing).
4 years ago
I would agree the Ameba board is not Arduino, but it is close and can work with Arduino IDE. I understand the issue is that it needs the right firmware to work with arduino ide (there is mbed and then there is arduino).
Bought it from Amazon and I am still struggling to get it to work. Apparently the board comes with an outdated firmware, upgraded the firmware and now I think it's bricked... still working with the Realtek Ameba folks to resolve this.
4 years ago
not sure i'd describe this ameba board as 'arduino'....it may be programmable via the arduino IDE but its worlds apart...
5 years ago
Can it be used with MKR 1000 arduino board? I don't see any tutorial for the same all over the internet.
Reply 5 years ago
I believe any Arduino board with wifi should be able to get this to work. It's the SW make the magic. Make sure you have MQTT library support.
5 years ago
where is your library for this project? thanks
Reply 5 years ago
If you have Ameba Arduino board, you'll get the library after installing their SDK.
6 years ago
Can I do this on my ESP8266? I'm a bit confused with the whole ameba arduino thing.
Reply 5 years ago
Have you successfully connected your ESP8266 with AWS IOT ..?
Reply 6 years ago
As long as your device can connect to Internet, it should work for you, but you need to modify the Arduino sketch to suit your platform.