ESP32 with AWS IoT -2-

์น˜์‚ผ์ดยท2021๋…„ 11์›” 3์ผ
0

Workshop:ESP32 with AWS IoT

๋ชฉ๋ก ๋ณด๊ธฐ
2/4
post-thumbnail

๐Ÿ‘จโ€๐Ÿ’ป Connecting AWS with ESP32: Tutorial

์ด๋ฒˆ ๊ธ€์€ ๐Ÿ”— Workshop:ESP32 with AWS IoT ์˜ ๋‚ด์šฉ์„ ์ง์ ‘ ์‹ค์Šตํ•œ ๋’ค ์žฌ ํฌ์ŠคํŒ… ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

1. AWS IoT ์—์„œ ์‚ฌ๋ฌผ(Thing) ๋งŒ๋“ค๊ธฐ

ํ‰์†Œ์—๋Š” Board๋‹ˆ ํ˜น์€ IoT ๊ธฐ๊ธฐ ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋ง‰์ƒ ์‚ฌ๋ฌผ ์ด๋ผ๋Š” ๋‹จ์–ด๋กœ ๊ธ€์„ ์ ์ด๋‹ˆ ์ข€ ์–ด์ƒ‰ํ•œ ๋Š๋‚Œ์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ IoT์˜ ์šฐ๋ฆฌ๋‚˜๋ผ ์ˆœํ™”์–ด๋Š” ์‚ฌ๋ฌผ ์ธํ„ฐ๋„ท์ด๊ณ  AWS IoT ์—์„œ๋„ ์‚ฌ๋ฌผ๋กœ ํ‘œ๊ธฐํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์‚ฌ๋ฌผ ์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ๋œป์„ ์•Œ์•„ ๋ณด์ž.

์‚ฌ๋ฌผ(ไบ‹็‰ฉ)

ใ€Œ๋ช…์‚ฌใ€

ใ€Œ1ใ€ ์ผ๊ณผ ๋ฌผ๊ฑด์„ ์•„์šธ๋Ÿฌ ์ด๋ฅด๋Š” ๋ง.

ใ€Œ2ใ€ ๋ฌผ์งˆ์„ธ๊ณ„์— ์žˆ๋Š” ๋ชจ๋“  ๊ตฌ์ฒด์ ์ด๋ฉฐ ๊ฐœ๋ณ„์ ์ธ ์กด์žฌ๋ฅผ ํ†ตํ‹€์–ด ์ด๋ฅด๋Š” ๋ง.

์ถœ์ฒ˜: ๊ตญ๋ฆฝ๊ตญ์–ด์› ํ‘œ์ค€๊ตญ์–ด๋Œ€์‚ฌ์ „

AWS IoT์—์„œ ์‚ฌ๋ฌผ๊ณผ ์—ฐ๊ฒฐ์„ ์‹œํ‚ค๋ ค๋ฉด ๋จผ์ € ์‚ฌ๋ฌผ์„ ์ƒ์„ฑํ•œ ๋’ค, ๋“ฑ๋กํ•ด์•ผํ•œ๋‹ค. ์šฐ์„  AWS์— ๋กœ๊ทธ์ธ ํ•œ ๋’ค AWS ๊ด€๋ฆฌ ์ฝ˜์†”์— ๋“ค์–ด๊ฐ€ ๋ชจ๋“  ์„œ๋น„์Šค โžœ ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท โžœ IoT Core ๋ฅผ ํด๋ฆญํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ์ขŒ์ธก์—์„œ ๊ด€๋ฆฌ โžœ ์‚ฌ๋ฌผ ์„ ํด๋ฆญํ•˜๋ฉด ์‚ฌ๋ฌผ ์ƒ์„ฑ ์ด๋ผ๋Š” ๋ฒ„ํŠผ์ด ์žˆ๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

01

์‚ฌ๋ฌผ ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹จ์ผ ์‚ฌ๋ฌผ ์ƒ์„ฑ ๊ณผ ๋งŽ์€ ์‚ฌ๋ฌผ ์ƒ์„ฑ ๋‘ ๊ฐˆ๋ž˜๋กœ ๋‚˜๋‰˜์–ด์ง€๋Š”๋ฐ, ์ด๋•Œ ๋‹จ์ผ ์‚ฌ๋ฌผ ์ƒ์„ฑ์„ ํด๋ฆญํ•œ๋‹ค. ์‚ฌ๋ฌผ์˜ ์†์„ฑ์— ์‚ฌ๋ฌผ์ด๋ฆ„์— ์›ํ•˜๋Š” ์‚ฌ๋ฌผ์ด๋ฆ„์„ ์ ์–ด๋‘์ž ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ESP32 ๋กœ ์ด๋ฆ„์„ ์ง“๊ณ  ์ถ”๊ฐ€ ๊ตฌ์„ฑ์€ Default ์ƒํƒœ๋กœ ๋‘” ๋’ค ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค.

