หน้าแรก ดูโปรเจกต์ทั้งหมด
Expert

โปรเจกต์ AWS IoT Arduino library สำหรับ ESP32

library นี้ใช้ AWS C-SDK เพื่อพัฒนา Arduino class ชื่อ AWSGreenGrassIoT เพื่อให้ง่ายต่อการเชื่อมต่อ sensors/actuator เข้ากับ AWS IoT อย่างปลอดภัย

โปรเจกต์ AWS IoT Arduino library สำหรับ ESP32

รายการอุปกรณ์และเครื่องมือ

1x Air quality sensor SGP30
-
1x SparkFun Atmospheric Sensor Breakout - BME280
-
1x SG90 Micro-servo motor
🛒 สั่งซื้อ

รายละเอียดและวิธีทำ

https://github.com/aws-samples/arduino-aws-greengrass-iot

ไลบรารีนี้ใช้ AWS C-SDK เพื่อสร้าง Arduino class ที่ชื่อว่า AWSGreenGrassIoT เพื่อให้ง่ายต่อการเชื่อมต่อ Sensor/Actuator เข้ากับ AWS IoT Core อย่างปลอดภัย ไม่ว่าจะเชื่อมต่อโดยตรงหรือผ่านอุปกรณ์ AWS Greengrass (เช่น Raspberry PI) โดยใช้ X509 certificates.

คลาส AWSGreenGrassIoT มี Method ต่างๆ ดังนี้:

Constructor: AWSGreenGrassIoT(const char * AwsIoTCoreurl, // AWS IoT core URL
const char * thingName, // AWS thing name
const char * iotCoreCA, // AWS IoT core certificate (defined in certificate.c)
const char * thingCA, // thing certificate (defined in certificates.c)
const char * thingKey); // thing private key (defined in certificate.c)
bool connectToGG(void); // connect the device to greengrass
bool connectToIoTCore(void); // connect the device directly to AWS IoT Core
bool publish(char * pubtopic, char * pubPayLoad); // publish a JSON record to "pubTopic"
bool subscribe(char * subTopic, pSubCallBackHandler_t pSubCallBackHandler); // subscribe to "subTopic" and define the callback function to handle the messages coming from the IoT broker

Library dependencies

 ไลบรารีนี้อ้างอิงตาม Amazon iot C-SDK เวอร์ชัน 3.0.1 ล่าสุด (ณ เดือนมกราคม 2020) https://github.com/aws/aws-iot-device-sdk-embedded-C/releases/tag/v3.0.1. เวอร์ชันในอนาคตของ SDK สามารถดูได้ที่ https://github.com/aws/aws-iot-device-sdk-embedded-C และอาจจำเป็นต้องทำการ Recompilation โค้ด AWSGreengrassIoT ใหม่ ไลบรารีนี้มีการใช้งานไลบรารีมาตรฐานของ Arduino บางตัว ได้แก่: WiFi, WifiClientSecure, HTTPClient.

ตัวอย่างการใช้งานจำเป็นต้องติดตั้งไลบรารีต่อไปนี้จาก Public Repositories:

  • NPTClient: เพื่อซิงโครไนซ์ Real Time Clock กับ nptd server และสร้าง Timestamps สำหรับการวัดค่าของ Sensor (ในตัวอย่างการ Publishing รุ่น gg_SGP30_publisher และ aws_SGP30_publisher)
  • ADAFruit_SGP30: เพื่อใช้งาน Air Impurity Sensor ในตัวอย่าง gg_SGP30_publisher และ aws_SGP30_publisher
  • ESP32Servo: เพื่อควบคุมการหมุนของ Servo Motor ในตัวอย่าง gg_subscriber และ aws_subscriber

Pre-requisites

  • จากเมนูหลักของ Arduino IDE -> Preferences
  • ติดตั้ง Arduino IDE เวอร์ชันล่าสุดสำหรับแพลตฟอร์มของคุณ (ภาพหน้าจอต่อไปนี้จะอ้างอิงจากเวอร์ชัน MAC OS): https://www.arduino.cc/en/main/software;
  • ติดตั้ง ESP32 board manager:
  • จากเมนูหลักของ Arduino IDE -> Tools -> Board Manager
  • พิมพ์ ESP32 ในช่องค้นหาตามที่ระบุด้านล่าง และติดตั้ง ESP32 board manager ตัวใหม่:
  • ในเมนู Tools -> Board ให้เลือกประเภท ESP32 module ของคุณ ในกรณีของผมคือ ESP32 Dev Module
  • ติดตั้ง Arduino Libraries “NPTClient”, “Adafruit_SGP30”, “ESP32Servo” เพื่อให้สามารถใช้งานตัวอย่างการ Publishing และ Subscribing จากเมนู File -> Examples -> AWSGreengrassIoT) รูปภาพด้านล่างแสดงวิธีเพิ่มไลบรารีเสริมสำหรับ Servo motor ทั่วไป:

Installation of AWSGreengrassIoT library

  • ดาวน์โหลดไลบรารีในรูปแบบ Zip file ตามที่ระบุในภาพ:
  • เพิ่ม Zip file เป็น Arduino library ใหม่ในเมนู Sketch -> Include Library -> Add .ZIP library:
  • ตรวจสอบว่ามองเห็นไลบรารีในเมนู Sketch -> Include Library ตามที่แสดงในภาพหน้าจอด้านล่าง:

Examples

