R-Car/AWS IoT Greengrass

From eLinux.org
< R-Car
Revision as of 03:48, 9 December 2022 by Tnx94241 (talk | contribs) (Created page with "{{TOC right}} Category:R-Car == Introduction == This page describes how to setup the Ubuntu environment to use the AWS IoT Greengrass with R-Car and run it. This page c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

This page describes how to setup the Ubuntu environment to use the AWS IoT Greengrass with R-Car and run it.

This page contains information abot running AWS IoT Greengrass on Ubuntu environment on:

Software revisions

Software Revision
OS Ubuntu 20.04.5
Kernel Ver 5.10.41
Userland 64/32bit 64
U-Boot 2020.10
Greengrass 2.7.0

Environment

Host PC

OS Ubuntu 22.04 LTS (64bit)
Memory 16 GB or more
Storage At least 100 GB free

Boards confirmed to work

Board Confirmed
R-Car S4 Reference Board/Spider OK


Start Ubuntu on R-car S4

In subsequent chapters, the tags will be included in the title as follows.

  • Work on the target board (R-Car S4) is described as <Terminal>.
  • Work on the host PC is described as <PC>.
  1. <PC> Install software.
    $ sudo apt install qemu-user-static flex bison build-essential crossbuild-essential-arm64 libssl-dev git
    
  2. <PC> Make directory.
    $ mkdir S4_ubuntu
    
  3. <PC> Change directory.
    $ cd S4_ubuntu
    
  4. <PC> Copy tools>renesas-rcar-s4>make-rootfs.sh from https://github.com/sparkgene/aws-iot-fleetwise-edge/tree/fcc9bc471eee520a31d3805c628c236f69aae056 to Host PC.
    $ vim make-rootfs.sh
    
  5. <PC> Give authority.
    $ sudo chmod +x ./make-rootfs.sh
    
  6. <PC> Execute.
    $ sudo ./make-rootfs.sh 20.04.5 spider
    
  7. <PC> Setup TFTP server and NFS server.
    For more details, please refer to section "Setup TFTP server" and "Setup nfs-kernel-server" in "R-Car S4_StartupGuide_x_x_x.pdf"
  8. <Terminal> Boot NFS.
    =>setenv serverip XXX.XXX.XXX.XXX
    =>setenv ipaddr XXX.XXX.XXX.XXX
    =>setenv ethaddr XX:XX:XX:XX:XX:XX
    =>setenv bootargs 'rw root=/dev/nfs nfsroot={serverip}:/work/export/,nfsvers=3 ip={ipaddr}:::::tsn0 ignore_loglevel cma=560M'
    =>setenv bootcmd 'tftp 0x48080000 Image; tftp 0x48000000 r8a779f0-spider.dtb; booti 0x48080000 - 0x48000000'
    =>run bootcmd
    
  9. <PC> Copy Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 from current directory to /work/export/.
    $ sudo cp Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 /work/export/
    
  10. <Terminal> Flash Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4.
    # dd if=./Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 of=/dev/mmcblk0 bs=1M status=progress
    
  11. <Terminal> Synchronize.
    $ sync
    
  12. <Terminal> Reboot S4.
    Power off and Power on SW1.
    $ poweroff
    
  13. <Terminal> Boot eMMC.
    => setenv bootargs 'rw root=/dev/mmcblk0p1 rootwait ignore_loglevel cma=560M'
    => setenv bootcmd 'ext4load mmc 0:1 0x48080000 boot/Image; ext4load mmc 0:1 0x48000000 boot/r8a779f0-spider.dtb; booti 0x48080000 - 0x48000000'
    => run bootcmd
    


Start Ubuntu on R-car S4

In subsequent chapters, the tags will be included in the title as follows.

  • Work on the target board (R-Car S4) is described as <Terminal>.
  • Work on the AWS Cloud is described as <Web>.
  • Work on the host PC is described as <PC>.
  1. <Web> Create an AWS account
    https://console.aws.amazon.com/console/home
    Create an AWS account and log in to the AWS Management Console as the root user.
  2. <Web> Creating an IAM user
    Create an IAM user for work with reference to the following.
    https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_users_create.html#id_users_create_console
    The following policies should be attached at the time of creation.
    • AWSLambda_FullAccess
    • IAMFullAccess
    • AWSIoTFullAccess
    • AWSGreengrassFullAccess
    • IAMAccessAnalyzerFullAccess
    Note: Be sure to write down the "Access Key ID", "Secret Access Key", and "Password" that are displayed when users access the system. Note that the secret access key and password can only be confirmed at this time.
    Warning Warning: These three items are very important information for security reasons, and should be managed with great care.
    Sign out the AWS root user and log in with the IAM user you created.
    https://console.aws.amazon.com/console/home
  3. <Web> Setting up a region
    Set the desired region from the upper right corner of the screen.
    In this procedure, we will set "Asia Pacific(Tokyo) ap-northeast-1".
  4. <Terminal> Expand partition on R-Car S4.
    $ sudo parted
    (parted) resizepart 1
    End?  [31.8GB]? 100%
    (parted) quit
    $ sudo resize2fs /dev/mmcblk0p1
    
  5. <Terminal> Download AWS IoT Greengrass Core Software.
    $ curl -s https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-nucleus-latest.zip > greengrass-nucleus-latest.zip
    $ wget https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-2.8.0.zip
    
  6. <Terminal> Unzip AWS IoT Greengrass Core Software.
    $ unzip greengrass-nucleus-latest.zip -d GreengrassInstaller && rm greengrass-nucleus-latest.zip
    $ unzip greengrass-2.8.0.zip -d GreengrassInstaller && rm greengrass-2.8.0.zip
    
  7. <Terminal> Install Java.
    $ sudo apt update
    $ sudo apt install default-jre
    
  8. <Terminal> Confirm AWS IoT Greengrass Core Software’s version.
    $ java -jar ./GreengrassInstaller/lib/Greengrass.jar --version
    
  9. <Terminal> Setup environment value and directory.
    $ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
    $ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
    $ sudo mkdir -p /home/ggc_user
    $ sudo chown ggc_user:ggc_group /home/ggc_user
    
  10. <Terminal> Install AWS IoT Greengrass Core Software.
    $ sudo -E java -Droot="/greengrass/v2" -Dlog.store=FILE -jar ./GreengrassInstaller/lib/Greengrass.jar --aws-region region --thing-name MyGreengrassCore --thing-group-name MyGreengrassCoreGroup --thing-policy-name GreengrassV2IoTThingPolicy --tes-role-name GreengrassV2TokenExchangeRole --tes-role-alias-name GreengrassCoreTokenExchangeRoleAlias --component-default-user ggc_user:ggc_group --provision true --setup-system-service true --deploy-dev-tools true
    
  11. <Terminal> Wait a minute, confirm version and component list.
    $ /greengrass/v2/bin/greengrass-cli -V
    
  12. <Terminal> Confirm greengrass.service status.
    $ sudo systemctl status greengrass.service
    
  13. <Terminal> If greengrass.service doesn’t start, you need to start it.
    $ sudo systemctl start greengrass.service
    
  14. Attach policy.
    1. <Web> Select "IAM" from AWS services.
    2. <Web> Select "Policies" from the side menu.
    3. <Web> Select "Create Policy".
    4. <Web> Select "IoT" for Service, "Publish" for Actions, and "All Resources" for Resources.
    5. <Web> Click "Next: Tags" and "Next: Review", then enter any "Name" in the Review policy, and click "Create Policy" button.
      In this procedure, the name shall be "R-CarS4Device-IoT-Publish".
    6. <Web> Return to the IAM screen and select "Roles" from the side menu.
    7. <Web> Enter "GreengrassV2TokenExchangeRole" in the search and select the role with the same name.
    8. <Web> Click "Attach Policies", search for the policy you just created(Account name), and click "Attach Policy".
  15. Prepare the execution environment for Lambda functions.
    Build an environment to run the sample program on a terminal.
    <Terminal> Execute the following commands.
    $ sudo pip3 install numpy
    
  16. The endpoints can be found below.
    1. <Web> Select "IoT Core" from AWS services.
    2. <Web> Select "Settings" from the side menu.
    3. <Web> Refer to "Endpoint" in Device data endpoint, take notes.
      xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com
  17. Create a Lambda Function.
    Create a Lambda function to deploy to the terminal.
    <PC> Run the following command to create a template for a Lambda function.
    $ mkdir -p ~/lambda && touch ~/lambda/lambda_function.py
    
  18. <PC> Copy and paste the following code into the lambda_function.py that you created.
    Rewrite the endpoint_url to xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com that you wrote down.
    import datetime
    import json
    import numpy
    import boto3
    
    class DummyTemperatureSensor(object):
        def __init__(self, loc=25, scale=1, size=1):
            self.loc = loc
            self.scale = scale
            self.size = size
    
        def get_value(self):
            return numpy.random.normal(self.loc, self.scale, self.size)[0]
    
    
    def lambda_handler(event, context):
        print(event)
    
        # AWS IoT connection
        iot = boto3.client('iot-data', endpoint_url='https://xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com')
    
        # get temperature
        sensor = DummyTemperatureSensor()
    
        # set publish paramater
        topic='topic/sensor/temperature'
        payload = {
            "timestamp": str( datetime.datetime.now() ), 
            "temperature": sensor.get_value()
        }
        print(payload)
    
        # publish to AWS IoT
        iot.publish(
                topic=topic,
                qos=0,
                payload=json.dumps(payload, ensure_ascii=False)
            )
    
  19. Zip the packages.
    <PC> Zip the lambda_function.py and boto3 packages you have created.
    $ cd ~/lambda
    $ sudo apt install docker.io
    $ sudo docker run --rm -v $(pwd):/var/task amazon/aws-sam-cli-build-image-python3.8:latest pip install boto3 -t ./
    $ sudo zip -r lambda_artifact.zip ./
    
    The zip file is copied to a PC that has access to the AWS cloud for uploading to the cloud.
  20. Register the Lambda function.
    Register the created Lambda function to the AWS cloud.
    1. <Web> Select "Lambda" from AWS services.
    2. <Web> Select "Functions" from the side menu.
    3. <Web> Select "Create function".
    4. <Web> Check the "Author from scratch" box.
    5. <Web> Give an arbitrary name to the "Function name". In this procedure, "DummyTemperatureSensor" is used.
    6. <Web> Select "Python 3.8" as the Runtime.
    7. <Web> Select "arm64" as the Architecture.
    8. <Web> Other items keep the default values and click "Create Function".
    9. <Web> When the screen changes, Move Configuration tab and Timeout change 1 min 0 sec.
    10. <Web> Move Code tab, select "Upload from" → ".zip file", and upload → click "Save".
    11. <Web> Select "Actions" → "Publish new version" in the upper right corner and click “Publish”.
  21. Deploy the Lambda function.
    Deploy the registered Lambda function to the terminal.
    1. <Web> Select "IoT Core" from AWS services.
    2. <Web> Select "Greengrass devices" → "Components" from the side menu.
    3. <Web> Click "Create component".
    4. <Web> Check the "Import Lambda function" box and select the function that you created in "Lambda function", enter "topic/sensor/get" in the Topic field of the Event sources, select "AWS IoT Core MQTT" as the Type of the Event sources, enter "60" in the Timeout field of the Event sources.
    5. <Web> Set the Memory size of Container parameters to "64 MB".
    6. <Web> Other items keep the default values and click "Create component".
    7. <Web> When the screen changes, click "Deploy" in the upper right corner.
    8. <Web> Select “Add create”, and click your deployment and "Next".
    9. <Web> Other items keep the default values and click "Next" several times, and click "Deploy" at the last confirmation screen.
  22. Operation check.
    Confirm that the function you created will be deployed on the terminal side.
    Also, check the log on the terminal side to see if messages are being received by AWS IoT → terminal.
    Check the operation of R-Car SK.
    Confirm that the Lambda function has been deployed.
    <Terminal> Execute the following command to confirm.
    $ sudo /greengrass/v2/bin/greengrass-cli component list
    
    When you execute the command the following log will be output, and you can see that the deployed function (DummyTemperatureSensor) is displayed in the list.
    root@m3ulcb:~# /greengrass/v2/bin/greengrass-cli component list
        :
        :
    
    Component Name: DummyTemperatureSensor
        Version: 1.0.0
        State: RUNNING
        Configuration: {"containerMode":"GreengrassContainer","containerParams":{"devices":{},"memorySize":64000.0,"mountROSysfs":false,"volumes":{}},"inputPayloadEncodingType":"json","lambdaExecutionParameters":{"EnvironmentVariables":{}},"maxIdleTimeInSeconds":60.0,"maxInstancesCount":100.0,"maxQueueSize":1000.0,"pinned":true,"pubsubTopics":{"0":{"topic":"topic/sensor/get","type":"IOT_CORE"}},"statusTimeoutInSeconds":60.0,"timeoutInSeconds":3.0}
    
  23. Check the log.
    <Terminal> Run the following command to check the log.
    $ sudo –s
    $ cd /greengrass/v2/logs/
    $ tail -f <name of created function>.log
    
    Verify that the following log is output.
    serviceName=<name of created function>, currentState=RUNNING
  24. Checking the operation of the AWS cloud.
    1. <Web> Select "IoT Core" from AWS services.
    2. <Web> Select "MQTT Test Client" from the side menu.
    3. <Web> Enter "topic/sensor/temperature" in the Topic filter and click "Subscribe".
    4. <Web> Enter "topic/sensor/get" in the Topic name of Publish to a topic and click "Publish.
    Note: The default message payload will be garbled on the terminal side, so change it to any alphanumeric text if necessary.
  25. <Terminal> The message received will be displayed in the log as follows.
    root@m3ulcb:/greengrass/v2/logs# tail -f DummyTemperatureSensor.log
    2021-12-06T04:50:40.524Z [INFO] (pool-2-thread-38) DummyTemperatureSensor: lambda_function.py:17,{'message': 'AWS IoT ??????????????????????????????'}. {serviceInstance=0, serviceName=DummyTemperatureSensor, currentState=RUNNING}
    2021-12-06T04:50:41.172Z [INFO] (pool-2-thread-38) DummyTemperatureSensor: lambda_function.py:31,{'timestamp': '2021-12-06 04:50:41.168002', 'temperature': 27.381898093845034}. {serviceInstance=0, serviceName=DummyTemperatureSensor, currentState=RUNNING}
    2021-12-06T04:51:14.939Z [INFO] (pool-2-thread-38) DummyTemperatureSensor: lambda_function.py:17,{'message': 'hello world'}. {serviceInstance=0, serviceName=DummyTemperatureSensor, currentState=RUNNING}
    2021-12-06T04:51:14.969Z [INFO] (pool-2-thread-38) DummyTemperatureSensor: lambda_function.py:31,{'timestamp': '2021-12-06 04:51:14.964931', 'temperature': 25.003559249938885}. {serviceInstance=0, serviceName=DummyTemperatureSensor, currentState=RUNNING
    
  26. <Web> A message is received in the subscription.