02

๋‹ค์Œ์œผ๋กœ ๋””๋ฐ”์ด์Šค ์ธ์ฆ์„œ ๊ตฌ์„ฑ์„ ํ•œ๋‹ค. ์ž์‹ ์€ ์ž์‹ ๋งŒ์˜ ์ธ์ฆ์„œ์™€ ์ธ์ฆ๊ธฐ๊ด€์„ ์‚ฌ์šฉํ•˜๊ณ ์‹ถ๋‹ค๋ฉด, ๊ถŒ์žฅ์‚ฌํ•ญ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์„ ํƒ์„ ๋ˆ„๋ฅด๋ฉด ๋˜์ง€๋งŒ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ƒˆ ์ธ์ฆ์„œ ์ž๋™ ์ƒ์„ฑ(๊ถŒ์žฅ) ์„ ์„ ํƒํ•œ ๋’ค ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค. ์ด์ œ ๋‚ด ์‚ฌ๋ฌผ์˜ ์ธ์ฆ์„œ์™€ ์—ฐ๊ฒฐ๋  ์ •์ฑ…์„ ์„ค์ • ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ •์ฑ…์€ ์ฐจํ›„์— ์„ค์ •ํ•˜๋‹ˆ ์šฐ์„  ์‚ฌ๋ฌผ ์ƒ์„ฑ์„ ๋ˆ„๋ฅธ๋‹ค.

๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒ์—…์ฐฝ์ด ๋œจ๋Š”๋ฐ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์น ํ•ด์ง„๊ฑด ์ง€๊ธˆ ๋ฐ”๋กœ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„์•ผํ•˜๋Š” ๋ถ€๋ถ„์ด๊ณ  ํŒŒ๋ž€์ƒ‰์€ ๋‚˜์ค‘์— ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‹ค.

03

์ด์ œ ๋‹ค์‹œ ๊ด€๋ฆฌ โžœ ์‚ฌ๋ฌผ ํƒญ์„ ๋“ค์–ด๊ฐ€๋ณด๋ฉด ESP32 ์‚ฌ๋ฌผ์„ ์ƒ์„ฑํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. โœŒ๏ธ

2. AWS IoT ์˜ ์ •์ฑ…

AWS IoT์ธ์ฆ์„œ๋ฅผ ๋งŒ๋“ค๋ฉด ์ด ์ธ์ฆ์„œ์™€ ์ •์ฑ…์„ ์—ฐ๊ฒฐ๋˜์•ผ ์‹ค์ œ ์‚ฌ๋ฌผ์ด AWS IoT์™€ ์—ฐ๊ฒฐ์ด ๋œ๋‹ค. AWS IoT Core ์ •์ฑ…์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์˜์—ญ์— ๋Œ€ํ•œ ์—‘์„ธ์Šค๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ง์ด ์–ด๋ ต์ง€๋งŒ ์‚ฌ์‹ค์ƒ JSON ํฌ๋งท์˜ ๋ฌธ์„œ์ด๋ฉฐ ์ •์ฑ…์— ๋”ฐ๋ผ MQTT ๋ธŒ๋กœ์ปค์™€์˜ Connection, Subscribe, Publish ๋“ฑ์„ ์ œ์–ดํ•œ๋‹ค. ์ •์ฑ…์€ ๋‹ค์Œ ์„ธ ํ•ญ๋ชฉ์„ ํฌํ•จํ•œ๋‹ค.

  • Effect - ์ž‘์—…์ด ํ—ˆ์šฉ๋˜๋Š”์ง€ ๋˜๋Š” ๊ฑฐ๋ถ€๋˜๋Š”์ง€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • Action - ์ •์ฑ…์ด ํ—ˆ์šฉํ•˜๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€ํ•˜๋Š” ์ž‘์—…์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • Resource - ์ž‘์—…์ด ํ—ˆ์šฉ๋˜๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€๋˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ฌด์Šจ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋„ ๋‹ค์Œ ์Šคํ…์„ ๋ฐŸ๋‹ค๋ณด๋ฉด ์–ด๋Š์ •๋„ ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๐Ÿฆถ ์ผ๋‹จ ๋ธŒ๋ผ์šฐ์ € ์ขŒ์ธก์—์„œ ๋‹ค์‹œ ๋ณด์•ˆ โžœ ์ •์ฑ… ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์ •์ฑ… ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์ž.

