# How to integrate with AWS IoT Core

### The Business Goal <a href="#ka6ksq5i605t" id="ka6ksq5i605t"></a>

Did you know the **Datacake** platform can integrate with **AWS Cloud** using the **MQTT protocol**, enabling it to receive MQTT messages originating from AWS-managed devices? This capability opens up various possibilities, allowing users to leverage Datacake's intuitive interface to explore and make data actionable.

**Benefits of Datacake and AWS Cloud Integration:**

1. **Real-time Data Ingestion:**
   * Datacake's integration with AWS Cloud enables real-time ingestion of MQTT messages from AWS-managed devices.
   * This capability is particularly valuable for applications where timely access to data is crucial, such as IoT monitoring, industrial automation, and real-time analytics.
2. **Simplified Data Exploration:**
   * Datacake's user-friendly interface simplifies data exploration, allowing users to quickly visualize, analyze, and interpret data received from AWS-managed devices.
   * This eliminates the need for complex data engineering or programming skills, making it accessible to users of all technical backgrounds.
3. **Actionable Insights:**
   * Datacake empowers users to derive actionable insights from the data received from AWS-managed devices.
   * Customizable dashboards and visualizations enable users to monitor key metrics, identify trends, and make informed decisions based on real-time data.

In this article, I will explain the integration between the Datacake platform and the AWS Cloud using the MQTT protocol. That is the first step to unlocking the full potential of IoT data, gaining actionable insights, and driving data-driven decision-making to optimize operations and achieve business outcomes.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F56PT3ThBiVsDjYJAnYs8%2F0.png?alt=media)

You can experiment with Datacake's capabilities without disrupting your operations since no device-level configuration changes are needed.

I will guide you through the AWS and Datacake setup. Together, we will create the end-to-end working solution using a simulated device. I will explain every step, so no AWS, Datacake, or MQTT knowledge is required.

### Solution Overview <a href="#c5w0exx2x15n" id="c5w0exx2x15n"></a>

This is the overview of the end-to-end solution:

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FluOJY14QbbdlE4X57NkK%2F1.png?alt=media)

[Datacake](https://datacake.co/) is a multi-purpose, low-code IoT platform that requires no programming skills and minimal time to create custom IoT applications.

[AWS IoT](https://aws.amazon.com/iot/) can support billions of devices and trillions of messages. It can process and route those messages reliably and securely.

Combining those platforms creates a highly scalable yet easy-to-use solution.

### Datacake Setup <a href="#jlxkvp4d93ok" id="jlxkvp4d93ok"></a>

Let’s start by configuring the Datacake platform.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FvEjXFPdYZrHGYiR9dsRY%2F2.png?alt=media)

Log into your Datacake account and create a Workspace. I called mine “AWS Workspace,” but you can use any name relevant to your use case.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2Fj9O669HJfonDpUgNivwW%2F3.jpeg?alt=media)

Our objective is to seamlessly integrate Datacake with the AWS IoT platform. To accomplish this, we will utilize the Generic API device. This device supports the MQTT protocol, essential for establishing connectivity between the AWS Cloud and Datacake.

[MQTT](https://mqtt.org/) (Message Queuing Telemetry Transport) is a specialized messaging protocol tailored for the Internet of Things (IoT) domain. Developed and managed by the [OASIS](https://www.oasis-open.org/) (Organization for the Advancement of Structured Information Standards), MQTT offers a lightweight publish/subscribe messaging transport.

Use the web interface to add a new Generic API device:

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FmT28a7Nnj6w8g6pOahN5%2F4.jpeg?alt=media)

Datacake leverages Products to manage a fleet of connected devices effectively. Let’s create a new Product for our use case.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FZJ0GoesWZTcZC41jzwRt%2F5.jpeg?alt=media)

Fill in the attributes characterizing your first device. Provide the Serial Number and Name to describe your equipment.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FN0Dgy4BmRqwEcvonbYfo%2F6.jpeg?alt=media)

To conclude this step, select a billing plan. Datacake offers a Free Plan, allowing users to explore the platform without financial commitment. This option provides all the necessary features to implement our use case.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FcG4tnj6dkaWUCqVjivea%2F7.jpeg?alt=media)

Congratulations! You successfully added your first device to the Datacake platform!

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FyO9sBptxGJyrOYr5BHMB%2F8.jpeg?alt=media)

