ทดสอบ AWS IoT Greengrass ด้วย Raspberry Pi พาร์ท 1/2 : การติดตั้ง AWS IoT Greengrass

ทดสอบ AWS IoT Greengrass ด้วย Raspberry Pi พาร์ท 1/2 : การติดตั้ง AWS IoT Greengrass

AWS Greengrass core ติดตั้งยังไง Deploy public component ยังไง บทความนี้มีคำตอบ
Clock Icon2024.05.27 01:23

สวัสดีครับ ต้าครับ

วันนี้ผมจะมาทดสอบ ทดลองใช้งาน AWS IoT Greengrass จริงๆกันครับ
โดยในพาร์ท 1/2 นี้จะเป็นบทความการเตรียมตัวต่างๆ ที่เขียนเกี่ยวกับอุปกรณ์ที่ใช้ทดลองและการติดตั้ง AWS IoT Greengrass core software ลงใน Edge Device(Raspberry Pi) กันครับ หวังว่าบทความนี้จะเป็นประโยชน์สำหรับคนที่กำลังมองหาวิธีการติดตั้ง AWS IoT Greengrass core software ลงใน Edge Device กันนะครับ

สำหรับใครที่ไม่รู้จักว่า AWS IoT Greengrass คืออะไร สามารถเข้าไปอ่านบทความด้านล่างนี้ก่อนได้ครับ

ขั้นตอนโดยรวม

ขั้นตอนคร่าวๆ ที่จะเขียนในบทความนี้จะมีประมาณนี้ครับ

  1. เตรียมอุปกรณ์
  2. ติดตั้ง Raspberry Pi OS ใน Raspberry Pi
  3. ติดตั้ง AWS IoT Greengrass core software V2 ใน Raspberry Pi
  4. Deploy Public Component จาก AWS IoT Greengrass ไปยัง Raspberry Pi

อุปกรณ์ที่ใช้ในการทดลอง

  • คอมพิวเตอร์(เครื่องที่คุณใช้อ่านบทความอยู่ตอนนี้)
  • Raspberry Pi (ของผมใช้เป็น Raspberry Pi 4 Model B)
    • micro SD card เอามาเป็น storage ของ Raspberry Pi (ของผม 32 GB)
    • สายชาร์ต USB-C (เอามาจ่ายไฟให้ Raspberry Pi เป็น Power supply)
    • เมาส์ ของ Raspberry
    • คีย์บอร์ด ของ Raspberry
    • สาย HDMI ของ Raspberry
    • monitor ของ Raspberry
  • ตัวต้านทาน หรือ Resistor 1 ตัว ควรจะมากกว่า 75Ω
  • ไดโอดเปล่งแสง หรือ light diode 1 ตัว
  • breadboard 1 ตัว
  • breadboard jumper 2 ตัว

อุปกรณ์ที่รองรับ AWS Greengrass ที่เป็นของ partner สามารถตรวจสอบจากลิ้งค์ต่อไปนี้ครับ
Find IoT hardware that works with AWS | Search by industry, application, features, and more

หรือสเปคขั้นต่ำของ Device ที่จะใช้ AWS Greengrass ได้ก็สามารถตรวจสอบได้ที่ FAQs ด้านล่างนี้ครับ

Q: What are AWS IoT Greengrass Core devices? What minimum hardware specifications are required?
The AWS IoT Greengrass Core software runs on an IoT device, hub, or gateway to automatically sync and interact with the cloud. AWS IoT Greengrass Core is designed to run on devices with a general-purpose processor that are powerful enough to run a general-purpose operating system, such as Linux. AWS IoT Greengrass requires at least 1 GHz of compute (either Arm or x86), 96MB* of RAM (v2.0 edge runtime or higher), plus additional resources to accommodate the desired OS, message throughput, and AWS Lambda execution depending on the use case. AWS IoT Greengrass Core can run on devices that range from a Raspberry Pi to a server-level appliance.
AWS IoT Greengrass FAQs - Amazon Web Services

หรือตามภาพด้านล่างครับ(ไม่มีรูป เมาส์, คีย์บอร์ด, monitor, สาย HDMI, สายชาร์ต USB-C เพราะคิดว่าน่าจะรู้จักกันอยู่แล้ว)

ติดตั้ง Raspberry Pi OS ใน Raspberry Pi

ก่อนอื่นในเราไปทำการดาวน์โหลดตัวติดตั้ง Raspberry Pi OS ผ่านเว็บไซต์ต่อไปนี้ก่อนครับ Raspberry Pi OS – Raspberry Pi
โดยคอมที่ผมใช้อยู่ตอนนี้เป็น windows จึงขอเลือก ดาวน์โหลดแบบเป็น windows ครับ