04

์ด๋ฆ„์€ ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋„ฃ์–ด๋„ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ESP32 _Policy ๋กœ ์ง€์—ˆ๋‹ค. ์ž‘์—…๋ž€์— iot:Connect๋ฅผ ์ž…๋ ฅ ํ•˜๋ฉด, ๋ฆฌ์†Œ์Šค ARN๋ž€์— ์ž๋™์™„์„ฑ์œผ๋กœ ์ฑ„์›Œ ๋„ฃ์–ด์ง„๋‹ค. ๋‹ค์Œ ๋…ธ๋ž—๊ฒŒ ๊ฐ•์กฐ๋œ ๊ณณ์ธ replaceWithAClientId ๋ผ๊ณ  ์ ํžŒ ๋ถ€๋ถ„์€ ์‚ฌ๋ฌผ์˜ ์ด๋ฆ„์„ ์ ์–ด ๋„ฃ์–ด์•ผํ•œ๋‹ค. ์ด ํฌ์ŠคํŒ…์„ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋ฉด ESP32 ๋ผ๊ณ  ์ ์œผ๋ฉด ๋œ๋‹ค. ๊ทธ ๋’ค ํ—ˆ์šฉ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ํด๋ฆญํ•˜์ž. ๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ ์ƒ์„ฑ์„ ํ•˜๋ฉด AWS์˜ MQTT Broker์—๋Š” ์—ฐ๊ฒฐ์ด ๋˜๊ฒ ์ง€๋งŒ... Sub์ด๋‚˜ Pub์„ ํ•  ์ˆœ์—†๋‹ค.

๋”ฐ๋ผ์„œ ์„ค๋ช…๋ฌธ ์ถ”๊ฐ€๋ฅผ ๋ˆŒ๋Ÿฌ ๋‹ค์Œ ์ž‘์—…๋“ค๋„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž. ์ด๋ฒˆ์—๋„ ๊ฐ•์กฐ๋œ ๋ถ€๋ถ„์€ Sub ๊ณผ Pub ํ† ํ”ฝ์ด๋ฉฐ, ์ด๋ฅผ ์•„๋ž˜ ์˜ˆ์‹œ๊ฐ€ ๋ง˜์— ๋“ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค๋ฅธ ํ† ํ”ฝ์„ ์ด์šฉํ•ด๋„ ๋œ๋‹ค.

05

์ด์ œ ์ •์ฑ…๊นŒ์ง€ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ์ด์ „์— ๋งŒ๋“ค์—ˆ๋˜ ์ธ์ฆ์„œ์™€ ์ด ์ •์ฑ…์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋ฉด ์‚ฌ๋ฌผ์—ฐ๊ฒฐ์„ ์œ„ํ•œ ์…‹์—…์€ ๋‹ค ๋์ด๋‚œ๋‹ค. ๊ด€๋ฆฌ โžœ ์‚ฌ๋ฌผ โžœ YourThingName โžœ ์ธ์ฆ์„œ ์—์„œ ์ธ์ฆ์„œ ID๋ฅผ ํด๋ฆญํ•œ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ ์ฐฝ์ด ๋„๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์ž‘์—… ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.

06

๊ทธ๋’ค, ๋ฐฉ๊ธˆ ๋งŒ๋“  ์ •์ฑ…์—ฐ๊ฒฐ์„ ์—ฐ๊ฒฐํ•˜๋ฉด ๋๋‚œ๋‹ค.

3. ESP32 Code Uploading