Let’s review our progress: We created the **Workspace**, **Product**, and **Device**.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FeMsplV5CZbWAGdLRSkBw%2F9.png?alt=media)

But how can we link that Device to our physical equipment connected to the AWS Cloud? To achieve that goal, we will utilize the MQTT protocol.

### MQTT configuration <a href="#i2zpg6gy1nz" id="i2zpg6gy1nz"></a>

Select your **Device** in the Datacake platform and scroll down to the **MQTT configuration**.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2Fc0JQHG955V5vTfe0kkqr%2F10.jpeg?alt=media)

As the hist suggests, the MQTT integration enables connection to an external MQTT broker. In the presented scenario, we will utilize the MQTT broker exposed by AWS IoT Core.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FKqHdAxIbJb83kMPzG2Qy%2F11.png?alt=media)

Let’s start by clicking the Add new MQTT Broker button to check the required configuration.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F5WOsONPZPslapBNJXAS5%2F12.jpeg?alt=media)

To ensure data confidentiality during communication between AWS and Datacake, we will leverage the MQTTS protocol. It provides an additional layer of security by encrypting the communication using Transport Layer Security (TLS). This encryption ensures that our data remains protected and confidential throughout the communication.

AWS IoT Core uses port 8883 for MQTT, so that is the port number we need to define.

To establish trust between AWS and Datacake, we will use **X.509 Certificates**. Certificates allow both parties to confirm identities and securely communicate with each other. I will show you how to obtain those certificates and the hostname from your AWS Account in the next step.

### AWS Setup <a href="#nb41u2gmz9ax" id="nb41u2gmz9ax"></a>

Switch to the AWS Console to prepare AWS IoT Core for integration with Datacake.

In the AWS Console, search for the **IoT Core** service.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FVzBnjAGqBxJN1UE4rjFN%2FDatacake_009.jpeg?alt=media&#x26;token=7be232ae-6103-4bf5-9f0b-02b3787e500f" alt=""><figcaption></figcaption></figure>

On the AWS IoT page, go to settings (bottom left part of the screen) and copy the **Endpoint** value. We will use it as the **Hostname** of the MQTT Broker.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FGs0udP0qYk9tSIVVdD7x%2F14.jpeg?alt=media)

To obtain the X.509 Certificate, go to the Security → Certificates and click on the “Create certificate”.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FcBLS6Dr0lYABdhrTDyKL%2F15.jpeg?alt=media)

Leave the Auto-generated certificates option and change the Certificate status to **Active** so that Datacake can use this certificate to communicate with AWS.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FA8cPy2CZyDy4Z4HvX9Lr%2F16.jpeg?alt=media)

Download the following files:

* Device certificate
* Private key file
* Amazon Root CA1

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FYYhfdPIH6xZmsZSvfWCq%2F17.jpeg?alt=media)

We use the X.509 Certificates for **authentication** - to confirm the **identity** of a device or application.

Additionally, we need to **authorize** Datacake to communicate with the AWS IoT Core. To provide that permission, we use the **AWS IoT Policies**.

Go to Security → Policies and hit “Create”.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FWwePjOBuzuPEaeikU8jI%2F18.jpeg?alt=media)

I will call my Policy a “DatacakePolicy”, but you can use whatever name suits you. As a good design practice, the name should represent the intent of a given Policy.

Then select “Policy examples”. Search for “ticket”, pick “Publish/Subscribe - Subscribe to and receive messages on a ticket”, and hit “Add to policy”.

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FDr51FSzL2BoIq6nPDAM5%2F19.jpeg?alt=media)

Remove the first row and switch to the JSON view:

![](https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F19YLuZpQMMDBMqm335WV%2F20.jpeg?alt=media)

