Muhammad Afzal
Published © Apache-2.0

Wio Terminal & AWS IoT Core

In this project you will learn how to send message from Wio Terminal to AWS IoT Core using Secure MQTT Protocol.

IntermediateProtip6 hours267
Wio Terminal & AWS IoT Core

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1

Software apps and online services

Arduino IDE
Arduino IDE
AWS IoT
Amazon Web Services AWS IoT

Story

Read more

Code

wio_aws_iot_core

Arduino
#include <AWS_IOT.h> //https://github.com/Seeed-Studio/Seeed_Arduino_rpcAWS
#include <rpcWiFi.h>
#include "SPI.h"
#include "secrets.h" // include your personal WiFi and AWS configuration. 

AWS_IOT hornbill;


static auto constexpr TOPIC_NAME = "wioterminal/pub";
static auto constexpr TOPIC_NAME_SUB = "wioterminal/sub";

int status = WL_IDLE_STATUS;
int tick=0,msgCount=0,msgReceived = 0;
char payload[512];
char rcvdPayload[512];

void mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad)
{
    strncpy(rcvdPayload,payLoad,payloadLen);
    rcvdPayload[payloadLen] = 0;
    msgReceived = 1;
}


void setup() {
    Serial.begin(9600);

    while (status != WL_CONNECTED)
    {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(WIFI_SSID);
        status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
        delay(5000);
    }

    Serial.println("Connected to wifi");

    if(hornbill.connect(HOST_ADDRESS, CLIENT_ID,
                AWS_CERT_CA, AWS_CERT_CRT, AWS_CERT_PRIVATE)== 0)
    {
        Serial.println("Connected to AWS");
        delay(1000);

        if(0 == hornbill.subscribe(TOPIC_NAME_SUB,mySubCallBackHandler))
        {
            Serial.println("Subscribe Successfull");
        }
        else
        {
            Serial.println("Subscribe Failed, Check the Thing Name and Certificates. RESET.");
            while(1);
        }
    }
    else
    {
        Serial.println("AWS connection failed, Check the HOST Address. RESET.");
        while(1);
    }

    delay(2000);

}

void loop() {

    if(msgReceived == 1)
    {
        msgReceived = 0;
        Serial.print("Received Message:");
        Serial.println(rcvdPayload);
    }
    if(tick >= 5)   // publish to topic every 5seconds
    {
        tick=0;
        sprintf(payload,"Hello from Wio Terminal : %d",msgCount++);
        if(hornbill.publish(TOPIC_NAME,payload) == 0)
        {        
            Serial.print("Publish Message:");
            Serial.println(payload);
        }
        else
        {
            Serial.println("Publish failed");
        }
    }  
    vTaskDelay(1000 / portTICK_RATE_MS); 
    tick++;
}

secrets.h

Arduino
// WiFi and MQTT configuration 
static auto constexpr WIFI_SSID = "-----WIFI-SSID";
static auto constexpr WIFI_PASSWORD = "-----WIFI-Password------";
static auto constexpr CLIENT_ID = "WioTerminal";

// hostname of your MQTT ATS endpoint 
static auto constexpr HOST_ADDRESS = "-------Region-------------;



// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
)EOF";

// Device Certificate
static const char AWS_CERT_CRT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
)KEY";

// Device Private Key
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----
)KEY";

Credits

Muhammad Afzal

Muhammad Afzal

22 projects • 105 followers
I am Software Eng having 13+ Years of experience. Hackster.io & Cayenne Mydevices Ambassador in Pakistan.

Comments

Add projectSign up / Login