์ด์ œ ESP32 ์™€ AWS IoT๋ฅผ ์—ฐ๊ฒฐํ•  ์ฝ”๋“œ๋ฅผ ์—…๋กœ๋“œ ํ•ด๋ณด์ž. ์šฐ์„  ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถ€ํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„์•ผ ํ•œ๋‹ค. Arduino IDE์— ํˆด โžœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ด€๋ฆฌ ์— ๋“ค์–ด๊ฐ€์„œ ArduinoJson ๊ณผ lwmqtt ๋ฅผ ๊ฒ€์ƒ‰ํ•ด ๋‹ค์šด๋ฐ›์•„ ์ฃผ์ž. Ardunio IDE์—๋Š” ๋””ํดํŠธ๋กœ wifi ๋ชจ๋“ˆ์ด ๊น”๋ ค์ ธ ์žˆ์ง€๋งŒ, ๋งŒ์•ฝ ์—†๋‹ค๋ฉด ์ด ๋ชจ๋“ˆ๋„ ๋‹ค์šด๋ฐ›์•„์•ผํ•œ๋‹ค. 2021๋…„ 11์›” 4์ผ ๋ชจ๋“ˆ๋ณ„ ์ตœ์‹  ๋ฒ„์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ArduinoJson: 6.18.5
  • lwmqtt: 2.5.0
  • WiFi: 1.2.7

๋‹ค์Œ์€ ๋ฉ”์ธ ESP_AWS_IoT_Tutorial.ino ํŒŒ์ผ์ด๋‹ค. ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋’ค, ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด ๋ถ™์—ฌ๋„ฃ์ž.

