AWS IoT development for Arduino

Most IoT device developments use the Arduino board as the board target. Since Arduino shares schemes and designs, the makers make their own boards with custom Arduino models. Arduino models range from the basic Arduino board model to the advanced Arduino board model. The Arduino board is the best choice to introduce IoT development.

In this section, we will explore how to use Arduino Yún to interact with AWS IoT. For further information about Arduino Yún, I recommend that you read the official documentation at https://www.arduino.cc/en/Guide/ArduinoYun.

To work with AWS IoT using Arduino Yún, we perform the following steps:

  1. Install the Arduino software
  2. Connect Arduino Yún to a network through Ethernet or a Wi-Fi module
  3. Configure Arduino Yún for AWS IoT SDK
  4. Build a program for Arduino Yún

Each step will be performed. Now, you can follow the steps in detail, as follows:

  1. Firstly, you should install the Arduino software for your OS platform. You can download it at https://www.arduino.cc/en/Main/Software.
  2. The next step is to connect your Arduino Yún to the existing network. You should  join your Arduino Yún into an existing Wi-Fi hotspot. Then, configure your Arduino Yún to join the Wi-Fi network. You can also connect this board to a network through the Ethernet module:

Make sure your computer and Arduino Yún have the same network segment so you can access Arduino Yún over the network.

  1. The next step is to configure Arduino Yún for AWS IoT. You should install AWS IoT SDK for Arduino Yún at https://github.com/aws/aws-iot-device-sdk-arduino-yunYou can download the latest AWS IoT SDK for Arduino Yún at https://s3.amazonaws.com/aws-iot-device-sdk-arduino-yun/AWS-IoT-Arduino-Yun-SDK-latest.zip.
  2. Extract a ZIP file into a certain folder. In Chapter 1, Getting Started with AWS IoT, we have the certificate and private key files for AWS IoT. Put them in the AWS-IoT-Arduino-Yun-SDK/AWS-IoT-Python-Runtime/certs folder from the extracted AWS IoT SDK for Arduino Yún .
  3. Now, we configure and upload our certificate and private key files into Arduino Yún. For Mac and Linux, you can open the Terminal and navigate to a folder where the SDK file is extracted. You should see the AWSIoTArduinoYunInstallAll.sh file. Then, execute this file using the following commands:
$ chmod 755 AWSIoTArduinoYunInstallAll.sh
$ ./AWSIoTArduinoYunInstallAll.sh <Board IP> <UserName> <Board Password>

In the preceding code, <Board IP> is the IP address of your Arduino Yún. <UserName> and <Board Password> are the username and password to access your Arduino Yún. By default, if you do not change the password, the username is root and the password is arduino. This process will take several minutes. Please do not close the process.

  1. For Windows, you can use Putty and WinSCP to configure AWS IoT on Arduino Yún . You can download Putty at http://www.chiark.greenend.org.uk/%7Esgtatham/putty/download.html and WinSCP at http://winscp.net/eng/download.php. Then, you can type the following commands:
$ opkg update
$ opkg install distribute
$ opkg install python-openssl
$ easy_install pip
$ pip install AWSIoTPythonSDK==1.0.0

The following is a sample of the executing output on macOS:

  1. For a demo, we use a sample program from AWS IoT SDK for Arduino Yún. It is called BasicPubSub. You can find it on the FileExamplesAWS-IoT-Arduino-Yun-Library menu from the Arduino IDE. You should see the BasicPubSub menu. Click on it so you can open the BasicPubSub program, as shown in the following screenshot:
  1. You can see that there are two files—BasicPubSub.ino and aws_iot_config.h. The following is a program from the BasicPubSub.ino file:
...

aws_iot_mqtt_client myClient; // init iot_mqtt_client
char msg[32]; // read-write buffer
int cnt = 0; // loop counts
int rc = -100; // return value placeholder
bool success_connect = false; // whether it is connected

// Basic callback function that prints out the message
void msg_callback(char* src, unsigned int len, Message_status_t flag) {
if(flag == STATUS_NORMAL) {
Serial.println("CALLBACK:");
int i;
for(i = 0; i < (int)(len); i++) {
Serial.print(src[i]);
}
Serial.println("");
}
}

...
void loop() {
if(success_connect) {
// Generate a new message in each loop and publish to "topic1"
sprintf(msg, "new message %d", cnt);
if((rc = myClient.publish("topic1", msg, strlen(msg), 1, false)) != 0) {
Serial.println(F("Publish failed!"));
Serial.println(rc);
}

...

delay(1000);
  1. Now we can open the aws_iot_config.h file. We should configure the certificate and private key files on the aws_iot_config.h file.  Change AWS_IOT_MQTT_HOST for the IoT endpoint. Also, change the AWS_IOT_CERTIFICATE_FILENAME and AWS_IOT_PRIVATE_KEY_FILENAME values for the certificate and private key files for your IoT device:
#ifndef config_usr_h
#define config_usr_h

// Copy and paste your configuration into this file
//===============================================================
#define AWS_IOT_MQTT_HOST “<aws-iot-host>” // your endpoint
#define AWS_IOT_MQTT_PORT 8883 // your port
#define AWS_IOT_CLIENT_ID "My_ClientID" // your client ID
#define AWS_IOT_MY_THING_NAME "My_Board" // your thing name
#define AWS_IOT_ROOT_CA_FILENAME "root-CA.crt" // your root-CA filename
#define AWS_IOT_CERTIFICATE_FILENAME “thing.cert.pem" // your certificate filename
#define AWS_IOT_PRIVATE_KEY_FILENAME “private.key" // your private key filename
//===============================================================
// SDK config, DO NOT modify it
#define AWS_IOT_PATH_PREFIX "../certs/"
#define AWS_IOT_ROOT_CA_PATH AWS_IOT_PATH_PREFIX AWS_IOT_ROOT_CA_FILENAME // use this in config call
#define AWS_IOT_CERTIFICATE_PATH AWS_IOT_PATH_PREFIX AWS_IOT_CERTIFICATE_FILENAME // use this in config call
#define AWS_IOT_PRIVATE_KEY_PATH AWS_IOT_PATH_PREFIX AWS_IOT_PRIVATE_KEY_FILENAME // use this in config call

#endif

Save all the changes. 

  1. To execute the program, compile and upload this sketch program into Arduino Yún. After it is uploaded, you can open the Serial Monitor tool with baud rate 115200. You can see the program output on the Serial Monitor tool:

You can verify that this is executing by opening AWS IoT Console to see the incoming messages from Arduino Yún .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.136