ESP32 with AWS IoT -3-

cheesam31ยท2021๋…„ 11์›” 25์ผ
0

Workshop:ESP32 with AWS IoT

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

๐Ÿ‘จโ€๐Ÿ’ป Connecting AWS with ESP32: Create an AWS IoT Button

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

1. ์‹œ์Šคํ…œ ๊ฐœ์š”

์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ESP32์— Button์„ ์žฅ์ฐฉํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด MQTT ๋ฉ”์‹œ์ง€๋ฅผ AWS IoT์— ์ „์†กํ•˜๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด AWS Lambda๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. ์‹œ์Šคํ…œ ๊ตฌ์„ฑ์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

์‹ค์ œ ์›๋ณธ Workshop์—์„œ๋Š” TTP223 ๋ผ๋Š” Touch ์„ผ์„œ๋ฅผ ์ด์šฉํ•ด์„œ ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜์ง€๋งŒ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” 4๋ฐœ ์Šค์œ„์น˜๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. ์žฌ๋ฃŒ๋Š” ์ด๊ณณ ์—์„œ ๊ตฌ๋งคํ–ˆ์œผ๋ฉฐ, ์•ฝ 3๋งŒ์› ๋ˆ์œผ๋กœ Arduino ๊ธฐ์ดˆ ํ‚คํŠธ๋ฅผ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ๋‹ค.

Arduino ๊ธฐ์ดˆ ํ‚คํŠธ์—์„œ ์‚ฌ์šฉํ•œ ์žฌ๋ฃŒ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • 4๋ฐœ ์Šค์œ„์น˜
  • 220ฮฉ or 10kฮฉ ์ €ํ•ญ(option)

220(10k)ฮฉ ์ €ํ•ญ์€ pull down์ €ํ•ญ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋„ฃ์€ ์ €ํ•ญ์ด๋ฉฐ, option์œผ๋กœ ํ‘œ์‹œํ•œ ์ด์œ ๋Š” ESP32๋Š” ๋‚ด๋ถ€ pull up ํ˜น์€ pull down ์ €ํ•ญ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. pinMode(pin_number, INPUT_PULLDOWN) ํ˜น์€ pinMode(pin_number, INPUT_PULLUP) ์„ ์ด์šฉํ•ด ํ•€ ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ๋ชจ๋“  GPIO port๊ฐ€ pull up ๋ฐ pull down ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ๋‹ฌ๋ ค์žˆ๋Š”๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ESP32 Datasheet๋ฅผ ์‚ดํŽด๋ณด๋ฉด

GPIO 34~39๋ฒˆ์€ Input ์ „์šฉ์ด๋ฉฐ pull up ๋ฐ pull down์ด ์—†๋‹ค๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.

2. ESP32 Setting

2.1 button ํ…Œ์ŠคํŠธ

pin_number๋Š” Input(ํ˜น์€ INPUT_PULLUP)์œผ๋กœ ์„ค์ • ํ•œ ๋’ค ์œ„์™€ ๊ฐ™์ด pull up ์ €ํ•ญ์„ ๋‹ฌ์•„์ฃผ๋ฉด Floating ์ƒํƒœ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ™•์ธํ•ด ๋ณด์ž.

int buttonPin = 4;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
}

void loop() {
  Serial.println(digitalRead(buttonPin));
}

์œ„ ์ฝ”๋“œ๋ฅผ ์—…๋กœ๋“œ ํ•˜๋ฉด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ ๋งˆ๋‹ค ์‹œ๋ฆฌ์–ผ ๋ชจ๋‹ˆํ„ฐ์— 1์ด ์ถœ๋ ฅ๋จ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ๐Ÿ”— Tutorial์—์„œ ์ž‘์„ฑํ–ˆ๋˜ 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);
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPin = 4;
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() {
  // Setting value of buttonState what the D4 touch sensor is reporting
  buttonState = touchRead(4);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == 1) {
      // if the current state is 1 then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    // Delay a little bit to avoid bouncing
    delay(1258);
  }
  // save the current state as the last st/*
  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);
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPin = 4;

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(115200);
  connectAWS();
}

void loop() {
  // Setting value of buttonState what the D4 touch sensor is reporting
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == 1) {
      // if the current state is 1 then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    // Delay a little bit to avoid bouncing
    delay(1258);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}ate, for next time through the loop
  lastButtonState = buttonState;
}

์œ„ ์ฝ”๋“œ๋ฅผ ์—…๋กœ๋“œ ํ•˜๋ฉด AWS IoT์— ์ ‘์†์€ ํ•˜์ง€๋งŒ publish๋ฅผ ํ•˜์ง€๋Š” ์•Š๊ณ  ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ ๋งˆ๋‹ค ๋ช‡๋ฒˆ ๋ˆŒ๋ ธ๋Š”์ง€๋ฅผ ์นด์šดํŒ… ํ•ด์ค€๋‹ค.