ทำการติดตั้งตัว imager ที่เราดาวน์โหลดมาครับ

เสียบ SD card ของเราเข้าไป แล้วเปิดโปรแกรมที่ติดตั้งขึ้นครับ
เลือก Operating System เป็น Raspberry Pi OS (32-BIT)
เลือก Storage เป็น SD card ของเราครับ
จากนั้นกดรูปฟันเฟือง

เข้าไปตั้งค่า locale settings ให้เป็น เวลาของที่ๆเราอยู่ครับ

จากนั้นสามารถ WRITE ได้เลยครับ

พอเราติดตั้งเสร็จแล้วก็นำ SD card มาเสียบเข้าที่ตัว Raspberry Pi แล้วก็ทำการต่อสายต่างๆ(monitor, เมาส์, คีย์บอร์ด, สาย HDMI, สาย power supply)
จะขึ้นตามรูปภาพด้านล่างนี้ครับ ให้เรากดติดตั้งตามภาพไปได้เลยครับ

ในส่วนนี้ผมจะเลือกทุกๆอย่าง รวมถึง Language เป็นภาษาไทย จะทำให้ข้อมูลที่แสดงบางส่วนใน Laspberry Pi เป็นภาษาไทยไปด้วย หากใครชอบภาษาอังกฤษ ก็สามารถเลือกภาษาอังกฤษได้ครับ

อันนี้เป็น username password ที่ผมจะใช้ในงานครับ ใครใช้ต่างจากนี้ให้จดไว้ครับ เพราะจะได้ใช้

ทำการต่อไวไฟครับ

จะมีให้เราทำการอัพเดท software ในเครื่อง ซึ่งผมแนะนำให้อัพเดทครับ
หากอัพเดทแล้วเกิด error สามารถอัพเดทได้อีกทีในหน้าต่าง desktop ครับ

ติดตั้งเสร็จแล้วกด Restart ครับ

ทำการตั้งค่า Raspberry Pi

ก่อนที่เราจะทำการติดตั้ง AWS IoT Greengrass core software V2 ใน Raspberry Pi เราจำเป็นต้องตั้งค่า Raspberry Pi กันก่อนครับ
โดยสิ่งที่เราจำเป็นต้องตั้งค่ามีดังต่อไปนี้ครับ

เปิด SSH port

ให้เรากดที่รูป Raspberry Pi ซ้ายบน แล้วกดปรับแต่งพื้นโต๊ะแล้วกดไปที่Raspberry Pi Configuration

ไปที่หัวข้อInterfaceเปิดการใช้งานSSHแล้วกดOKครับ

ทำการ install Java

เปิด Terminal ของ Raspberry Pi

ทำการอัพเดท software ต่างๆใน Raspberry Pi ก่อน

sudo apt update
sudo apt upgrade

ทำการติดตั้ง java (โดยผมเลือกเป็นเวอร์ชัน 17)

sudo apt install openjdk-17-jdk -y

ใช้คำสั่งต่อไปนี้เพื่อเช็คว่า java install เสร็จแล้วหรือยัง

java --version

หากเสร็จแล้วจะแสดงผลออกมาประมาณนี้

openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment (build 17.0.7+7-Raspbian-1deb11u1rpt1)
OpenJDK Client VM (build 17.0.7+7-Raspbian-1deb11u1rpt1, mixed mode, emulated-client)

ทำการเชื่อมต่อ SSH

ในขั้นตอนนี้เราจะใช้ port SSH เชื่อมไปยัง Raspberry Pi เพื่อทำการรันคำสั่งต่างๆ :7j'ในการทำงานจริงแล้วในการรันคำสั่งต่างๆมีหลายวิธี เราสามารถทำวิธีอื่นๆได้เช่น เข้าไปยัง Raspberry Pi ผ่าน RDP หรือ VNC หรือจะรันคำสั่งเหล่านี้โดยตรงที่ Raspberry Pi เลยก็ได้ แต่สำหรับบทความนี้เราจะใช้วิธี SSH ครับ

โดยการเชื่อมต่อ SSH นั้นสามารถใช้ได้หลาย Software ตั้งแต่ของที่มีติดเครื่อง windows คือ PowerShell หรือ cmd แต่ในบทความนี้เราจะใช้ Terminal ของ VScode ครับ
สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ VScode สามารถเข้าไปอ่านได้ที่ วิธีการติดตั้ง Visual Studio Code บน Windows 10/11 [2022] | DevelopersIO

