โปรเจกต์ AWS IoT Arduino library สำหรับ ESP32
library นี้ใช้ AWS C-SDK เพื่อพัฒนา Arduino class ชื่อ AWSGreenGrassIoT เพื่อให้ง่ายต่อการเชื่อมต่อ sensors/actuator เข้ากับ AWS IoT อย่างปลอดภัย
library นี้ใช้ AWS C-SDK เพื่อพัฒนา Arduino class ชื่อ AWSGreenGrassIoT เพื่อให้ง่ายต่อการเชื่อมต่อ sensors/actuator เข้ากับ AWS IoT อย่างปลอดภัย
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
ไลบรารีนี้อ้างอิงตาม 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:
ก่อนที่จะใช้ตัวอย่างจากเมนู Sketch -> Examples โปรดอย่าลืม:
char WIFI_SSID[]="SSID";
char WIFI_PASSWORD[]="PASSWORD";
char AWSIOTURL[]="xxxxxxxxxxxxxxx-ats.iot.region.amazonaws.com";
char THING[]= "your device name here";
ไลบรารี AWSGreenGrassIoT มาพร้อมกับ 5 ตัวอย่างการใช้งาน:
ในตัวอย่างเหล่านี้ Servo motor จะถูกเชื่อมต่อกับ Analog GPIO port0 บน ESP32 และจำลองการเปิด-ปิดหน้าต่างจากระยะไกลโดยการหมุนมอเตอร์ ±90 องศา ขึ้นอยู่กับ Topic "Window" ที่ Subscribe ไว้ โดยคำสั่ง "open" จะหมุนมอเตอร์ +90 องศา และคำสั่ง "close" จะหมุนมอเตอร์ในทิศทางตรงกันข้าม -90 องศา
แผนผังวงจร (Circuit diagram):
ความแตกต่างเพียงอย่างเดียวระหว่างสองตัวอย่างนี้คือ:
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);
}
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;
}
สองตัวอย่างนี้ใช้ 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
ในตัวอย่างนี้เราจะแสดงวิธีการใช้ Sensor สองตัวบน I2C bus เดียวกัน ได้แก่ BME280 (อุณหภูมิ, ความชื้น, ความกดอากาศ, ความสูง) และ SGP30 โค้ดตัวอย่างนี้จะคล้ายกับ aws_sgp30_publisher โดยมีการเพิ่มการตั้งค่าเริ่มต้น (Initialization) และการอ่านค่าจาก Sensor Adafruit BME280
นี่คือแผนผังวงจร (Circuit diagram):
สนับสนุนเพื่อรับ Source Code หรือแอปพลิเคชันสำหรับโปรเจกต์นี้