2.2 ๋ฒ„ํŠผ๊ณผ AWS IoT์˜ ์—ฐ๊ฒฐ

์ด์ œ ๋ฐฉ๊ธˆ ์ „ ์—…๋กœ๋“œํ•œ ์ฝ”๋“œ ์ผ๋ถ€๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค. ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ ๋งˆ๋‹ค Publish๋ฅผ ํ•˜๊ณ  ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์€ json ํ˜•์‹์œผ๋กœ {'Action': 'pressed'} ๋ณด๋‚ด๊ฒŒ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•ด ๋ณด์ž.

void publishMessage()
{
  char payload[512];
  sprintf(payload,"{\"Action\":\"pressed\"}");
  client.publish(AWS_IOT_PUBLISH_TOPIC, payload);
  Serial.print("Published Message:");
  Serial.println(payload);
}

๋ฉ”์‹œ์ง€๋ฅผ publishํ•˜๋Š” ํ•จ์ˆ˜์™€

void loop() {  
  buttonState = touchRead(4);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == 1) {
      // if the current state is 1 then the button went from off to on:=
      buttonPushCounter++;
      Serial.println("Button Pressed");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
      // Publish Message to AWS IoT
      publishMessage();
      Serial.println("==================");
    }
    // Delay a little bit to avoid bouncing
    delay(1258);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}

loop๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค. ๐Ÿ”— Tutorial์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ MQTT ํ…Œ์ŠคํŠธ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ–ˆ๋˜ json ํ˜•์‹์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๋ฉด ์„ฑ๊ณต์ด๋‹ค. ๐Ÿ‘๐Ÿ‘

3. ASW Lambda

3.1 AWS Lambda ๋ž€?

AWS Lambda๋Š” Amazon Web Services์˜ ์ผ๋ถ€๋กœ Amazon์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋ฒคํŠธ ์ค‘์‹ฌ์˜ ์„œ๋ฒ„๋ฆฌ์Šค ์ปดํ“จํŒ… ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ•ด๋‹น ์ฝ”๋“œ์— ํ•„์š”ํ•œ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๋ฅผ ์ž๋™์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ปดํ“จํŒ… ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.

์ถœ์ฒ˜: ์œ„ํ‚ค๋ฐฑ๊ณผ

AWS Lambda๋Š” ์„œ๋ฒ„๋ฅผ ๋”ฐ๋กœ ๊ตฌ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” AWS์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปดํ“จํ„ฐ ์„œ๋น„์Šค์ด๋‹ค. Lambda๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฑฐ์˜ ๋ชจ๋“  ์œ ํ˜•์˜ ์• ํ•„๋ฆฌ์ผ€์ด์…˜ ํ˜น์€ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค์—๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด Database์—์„œ Query๋ฅผ ์ „์†กํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜์žˆ๋‹ค.