ก่อนที่จะใช้ตัวอย่างจากเมนู Sketch -> Examples โปรดอย่าลืม:

  • สร้างและดาวน์โหลด thing's certificate, thing's private key และ AWS service certificate (หาก Endpoint ของคุณต่างจาก eu-central-1)
  • สร้างไฟล์ aws_certificates.c พร้อมกับ Certificates/Key ที่สร้างในขั้นตอนก่อนหน้า
  • ปรับแต่งโค้ดตัวอย่าง Arduino โดยกำหนดค่าพารามิเตอร์สำหรับ AWS IoT Core URL, "thing" ของคุณ และเครือข่าย WiFi ของคุณ:
char WIFI_SSID[]="SSID";
char WIFI_PASSWORD[]="PASSWORD";
char AWSIOTURL[]="xxxxxxxxxxxxxxx-ats.iot.region.amazonaws.com";
char THING[]= "your device name here";

ไลบรารี AWSGreenGrassIoT มาพร้อมกับ 5 ตัวอย่างการใช้งาน:

aws_servo_subscriber, gg_servo_subscriber

ในตัวอย่างเหล่านี้ Servo motor จะถูกเชื่อมต่อกับ Analog GPIO port0 บน ESP32 และจำลองการเปิด-ปิดหน้าต่างจากระยะไกลโดยการหมุนมอเตอร์ ±90 องศา ขึ้นอยู่กับ Topic "Window" ที่ Subscribe ไว้ โดยคำสั่ง "open" จะหมุนมอเตอร์ +90 องศา และคำสั่ง "close" จะหมุนมอเตอร์ในทิศทางตรงกันข้าม -90 องศา

แผนผังวงจร (Circuit diagram):

ความแตกต่างเพียงอย่างเดียวระหว่างสองตัวอย่างนี้คือ:

  • aws_servo_subscriber เชื่อมต่อกับ AWS IoT Core โดยตรงโดยใช้ Member function "connectToIoTCore"
if(greengrass->connectToIoTCore() == true)
{
Serial.println("Connected to AWS IoT core");
delay(2000);
if( true == greengrass->subscribe(TOPIC_NAME,subscribeCallback)) {
Serial.println("Subscribe to Window/# topic successful ");
}
else {
Serial.println("Subscribe to Window/# Failed, Check the Thing Name and Certificates");
while(1);
}
}
else
{
Serial.println("Connection to AWS IoT core failed");
while(1);
}
  • gg_servo_subscriber เชื่อมต่อผ่านอุปกรณ์ Greengrass (อาจเป็น Raspberry Pi ที่เชื่อมต่อกับเครือข่าย WiFi เดียวกัน หรือ EC2 instance บน AWS Cloud) โดยใช้ Member function "connectToGG"
if(greengrass->connectToGG() == true)
{
Serial.println("Connected to AWS GreenGrass");
delay(2000);
if( true == greengrass->subscribe(TOPIC_NAME,subscribeCallback)) {
Serial.println("Subscribe to Window/# topic successful ");
}
else {
Serial.println("Subscribe to Window/# Failed, Check the Thing Name and Certificates");
while(1);
}
}
else
{
Serial.println("Connection to Greengrass failed, check if Greengrass is on and connected to the WiFi");
while(1);
}

Callback function ที่จัดการการ Subscribe Topic นั้นเหมือนกันสำหรับทั้งสองกรณี:

static void subscribeCallback (char *topicName, int payloadLen, char *payLoad)
{
//check if the topic is Window/close or Window/open
rcvdPayload = String(payLoad);
cmdReceived = CMD_UNKNOWN;
String topic = String(topicName);
if ( topic.startsWith(topicClose+ "{")) {
cmdReceived = CMD_CLOSE;
rcvdTopic = topicClose;
}
else if (topic.startsWith(topicOpen + "{")) {
cmdReceived = CMD_OPEN;
rcvdTopic = topicOpen;
}
else
rcvdTopic = topicName;
msgReceived = 1;
}

aws_sgp30_publisher, gg_sgp30_publisher

สองตัวอย่างนี้ใช้ Air Impurity Sensor รุ่น SGP30 จาก Adafruit เชื่อมต่อกับหนึ่งใน I2C port บน ESP32 ตามที่แสดงในแผนผังด้านล่าง ตัวอย่างเหล่านี้จำเป็นต้องติดตั้งไลบรารี ADAFruit_SGP30 ตามที่ระบุในข้อ 3 ของส่วนก่อนหน้า

แผนผังวงจร (Circuit diagram):

ทั้งสองตัวอย่างใช้โค้ดเดียวกัน ยกเว้นส่วนที่เชื่อมต่อ ESP32 Arduino เข้ากับ Cloud ในตัวอย่าง aws_sgp30_publisher เราใช้ฟังก์ชัน "connectToIoTCore" เพื่อ Publish ค่าที่วัดได้ไปยัง AWS IoT Core โดยตรง ส่วนในตัวอย่าง gg_sgp30_publisher เราใช้ Member function "connectToGG" เพื่อส่งค่าที่วัดได้ไปยังอุปกรณ์ Greengrass Edge

aws_bme280_sgp30_publisher

ในตัวอย่างนี้เราจะแสดงวิธีการใช้ Sensor สองตัวบน I2C bus เดียวกัน ได้แก่ BME280 (อุณหภูมิ, ความชื้น, ความกดอากาศ, ความสูง) และ SGP30 โค้ดตัวอย่างนี้จะคล้ายกับ aws_sgp30_publisher โดยมีการเพิ่มการตั้งค่าเริ่มต้น (Initialization) และการอ่านค่าจาก Sensor Adafruit BME280

นี่คือแผนผังวงจร (Circuit diagram):

Code

🔒 ปลดล็อก Code

สนับสนุนเพื่อรับ Source Code หรือแอปพลิเคชันสำหรับโปรเจกต์นี้

รหัสอ้างอิงโปรเจกต์: aws-iot-arduino-library-for-esp32-e40660
2450 บาท
PromptPay QR Code