โดยวิธีการเชื่อม SSH นั้นจำเป็นต้องทราบ IP Address ของ Raspberry Pi ก่อน(จำเป็นต้องต่อไวไฟเดียวกันกับเครื่องที่เราจะเชื่อม หรืออยู่ในวงเน็ตเดียวกัน)
((จำเป็นต้องมี Extention Remote – SSH ด้วย ดูวิธีติดตั้งได้จาก ทำการติดตั้ง Extention Remote – SSH ใน VSCode และทดลองใช้งาน EC2 โดยตรงใน VSCode | DevelopersIO)) วิธีค้นหา IP Address ที่ง่ายที่สุดคือ เปิด Terminal ของ Raspberry Pi แล้วใช้คำสั่งต่อไปนี้

hostname -I

เราจะได้ IP Address ของ Raspberry Pi มา โดยในกรณีของผมคือ 192.168.0.15 ให้ทำการจดไว้

กลับมาที่คอมพิวเตอร์ของเรา เปิด VScode แล้วกดปุ่มสีฟ้าซ้ายล่างตามในภาพ

แล้วกด Connect to Host...

พิมพ์ตามรูปแบบต่อไปนี้ (username password สามารถดูได้ที่ตอนที่เราติดตั้ง OS)

{username}@{IP Address ของ Raspberry Pi}

ซึ่งในกรณีของผมคือ

pi@192.168.0.15

ทำการใส่ password

ล็อกอินเสร็จแล้ว กดเมนูด้านบน Terminal แล้วก็ New Terminal แล้วก็จะได้หน้าต่าง terminal ตามด้านล่างที่เห็น

หลังจากนี้พักตรงส่วนนี้ไว้ก่อน แล้วไปทำในส่วนของ AWS Management Console กันต่อครับ

ติดตั้ง AWS IoT Greengrass core software V2 ใน Raspberry Pi

เตรียม IAM Role สำหรับ Greengrass

เราจำเป็นต้องสร้าง AWS credentials สำหรับ device โดยต้องเริ่มสร้างจาก IAM User ก่อนครับ ให้เราไปที่ Console ของ IAM หรือไปได้ที่ AWS IAM

กดตามภาพที่เห็นโดยเริ่มจากCreate User

ทำการตั้งชื่อ โดยผมใช้ชื่อว่า tutorial-light-diobe-user

เลือกAttach policies directly

เพิ่ม Policy เหล่านี้เข้าไปโดยแนะนำให้ใช้ช่อง search ช่วย

  • AmazonS3FullAccess
  • AWSGreengrassFullAccess
  • AWSIoTFullAccess
  • IAMFullAccess

เพิ่มเสร็จแล้วให้กด Next ด้านล่าง

แล้วกดCreateได้เลยครับ

ไปที่หน้าต่าง User ที่เราสร้างขึ้น แล้วไปที่หัวข้อSecurity credentials

เลื่อนลงมาด้านล่างที่หัวข้อAccess Key กดCreate access key

เลือก Use case เป็น CLI แล้วกดติ้กถูกด้านล่าง แล้วกด Next

ต่อไปให้กด Create access key เลย

จด Access key กับ Secret access key ไว้ จะ Download .csv ไว้ด้วยก็ดี

หมายเหตุ: โปรดระวังการ Access Key รั่วไหล หากทำการทดสอบเสร็จแล้วให้ทำการ InActive Access Key ด้วยครับ
สามารถอ่านเกี่ยวกับความอันตรายของการที่ Access Key รั่วไหล ได้ที่บทความต่อไปนี้

ติดตั้ง AWS IoT Greengrass core software

ให้เราเข้าไปที่ AWS IoT Greengrass Console ผ่านทางลิ้งค์ต่อไปนี้ AWS IoT Greengrass console แล้วกด Set up one core device ครับ

ตั้งชื่อ Core device name จะตั้งชื่อเป็นอะไรก็ได้ครับ ผมตั้งชื่อว่า Greengrass-tutorial-light-diobe ครับ
ในส่วน Thing group เลือกNo groupครับ(ในการทดสอบนี้ใช้แค่เดียว เลยไม่ต้องมี group)

เลื่อนลงมาใน Step 3 จะเห็นวิธีการ Install the Greengrass Core software
ซึ่งไปดูกันในแต่ละขั้นตอนกันครับ

Step 3.1: Install Java on the device <<< เราทำการติดตั้งไปแล้วในขั้นตอนที่แล้วๆ