๋ชจ๋“  ์–ธ์–ด๋ฅผ ์ง€์›ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋Œ€๋žต์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ์–ธ์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Python(2.7์€ ์ง€์› ์ข…๋ฃŒ ์˜ˆ์ •)
  • Node.js(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)
  • Ruby
  • Java
  • Go
  • .NET Core(C#)

๊ฐ ์–ธ์–ด๋“ค์ด ๋Œ์•„๊ฐ€๋Š” ์šด์˜์ฒด์ œ ํ™˜๊ฒฝ์€ Amazon Linux ํ˜น์€ Amazon Linux2 ์ง€์›ํ•˜๋Š” ์•„ํ‚คํ…์ณ๋Š” x86_64, arm64 ์ด๋ฉฐ, ๋” ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ๐Ÿ”— ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

3.2 AWS Lambda ํ•จ์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ

AWS Lambda ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•ด AWS ๊ด€๋ฆฌ ์ฝ˜์†”์—์„œ Lambda ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ€ ๋ณด์ž. Lambda ํƒญ์€ ๋ชจ๋“  ์„œ๋น„์Šค> ์ปดํ“จํŒ… > Lambda ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

์ขŒ์ธก์— AWS Lambda์—์„œ ํ•จ์ˆ˜๋ฅผ ํด๋ฆญํ•œ๋’ค ํ•จ์ˆ˜ ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด๋ณด์ž.

ํ•จ์ˆ˜ ์ด๋ฆ„์€ IoT_Button ์œผ๋กœ ์ง€์ •ํ•˜๊ณ  ๋Ÿฐํƒ€์ž„์€ Python 3.9๋กœ ์•„ํ‚คํ…์ณ๋Š” default์ธ x86_64๋กœ ์ง€์ •ํ•œ๋’ค, ํ•จ์ˆ˜ ์ƒ์„ฑ์„ ํด๋ฆญํ•ด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

Lambda์—์„œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋„๋ก ํ•˜์ž. โœ๏ธ

import json

def lambda_handler(event, context):
    print(json.dumps(event))
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

์ž‘์„ฑํ•œ ๋’ค Deploy ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋ฐฐํฌํ•œ๋’ค ์ด ํ•จ์ˆ˜๋ฅผ ๊นจ์šธ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.

3.3 IoT Rule Engine ์„ค์ •

AWS IoT Core์— ์ ‘์†ํ•˜๋ฉด ์ขŒ์ธก ํƒญ์— ๋™์ž‘ > ๊ทœ์น™ ์œผ๋กœ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ AWS IoT Core ๊ทœ์น™ ์—”์ง„์„ ์ƒ์„ฑํ•ด AWS Lambda๋ฅผ ํŠธ๋ฆฌ๊ฑฐ๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ๊ทœ์น™์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด ๊ทœ์น™์„ ์ƒ์„ฑํ•ด๋ณด๋„๋ก ํ•˜์ž.

์ด๋ฆ„์€ IoTButtonRule ๋กœ ์ง€์ •ํ•œ๋’ค ๊ทœ์น™ ์ฟผ๋ฆฌ ์„ค๋ช…๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ์ง‘์–ด ๋„ฃ๋Š”๋‹ค. ์ข€ ๋” ์ž์„ธํ•œ ๊ทœ์น™ ์ฟผ๋ฆฌ๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด AWS IoT SQL ์ฐธ์กฐ๋ฅผ ์ฐธ์กฐํ•˜์ž.

SELECT * FROM 'esp32/pub' WHERE Action='pressed'

์ด์ œ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ž‘์—… ์„ค์ • ๋ž€์—์„œ ์ž‘์—…์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ AWS Lambda๋ฅผ ์„ ํƒํ•ด ๋ณด์ž.

์ž‘์—…์„ ์ถ”๊ฐ€ํ•˜๋‹ค ๋ณด๋ฉด ํ•จ์ˆ˜์ด๋ฆ„์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  AWS Lambda์ธ IoT_Button์ด ์žˆ๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

4. ์‹œ์Šคํ…œ ํ…Œ์ŠคํŠธ

์ด์ œ ์ „์ฒด ์‹œ์Šคํ…œ์„ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ์œ„ํ•ด ๋‹ค์‹œ AWS Lambda์— ๋“ค์–ด๊ฐ€๋ณด์ž. ์ข€ ์ „ ๊ณผ ๋‹ค๋ฅด๊ฒŒ ํ•จ์ˆ˜ ๊ฐœ์š” ์˜ IoT_Button ์— AWS IoT ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ ์„ ํ™•์ธ ํ•  ์ˆ˜์žˆ๋‹ค.

์ด์ œ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•ด ๋ชจ๋‹ˆํ„ฐ๋ง ํƒญ์— ๋“ค์–ด๊ฐ€์„œ CloudWatch์—์„œ ๋กœ๊ทธ ๋ณด๊ธฐ ๋ฅผ ํด๋ฆญํ•ด AWS CloudWatch ์„œ๋น„์Šค์— ์ ‘์†ํ•˜์ž. ์ขŒ์ธก ํƒญ์—์„œ ๋กœ๊ทธ > ๋กœ๊ทธ ๊ทธ๋ฃน ์œผ๋กœ ๋“ค์–ด๊ฐ€ ๋กœ๊ทธ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•ด๋ณด์ž. ๋กœ๊ทธ ๊ทธ๋ฃน ์ด๋ฆ„์€ /aws/lambda/IoT_Button ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์„ค์ •์‚ฌํ•ญ์€ default๋กœ ์ƒ์„ฑํ•œ๋‹ค.

์ด์ œ ESP32์˜ Reset ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์šฐ๋ฆฌ๊ฐ€ ESP32์— ์žฅ์ฐฉํ–ˆ๋˜ ์Šค์œ„์น˜๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด์ž. ๋ˆ„๋ฅธ ๋’ค AWS CloudWatch์—์„œ /aws/lambda/IoT_Button๋กœ๊ทธ ๊ทธ๋ฃน์œผ๋กœ ๋“ค์–ด๊ฐ€ ๋กœ๊ทธ ์ŠคํŠธ๋ฆผ์„ ๋ณด์ž. ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ƒˆ๋กœ์šด ๋กœ๊ทธ ์ŠคํŠธ๋ฆผ์ด ์ƒ์„ฑ๋˜ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋กœ๊ทธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋“ค์–ด๊ฐ€๋ณด๋ฉด ๋ฌผ๋ฆฌ ์Šค์œ„์น˜๊ฐ€ ์ด 3๋ฒˆ ๋ˆŒ๋ฆฐ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”— Connecting AWS with ESP32 Create an AWS IoT Button

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