/*
  Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  Permission is hereby granted, free of charge, to any person obtaining a copy of this
  software and associated documentation files (the "Software"), to deal in the Software
  without restriction, including without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so.
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"

// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC   "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);

void connectAWS()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.begin(AWS_IOT_ENDPOINT, 8883, net);

  // Create a message handler
  client.onMessage(messageHandler);

  Serial.print("Connecting to AWS IOT");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    return;
  }

  // Subscribe to a topic
  client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

  Serial.println("AWS IoT Connected!");
}

void publishMessage()
{
  StaticJsonDocument<200> doc;
  doc["time"] = millis();
  doc["sensor_a0"] = analogRead(0);
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer); // print to client

  client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

//  StaticJsonDocument<200> doc;
//  deserializeJson(doc, payload);
//  const char* message = doc["message"];
}

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

void loop() {
  publishMessage();
  client.loop();
  delay(1000);
}

define ๋งคํฌ๋กœ๋ฅผ ์ด์šฉํ•œ AWS_IOT_PUBLISH_TOPIC ๊ณผ AWS_IOT_SUBSCRIBE_TOPIC์€ 2. AWS IoT ์˜ ์ •์ฑ… ํŽธ์—์„œ ๋งŒ๋“  Publish ์™€ Subscribe ์ •์ฑ…์˜ ํ† ํ”ฝ๊ณผ ์ผ์น˜ํ•ด์•ผํ•œ๋‹ค.

#define AWS_IOT_PUBLISH_TOPIC   "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"

๋‹ค์Œ์€ secret.h ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์—๋Š” AWS IoT์™€ ์—ฐ๊ฒฐ๋  ์ธ์ฆ์ •๋ณด ๋ฐ ๊ฐœ์ธ WiFi ์ •๋ณด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

/*
  Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  Permission is hereby granted, free of charge, to any person obtaining a copy of this
  software and associated documentation files (the "Software"), to deal in the Software
  without restriction, including without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so.
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <pgmspace.h>

#define SECRET
#define THINGNAME ""

const char WIFI_SSID[] = "";
const char WIFI_PASSWORD[] = "";
const char AWS_IOT_ENDPOINT[] = "xxxxx.amazonaws.com";

// 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";

์šฐ๋ฆฌ๊ฐ€ ์ˆ˜์ •ํ•ด์•ผํ•  ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • const char WIFI_SSID[] = ""; : WiFi ๋„คํŠธ์›Œํฌ SSID
  • const char WIFI_PASSWORD[] = "";: WiFi ๋„คํŠธ์›Œํฌ ๋น„๋ฐ€๋ฒˆํ˜ธ
  • const char AWS_IOT_ENDPOINT[] = "xxxxx.amazonaws.com"; = AWS ENDPOINT ์ฃผ์†Œ
  • static const char AWS_CERT_CRT[] PROGMEM = : ์ธ์ฆ์„œ
  • static const char AWS_CERT_PRIVATE[] PROGMEM =: ๊ฐœ์ธํ‚ค

๊ทธ์™ธ Amazon Root CA1์€ ๋‹ค์Œ ๋งํฌ๋ฅผ ๊ธ์–ด ๋ณต์‚ฌํ•˜๋ฉด ๋œ๋‹ค.

Amazon Root CA 1

WiFi์˜ SSID ๋ฐ PASSWORD๋Š” ์ค€๋น„๋œ WiFi์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด๋œ๋‹ค. AWS_IOT_ENDPOINT ๋Š” AWS IoT ์ขŒ์ธก ์ตœํ•˜๋‹จ ์„ค์ • ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ„๋’ค ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋ณต์‚ฌํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

07

AWS_CERT_CRT ๋Š” ๋ฏธ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•˜๋˜ ***-certificate.pem.crt ํŒŒ์ผ์„ ํŽธ์ง‘๊ธฐ๋กœ ์—ด์–ด์„œ ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•˜๊ณ , AWS_CERT_PRIVATE ๋Š” ***-private.pem.key ๋ฅผ ์—ด์–ด์„œ ๋ณต์‚ฌํ•˜๋ฉด ๋œ๋‹ค. ESP32 ๊ฐ€ AWS IoT์™€ ์—ฐ๊ฒฐ๋˜๋ฉด, AWS IoT Connected! ๋ผ๋Š” ๋‚ด์šฉ์ด ์‹œ๋ฆฌ์–ผ ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

08

4. MQTT Test Client

ESP32 ์—ฐ๊ฒฐ์ด ๋๋‚ฌ์œผ๋ฉด ์ด์ œ ๋‹ค์‹œ AWS IoT ํ™ˆํŽ˜์ด์ง€๋กœ์™€ ์ขŒ์ธก ํ•˜๋‹จ ํ…Œ์ŠคํŠธ โžœ MQTT ํ…Œ์ŠคํŠธ ํด๋ผ์ด์–ธํŠธ ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ„ ๋’ค, ์ฃผ์ œ ๊ตฌ๋…๋ž€์— esp32/pub ์„ ์ž…๋ ฅํ•˜๊ณ  ๊ตฌ๋… ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์ž.

09

์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ๋ถ€ํ„ฐ ๋ฐœ์ƒ๋˜๋Š” ๋กœ๋ฅด์ด๋‹ค. analog0 pin์— ์–ด๋–ค ์„ผ์„œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ์„ผ์„œ์˜ ๊ฐ’์ด AWS IoT๋กœ ๋ณด๋‚ด์ง€๊ฒŒ ๋œ๋‹ค.

void publishMessage()
{
  StaticJsonDocument<200> doc;
  doc["time"] = millis();
  doc["sensor_a0"] = analogRead(0);
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer); // print to client

  client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}

์‚ฌ๋ฌผ์—์„œ Publish๊ฐ€ ์ž˜ ๋˜๋Š”๊ฒƒ์„ ํ™•์ธ ํ–ˆ์œผ๋‹ˆ ์ด์ œ Subscribe๋„ ์ž˜ ๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ…Œ์ŠคํŠธ ํด๋ผ์ด์–ธํŠธ์—์„œ ์ฃผ์ œ ๊ฒŒ์‹œ ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์ฃผ์ œ ์ด๋ฆ„์„ esp32/sub ์„ ์ ๊ณ  ๋””ํดํŠธ ๋ฉ”์‹œ์ง€ ํŽ˜์ด๋กœ๋“œ๋กœ ๊ฒŒ์‹œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋ฆฌ์–ผ ๋ชจ๋‹ˆํ„ฐ์— ์ž˜ ์ถœ๋ ฅ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

10

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ˆ˜์‹ ๋œ ๋ฉ”์‹œ์ง€๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ๋ณด์—ฌ์ค€๋‹ค. โœŒ๏ธโœŒ๏ธ

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

//  StaticJsonDocument<200> doc;
//  deserializeJson(doc, payload);
//  const char* message = doc["message"];
}

client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

๐Ÿ”— Connecting AWS with ESP32 Tutorial

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 2์›” 6์ผ

์‹œ์Šคํ…œ์ด ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ์ฒญ์ฑ…์— ๋ฆฌ์†Œ์Šค ARN ์ž…๋ ฅ๋ถ€๋ถ„์ด ์ž๋™์œผ๋กœ ์ž…๋ ฅ์ด ์•ˆ๋˜๋„ค์š”..... ์–ด๋–ป๊ฒŒ ์ž…๋ ฅํ•ด์•ผํ•˜๋‚˜์š” ใ…œใ…œ

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