Step 3.2: Configure AWS credentials on the device <<< เราสร้าง AWS credential แล้ว แต่ยังไม่ได้ใส่ลงไปใน Rarpberry Pi ให้เรากลับไปที่หน้าต่าง SSH แล้วใส่ credential ที่เราสร้างขึ้น

export AWS_ACCESS_KEY_ID=<AWS_ACCESS_KEY_ID>
export AWS_SECRET_ACCESS_KEY=<AWS_SECRET_ACCESS_KEY>

# ในส่วน <AWS_ACCESS_KEY_ID> กับ <AWS_SECRET_ACCESS_KEY> ให้ใส่เป็นข้อมูลของคุณ  
# จากหน้าต่าง IAM ตอนที่ทำการสร้าง Access key เราจะไม่ได้ AWS_SESSION_TOKEN มาด้วย ทำให้ไม่ต้องใส่ AWS_SESSION_TOKEN ครับ  

พอใส่แล้วจะได้ประมาณนี้

Step 3.3: Run the installer <<< ในส่วนนี้ให้เรา Copy command ที่เขียนและ Run ผ่าน SSH (Run ทีละอัน)

ก็จะได้ผลลัพท์ประมาณนี้ครับ

pi@raspberrypi:~ $ curl -s https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-nucleus-latest.zip > greengrass-nucleus-latest.zip && unzip greengrass-nucleus-latest.zip -d GreengrassInstaller
Archive:  greengrass-nucleus-latest.zip
  inflating: GreengrassInstaller/META-INF/MANIFEST.MF  
  inflating: GreengrassInstaller/META-INF/SIGNER.SF  
  inflating: GreengrassInstaller/META-INF/SIGNER.RSA  
  inflating: GreengrassInstaller/LICENSE  
  inflating: GreengrassInstaller/NOTICE  
  inflating: GreengrassInstaller/README.md  
  inflating: GreengrassInstaller/THIRD-PARTY-LICENSES  
  inflating: GreengrassInstaller/bin/greengrass.exe  
  inflating: GreengrassInstaller/bin/greengrass.service.procd.template  
  inflating: GreengrassInstaller/bin/greengrass.service.template  
  inflating: GreengrassInstaller/bin/greengrass.xml.template  
  inflating: GreengrassInstaller/bin/loader  
  inflating: GreengrassInstaller/bin/loader.cmd  
  inflating: GreengrassInstaller/conf/recipe.yaml  
  inflating: GreengrassInstaller/lib/Greengrass.jar  
pi@raspberrypi:~ $ sudo -E java -Droot="/greengrass/v2" -Dlog.store=FILE -jar ./GreengrassInstaller/lib/Greengrass.jar --aws-region ap-southeast-1 --thing-name Greengrass-tutorial-light-diobe  --component-default-user ggc_user:ggc_group --provision true --setup-system-service true --deploy-dev-tools true
Provisioning AWS IoT resources for the device with IoT Thing Name: [Greengrass-tutorial-light-diobe]...
Found IoT policy "GreengrassV2IoTThingPolicy", reusing it
Creating keys and certificate...
Attaching policy to certificate...
Creating IoT Thing "Greengrass-tutorial-light-diobe"...
Attaching certificate to IoT thing...
Successfully provisioned AWS IoT resources for the device with IoT Thing Name: [Greengrass-tutorial-light-diobe]!
Setting up resources for aws.greengrass.TokenExchangeService ... 
Attaching TES role policy to IoT thing...
No managed IAM policy found, looking for user defined policy...
IAM policy named "GreengrassV2TokenExchangeRoleAccess" already exists. Please attach it to the IAM role if not already
Configuring Nucleus with provisioned resource details...
Downloading Root CA from "https://www.amazontrust.com/repository/AmazonRootCA1.pem"
Created device configuration
Successfully configured Nucleus with provisioned resource details!
Creating a deployment for Greengrass first party components to the device
Configured Nucleus to deploy aws.greengrass.Cli component
Successfully set up Nucleus as a system service

ตรวจสอบการติดตั้ง

หลังจากนั้นพอเรากด View core devices ถ้าเราเห็น Device ที่เราสร้างแสดงอยู่ แสดงว่าติดตั้งสำเร็จครับ

แนะนำให้ทำหลังจากการติดตั้ง

หลังจากติดตั้งเสร็จแล้ว ให้เราไปตั้งค่า Deactivate Access key ที่เราสร้างขึ้น เพื่อป้องกันคนขโมย Key ของเราไปทำอะไรมิดีมิชอบครับ

Deploy Public Component จาก AWS IoT Greengrass ไปยัง Raspberry Pi

