How to connect "The Things Network(TTN V3)" and "AWS IoT Core".
Tags: aws, ttn, iot, core
Published: 2022-06-03 00:34:02

This article will show you how to connect "The Things Network V3" and "AWS IoT Core".

# 1. TTN Settings
- Select the application to connect from TTN, and select "AWS IoT" from "Integrations".
- Then click on the "Deployment Guide".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irrbt485er74vm8v3weg.png)

- Scroll down. 
- For "Select your AWS region", select your region.
- Then click "Deploy for The Things Network" under "Community".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2n082np4rmbse20gggx.png)

# 2. AWS Settings
- Enter the TTN cluster address or TTN application ID.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqicmcprzyyj88t8p2fu.png)

- See below for cluster address and application ID

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkd9jfm9m6avw4v3yiyj.png)

## Creating an API key
- Select "API keys" from the TTN application and click "Add API key".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzwdbe6bosjibo68in1i.png)

- Make the settings as shown in the image.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qo2mwfhdpmugpz5az4j.PNG)

- Copy the created API key and paste it into the aws settings screen.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n7m7g20ql6z0ii5vyoc.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fj47zjtkfmlkah349vdl.PNG)

- When the stack is created by aws, it will be as follows.
- Creating the stack will take a few minutes.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9ikj52rve65irgrwhfx.PNG)


# 3. Set up with AWS IoT Core

- Log in to the aws IoT Core.
- And if you check "Things", you can see that a new device has been added.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw0o113ylb6zy1j8icao.png)

- Let's check if the data can be obtained.
- Enter "lorawan / #" in "MQTT test client".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pirfvd23ww8x27s7yag7.png)

- After waiting for a while, the data will be earned.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv2krv82ybkaw0zqn4kl.PNG)

- However, this alone is not enough to extract only the variables you want.
- Let's add a rule for the data format.
- I use a distance sensor, so I try to get only the distance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyby8l5hlipbbjb8c83z.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30zqpww1g3wh7qu53d5l.png)

- Edit the SQL statement.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0os673htlk5a7y3toqet.png)

- These are the SQL statements I used.

```
SELECT end_device_ids.device_id AS device, received_at AS r_date, uplink_message.decoded_payload.distance AS distance FROM 'lorawan/A840414/uplink' WHERE uplink_message.decoded_payload.distance > 20
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivmrvup37tcptdvj7drp.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5ty9sxcw2yuaa5ta9rc.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7h55mzff3c9ih4rgi01b.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ryww61vass89oi24l42.png)