Focus on Fresh: Using AWS & IoT in Food Warehouses Pt. 1

Focus on Fresh: Using AWS & IoT in Food Warehouses Pt. 1

Deploying an OPC-UA Server with Node.js

·

5 min read

As I've worked in food for the last ten years, the hardest part about trying to make the transition to tech is trying to figure out exactly where I stand or want to stand for that part. The "impostor syndrome is real", as they say, and with the seemingly endless possibilities, it can be hard to figure out where to start on your path. AWS re/Start was the entry point for me. Earning a Cloud Practitioner certification gave me familiarity with cloud infrastructure and how to use it to scale applications securely and with speed, but I still wasn't sure if, when, and where my past would aid me in my future.

IMG_20220311_073918327.jpg

However, as I let my natural interests lead the way, the vision started to become clearer. Single board computers, sensors, and machine learning had called out to me the entirety of my boot camp, so I started doing what I could to amass different parts to tinker with. Little by little, in the process of sharing my newfound hobby and talking to friends that work in food, it started to make sense. What about using this technology to improve warehouse efficiency and safety? When dealing with food specifically, a few hours can make the difference between a pleasant customer experience and negative associations that can have lifelong health effects. A few degrees of swing in a refrigeration unit could cause the loss of entire inventory. What if some of these essential business functions could be monitored and reported on asynchronously using sensors, programmatic controllers, and cloud based servers and storage? How much more efficient would companies that could leverage this technology be? Surely, it'd be worth exploring.

Screen Shot 2022-04-03 at 1.08.40 PM.png

With that in mind, I was driven to learn how to combine these technologies in a way to do just that. Lo and behold, as with most things, AWS has already figured that out and launched a tutorial for it. Success! The first step would be to deploy an EC2 instance which would act as my OPC-UA server. "OPC-UA is a modern communications protocol for industrial automation that is increasingly being adopted for data collection and control by traditional on-premises applications and Industrial IoT." In essence, this server would collect our data being reported by the sensors and other devices in our local system and process the pushing of that to the cloud.

*To follow along you will need an active AWS account

curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum install -y nodejs
wget https://aws-tc-largeobjects.s3.us-west-2.amazonaws.com/DEV-AWS-MO-IIOT/downloads/server.zip
unzip server.zip
npm install
./create_certs.sh
npm run start

Open Platform Communication Unified Architecture is "a machine-to-machine (M2M) communication protocol for industrial automation." To start building ours, the first thing I did was deploy an EC2 server and upload NodeJS to it via the the command line in AWS EC2 Instance Connect. With that done, I could download and extract the server application, and use bash to retrieve the simulated dataset I would work with initially. If you are following along, you will know that you have successfully done this by seeing the string 'server now waiting for connections.' Yay, we did it!

Screen Shot 2022-04-04 at 7.57.58 AM.png

sudo cp opcua-factory.service /lib/systemd/system/
sudo systemctl start opcua-factory
sudo systemctl enable opcua-factory
systemctl status opcua-factory

Next, you use the command line to stop the server manually, and you will set it as an automatically restarting feature so that it is continually running. Lastly, you can view a log of the service status using systemctl to ensure you've done everything correctly. Voila! Server successfully deployed.

Screen Shot 2022-04-04 at 8.54.05 AM.png

Ok, so we set up an OPC-UA server that would translate the radio frequencies from our sensors and relay that data to our cloud based systems. But where could that information live locally? Could we have an edge location or program that picks it up as well alongside our cloud-based deployment? Absolutely! That's where AWS IoT GreenGrass comes in handy.

Screen Shot 2022-04-06 at 8.10.28 AM.png

To use GreenGrass we will have to create an installer policy and then attach it to a role designated to act on an EC2 instance. Remember to specify that the role is for EC2 specifically! Launch an instance named "greengrass device" with the new role attached in the "configure instance" screen. Just that simply we created a "thing" which could represent an on-site computer processing and storing our sensor data. We will connect with EC2 Instance Connect so it is perfectly okay to proceed without a key pair here.

Select that instance in EC2 and start a dialogue with green grass device using Instance Connect. Run a root shell and use sudoers to give Greengrass the necessary additional privileges to switch groups. After that install the java runtime environment - coretto, and our green grass iot core software.

sudo -s
sed -i.bak "s/^root.*/root    ALL=(ALL:ALL) ALL/g" /etc/sudoers
amazon-linux-extras install corretto8
curl -s https://d2s8p88vqu9w66.cloudfront.net/releases/greengrass-nucleus-latest.zip > greengrass-nucleus-latest.zip
unzip greengrass-nucleus-latest.zip -d GreengrassInstaller && rm greengrass-nucleus-latest.zip

Download our nucleus using a curl command and then unzip the installer as well. This installer will need to know what region we are working in so we provide that here.

EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"

Next we run our installer and when prompted, include the following parameters for region, thing name, thing group, and aws resources. The installer will configure the AWS IoT Greengrass software, and create resources in IAM and AWS IoT

java -Droot="/greengrass/v2" -Dlog.store=FILE \
  -jar ./GreengrassInstaller/lib/Greengrass.jar \
  --aws-region $EC2_REGION \
  --thing-name iiot \
  --thing-group-name iiot-group \
  --thing-policy-name iiot-thing-policy \
  --tes-role-name iiot-token-exchange-role \
  --tes-role-alias-name iiot-token-exchange-alias \
  --component-default-user ggc_user:ggc_group \
  --provision true \
  --setup-system-service true \
  --deploy-dev-tools true

Screen Shot 2022-04-05 at 9.05.33 AM.png

Let's check our status to make sure we did that correctly!

systemctl status greengrass

Screen Shot 2022-04-05 at 9.05.59 AM.png

Can you believe it? We did it! Now, let's see if we can grab that sensor data and extract some insights. See ya'll in Part 2!

*Please delete any created assets to not incur charges