AWS IoT Greengrass จะมี Public Component ซึ่งเป็น component ที่ AWS เตรียมไว้ให้เราอยู่แล้ว โดยเราสามารถดาวน์โหลดได้ทั้งผ่าน CLI และ console แต่สำหรับผมแล้วแบบ console ง่ายกว่า
ใน tutorial นี้ เราเลยจะสอนเป็นวิธีที่ใช้ console กันครับ

โดยให้เรากดเข้าไปที่ Core devices > device ที่เราสร้างขึ้น > Deployments > เลือก Deployment ที่เราสร้างขึ้น

แล้วกดที่ Actions > Revise

กด Next ไปที่ Step 2 - Select components
ปิด Show only selected components แล้วหา component ที่ชื่อ aws.greengrass.LocalDebugConsole
ทำการติ๊กเพิ่ม aws.greengrass.LocalDebugConsole เข้ามา

หลังจากนั้นกด Next ไปเรื่อยๆจนถึง Deploy ก็กด Deploy ได้เลยครับ

จะเห็นได้ว่ามี component: aws.greengrass.LocalDebugConsole เพิ่มเข้ามาแล้ว

เราสามารถตรวจสอบไฟล์ที่ Edge Device หรือ Raspberry Pi ได้ว่าไฟล์ของเราได้ทำการ install ไปแล้วได้ด้วยการรัน command ต่อไปนี้

คำสั่ง:

$ sudo /greengrass/v2/bin/greengrass-cli component list

ผลลัพท์:

Components currently running in Greengrass:
Component Name: aws.greengrass.Nucleus
    Version: 2.11.2
    State: FINISHED
    Configuration: {"awsRegion":"ap-southeast-1","componentStoreMaxSizeBytes":"10000000000","deploymentPollingFrequencySeconds":"15","envStage":"prod","fleetStatus":{"periodicStatusPublishIntervalSeconds":86400.0},"greengrassDataPlaneEndpoint":"","greengrassDataPlanePort":"8443","httpClient":{},"iotCredEndpoint":"c1h0hkcg1sr3hq.credentials.iot.ap-southeast-1.amazonaws.com","iotDataEndpoint":"ahakc63geq1k5-ats.iot.ap-southeast-1.amazonaws.com","iotRoleAlias":"GreengrassV2TokenExchangeRoleAlias","jvmOptions":"-Dlog.store=FILE","logging":{},"mqtt":{"spooler":{}},"networkProxy":{"proxy":{}},"platformOverride":{},"runWithDefault":{"posixUser":"ggc_user:ggc_group"},"telemetry":{}}
Component Name: UpdateSystemPolicyService
    Version: 0.0.0
    State: RUNNING
    Configuration: null
Component Name: aws.greengrass.Cli
    Version: 2.11.2
    State: RUNNING
    Configuration: {"AuthorizedPosixGroups":null,"AuthorizedWindowsGroups":null}
Component Name: DeploymentService
    Version: 0.0.0
    State: RUNNING
    Configuration: null
Component Name: FleetStatusService
    Version: null
    State: RUNNING
    Configuration: null
Component Name: TelemetryAgent
    Version: 0.0.0
    State: RUNNING
    Configuration: null
Component Name: aws.greengrass.LocalDebugConsole
    Version: 2.3.1
    State: RUNNING
    Configuration: {"bindHostname":"localhost","httpsEnabled":"true","port":"1441","websocketPort":"1442"}

จะเห็นได้ว่า มี Component Name: aws.greengrass.LocalDebugConsole ถูกลงไว้อยู่

ทิ้งท้าย

สำหรับเนื้อหาในพาร์ท 1 ก็มีเพียงเท่านี้ สำหรับในพาร์ทต่อไปเราจะทำการลง Custom component ต่อแผงวงจรกันครับ

สำหรับบทความ 2/2 สามารถเข้าไปอ่านได้ที่ลิ้งค์ด้านล่างนี้

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>

สำหรับบทความนี้ก็มีเพียงเท่านี้ครับ ขอบคุณครับ

บทความอ้างอิง

AWS IoT GreengrassV2 (Part 1) Installing on a Raspberry Pi 3 2.0 aws iot - YouTube Raspberry Pi OS – Raspberry Pi
วิธีการติดตั้ง Visual Studio Code บน Windows 10/11 [2022] | DevelopersIO
ทำการติดตั้ง Extention Remote – SSH ใน VSCode และทดลองใช้งาน EC2 โดยตรงใน VSCode | DevelopersIO AWS IoT Greengrass คืออะไร? การแนะนำฟังก์ชันของ AWS IoT ในปี 2023 | DevelopersIO

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.