We need to adjust the **IoT Policy** to our needs. I propose the following configuration:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:eu-west-1:123123123:client/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:eu-west-1:123123123:topicfilter/aws-workspace/aws-integration/*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:eu-west-1:123123123:topic/aws-workspace/aws-integration/*"
    }
  ]
}

```

Let me break it down for you:

`123123123` - specifies the AWS Account ID, this value is unique for every AWS Account. Do not change the integer provided by the sample policy!

`"Action": "iot:Connect"` - permits connecting to the AWS IoT Core.

`"Action": "iot:Subscribe"` - permits subscribing to a specified MQTT Topic (`aws-workspace/aws-integration/*` in our case).

`"Action": "iot:Receive"` - permits receiving MQTT messages from a specified MQTT Topic.

When defining the MQTT Topic Structure, we should start from a generic level and follow with more specific parts. I used the `aws-workspace` representing my Datacake Workspace and the Datacake Product name: `aws-integration`.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FBEV0JI6alqJto1CQKXZ5%2FDatacake_017.jpeg?alt=media&#x26;token=7b10f929-b0ac-4271-806b-6fd0231542e8" alt=""><figcaption></figcaption></figure>

That approach will ensure the separation of data managed by distinct workspaces and products. I suggest it to avoid conflicts and maintain data confidentiality for every Product.

Update the Policy document and create the **IoT Policy**.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FiLuZa8MdRZMbDlArgcbc%2FDatacake_018.jpeg?alt=media&#x26;token=06dd4d35-db38-439b-a639-fbe62c04cf61" alt=""><figcaption></figcaption></figure>

To establish the integration between AWS IoT and Datacake, we must link the **X.509 Certificate** with the **IoT Policy**. Currently, there is no such connection. Without an attached Policy, the X.509 Certificate does not allow any action.

To resolve this, navigate to the Security → Certificates. Select the checkbox near the certificate and choose "Attach Policy" from the Actions menu.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FUZ6DKIF92kxIjao9NNGM%2FDatacake_019.jpeg?alt=media&#x26;token=a2cfb2ab-1f50-4082-a478-7219fe3ac102" alt=""><figcaption></figcaption></figure>

Pick the IoT Policy we created and hit Attach.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2Fz89I0fj0oiqmqKX5VNFL%2FDatacake_020.jpeg?alt=media&#x26;token=d89e3064-2b08-4797-b1d1-a13c8050533c" alt=""><figcaption></figcaption></figure>

### Datacake Setup <a href="#id-6hs1dhmevaa6" id="id-6hs1dhmevaa6"></a>

Return to the Datacake platform to complete the configuration.

Go to Devices → Configuration, find the “MQTT Configuration” section, and hit “Add New MQTT Server”. Fill the form with data gathered in the previous steps.

Use any text editor to view the content of the Private Key and X.509 Certificates.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FNbQvknxJiDzZRrjIpN9p%2FDatacake_021.jpeg?alt=media&#x26;token=e4f95f66-d2b2-480a-ac8f-655aaf093df3" alt=""><figcaption></figcaption></figure>

This is how the completed form looks on my end. You will have different values, but the structure should be similar.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F7xW5yWSwKGcv60J40vAp%2FDatacake_022.jpeg?alt=media&#x26;token=e0f870d6-8928-48b9-ab8d-543c7965bb9a" alt=""><figcaption></figcaption></figure>

Hit the “Test Connection” button to verify the connectivity between AWS IoT Core and Datacake.

You should get the “Connection successful” pop-up. Click on the “Add MQTT Server” to save your configuration.

Congratulations! We successfully established the MQTT integration between AWS and Datacake. AWS will forward MQTT Messages sent to the `aws-workspace/aws-integration/#` MQTT Topic to Datacake.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F1TmB24pUWJ9li3pjsQMW%2Fdatacake_v01-solution_03.png?alt=media&#x26;token=73184709-6469-455f-b2ad-6659af5b933a" alt=""><figcaption></figcaption></figure>

Now, we have to enable Datacake’s Devices to receive and store those MQTT Messages.

Go to the “Temp Sensor 0001” Configuration and scroll down to the **Fields** section:

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FdyC96OeyaU0lWlFg008c%2FDatacake_023.jpeg?alt=media&#x26;token=29df6424-0b5d-48af-9e6c-b6521b6d4ceb" alt=""><figcaption></figcaption></figure>

Fields describe the attributes stored by devices. In our example, we use a simulated temperature sensor, so add a Field describing temperature readings:

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2Fm6zIKap4o7KoDugS8Awl%2FDatacake_024.jpeg?alt=media&#x26;token=aae5dc4b-773d-4292-9ac7-6fd84a520cf1" alt=""><figcaption></figcaption></figure>

Now, we need to create the **MQTT Uplink Payload Decoder**. Uplink decoders subscribe to MQTT Topics, process incoming MQTT Messages, and store them.

Find the MQTT Uplink Decoders section and click on “Add Uplink Decoder”.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FRv153Tu40eHrRvCMjM7G%2FDatacake_025.jpeg?alt=media&#x26;token=46567361-e3f4-47ce-906a-cd6c06c29484" alt=""><figcaption></figcaption></figure>

We want to enable our devices to receive MQTT Messages from the `aws-workspace/aws-integration/<deviceid>` MQTT Topic, where the `<deviceid>` represents the ID of a specific device. To achieve that functionality, our Uplink Decoder has to subscribe to the `aws-workspace/aws-integration/+` MQTT Topic.

The `"+"` is a single-level wildcard; in our case, it will match any Device ID.

Use the following code as the Decoder function:

```javascript
function Decoder(topic, payload) {
    
    // Extract the Device ID from the MQTT Topic.
    dev = topic.split("/")[2];
    
    // Parse the payload of the MQTT Message.
    msg = JSON.parse(payload);
    
    // Obtain the temperature reading from the MQTT Message.
    temperature = msg.temp
    
    return [{
        device: dev,
        field: "TEMP",
        value: parseFloat(temperature),
        timestamp: Date.now()
    }];
}

```

The picture below presents a complete definition of the Uplink Decoder.

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FwfdQo7QpSg3MTfAZpSvV%2FDatacake_026.jpeg?alt=media&#x26;token=dcd241ad-5843-4cce-87b7-e6f3a549db44" alt=""><figcaption></figcaption></figure>

### End-to-end Test <a href="#cn6n2mwq2emf" id="cn6n2mwq2emf"></a>

It is time for the end-to-end test!

Go to the **AWS IoT Console** and find the **MQTT test client**:

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FaT8gUcDTpD2Ppiewfjoq%2FDatacake_027.jpeg?alt=media&#x26;token=51c340c5-810b-4190-be06-9a3118873fac" alt=""><figcaption></figcaption></figure>

We will use the MQTT test client to publish MQTT Messages simulating a device connected to the AWS IoT Core. Switch to the “Publish to a topic” tab and define the Topic name as:

`aws-workspace/aws-integration/tempsensor-0001`

And the Message payload as:

```json
{
  "temp": 21.0
}
```

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2F1bvirFVaGD60pjKxgiII%2FDatacake_028.jpeg?alt=media&#x26;token=263e157b-5652-4579-aa45-e7c5b3486bd8" alt=""><figcaption></figcaption></figure>

Once you are ready, hit “Publish”! It looks as if nothing has happened… Let’s switch to the Datacake platform and go to the **Debug** tab:

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FPkoIao7W5gsjnApXlaGP%2FDatacake_029.jpeg?alt=media&#x26;token=159b127b-7edf-411c-9b1c-3d417db886f1" alt=""><figcaption></figcaption></figure>

There is the MQTT Message!

### Summary <a href="#r7wdmxndk70n" id="r7wdmxndk70n"></a>

<figure><img src="https://2221155863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LNA8_b8wmabHgJ0QXoJ%2Fuploads%2FmH04o85MnJagOdcIaXZ8%2Fdatacake_v01-solution_01.png?alt=media&#x26;token=45eae87f-c163-4b89-b162-11853ae70b29" alt=""><figcaption></figcaption></figure>

Let’s summarize what we accomplished:

1. **AWS IoT Infrastructure Setup for Datacake Integration:**
   * Configuration of AWS IoT resources, including an IoT Core, X.509 Certificates, and IoT Policies.
   * Establishing secure MQTT communication between the AWS IoT platform and Datacake.
2. **Datacake Setup:**
   * Creation of a Datacake product describing our use case, expected properties, and data format.
   * Configuring External MQTT Server to facilitate communication with AWS Cloud.
3. **End-to-End Test using a Simulated Device:**
   * Connect the simulated device to AWS IoT using the MQTT protocol.
   * Publishing sensor data from the simulated device to AWS IoT.
   * Verifying successful data transfer between AWS IoT and Datacake.

By completing these steps, we have successfully established a secure and reliable data integration pipeline between AWS IoT and Datacake. This integration enables the seamless flow of device data from AWS IoT to Datacake's platform, allowing for real-time data analysis, visualization, and actionable insights.

Congratulations! Your dedication and hard work have paid off. You are now prepared to delve deeper into the capabilities of the Datacake platform.
