Powered by the popular Arduino MKR GSM 1400 and MKR NB 1500 development boards, the kit includes everything you need to build a connected device.
This example reads a Seeed Studio Ultrasonic Grove sensor and publishes a JSON message to the Soracom Unified endpoint when an object is within a specified range. This can serve as a proximity alert system.
In this guide, I show how to forward device messages to a cloud function written in Python using Soracom Funk and AWS Lambda. I also demonstrate how to send email and Slack alerts whenever the devices publishes a message.
Activate SIMSoracom Air provides worldwide, commitment-free mobile data service designed for IoT and M2M with coverage in 130 countries. The IoT and M2M SIM card provides an easy-to-use, secure connectivity platform to 2G/3G/LTE/Cat-M1 data with flexible pay-as-you-go rates.
Your device authenticates and connects to the Soracom network using the Soracom Air SIM card. Instead of having to use certificates or API tokens - the SIM card acts as the key. This gives you a secure connection on 2G/3G/LTE/Cat-M1 data.
Before inserting it into your device, the SIM card needs to be registered to an account and activated. It also needs to be attached to a Group in order to configure Soracom services such as Soracom Funk.
- Navigate to https://console.soracom.io/ to sign in or register a Soracom account.
- Choose Register SIM.
- Enter the ICCID and PUK (written on the back of the SIM card) and name your SIM - ideally something that signifies the device associated with it.
- Choose Create from the Group drop-down and name it something like "Arduino".
- Click Register.
The AWS Serverless Application Repository is a catalog of ready to deploy applications published by the community. I have published a basic application that deploys a Lambda function and an Amazon Simple Notification Service Email topic. You can deploy it from the browser using the following steps or manually using the AWS SAM CLI and the template included in the GitHub repository for this project.
- Login to the AWS Console.
- Navigate to soracom-sns-alert in the Serverless Application Repository.
- Under Application Settings provide an email address that you have access to.
- Check the box next to "I acknowledge that this app creates custom IAM roles."
- Click Deploy.
- After a few moments you will receive an email from AWS to confirm an email notification subscription.
- Once fully deployed, make a note of the ARN for the Lambda function listed under resources.
- Navigate to AWS Identity and Access Management console.
- Click on Users from the Menu and then click Add User.
- Check Programmatic Access and then click Next.
- Click Attach existing policies directly.
- Use the Search filter and type in "SoracomAlertManagedPolicy" and add select it from the list.
- Click through to Create User.
- Make note of the Access Key ID and Secret Access Key.
Soracom Funk is an adapter service that sends data from a device directly to a cloud service function for processing. Funk allows you to greatly simplify the logic embedded on a device, reducing device-side resource consumption, and instead handle data in the cloud without setting up complex server environments.
The app from the previous section deploys an AWS Lambda function. We'll need to configure Soracom Funk to forward messages to the Lambda function.
- Navigate to Groups in the Soracom Console.
- Select the group created for the SIM card.
- Under Soracom Funk, select AWS Lambda for service.
- Create a new pair of credentials using the Access Key ID and Secret Access Key for the IAM User created earlier.
- Add the Lambda function ARN from the serverless app deployment.
- Click Save.
Download and install the Arduino IDE for your operating system. https://www.arduino.cc/en/Main/Software
Install librariesTo install libraries:
- Open the Arduino IDE
- Go to Sketch > Manage Libraries.
- Use the search bar to find and install the libraries listed below.
Install the library that corresponds to your device.
Install Board dependanciesTo program the Arduino MKR GSM 1400 or MKR NB 1500 you'll need to install support for Arduino SAMD Boards.
- Open the Arduino IDE
- Go to Tools > Board > Boards Manager.
- Search "Arduino SAMD Boards" and choose Install.
- Open the Arduino IDE
- Create a new sketch. File > New
- Copy the contents of the example sketch corresponding to your Arduino MKR device.
- Attach the Ultrasonic Grove sensor to the Arduino MKR Connector Carrier.
- Plugin the Arduino MKR device to your computer using a USB cable.
- Select the corresponding port in the Arduino IDE. Tools > Port
- Press upload, this compiles, verifies, and flashes the device with the new firmware. Sketch > Upload
- Open the Serial Monitor by clicking the magnifying lens icon. This will allow you to see what the device logs, such as errors, connection status, and any messages it publishes.
Once connected, if an object is detected within 10 CM of the rangefinder a message is published to the Soracom Unified Endpoint. Soracom Funk will then invoke the Lambda function which publishes to an Amazon Simple Notification Service email topic. The email subscribed in the SAR app will then be notified.
Modifying the Lambda functionThe AWS Lambda console allows you to edit and instantly deploy changes to a Lambda function inline. This particular function is using the Python runtime. The event object contains the message sent from the Soracom device as JSON.
To edit:
- Navigate to the AWS Lambda Console.
- Select the SendAlertFunction from the list.
- In the Code source editor change the code as needed and click Deploy.
As an exercise, try incorporating the following snippet of Python code for posting to a Slack webhook!
import urllib3
http = urllib3.PoolManager()
data = {"channel": "#general", "username": "Sorabot", "text": json.dumps(event), "icon_emoji": “:ghost:”}
encoded_data = json.dumps(data).encode('utf-8')
r = http.request(
'POST',
'',
body=encoded_data,
headers={'Content-Type': 'application/json'}
)
What next?!Let's recap what we've done!
- Activated a Soracom SIM card.
- Deployed a serverless application using AWS Lambda.
- Configured Soracom Funk to forward device messages to a Lambda function.
- Programmed an Arduino device to publish messages over cellular.
- Learned how to modify the Lambda function with custom code.
Combining the principles learned in this guide you can now deploy remote cellular devices that can publish messages to the cloud and do a variety of tasks. This allows you to build complex automations or operation using cloud services.
What will you build? Share with us by tweeting at @SoracomIoT and #SoracomMakes
Hack the planet!
Comments
Please log in or sign up to comment.