This article is the 20th day article of IoT Backend Advent Calendar 2015. Create an application that sends the accelerometer value from Edison to the Amazon Kinesis stream like IoT.
Intel Edison + Eaglet for Switch Science
The I / O of Intel Edison is 1.8V, but since the Eaglet has a conversion circuit to 3.3V, many of the sensors on the market can be used. In addition, Eaglet has a connector that allows you to connect GROVE. So, I will use GROVE's accelerometer.
GROVE --I2C 3-axis acceleration sensor ADXL345 installed
The connection is very easy. Simply connect the Grove connectors to each other with the cable that came with the sensor. On the contrary, you can rest assured that it will not stick.
I will do the part of the article of the last time and the last time.
-Updated Intel Edison firmware and packages from Eagle-- Qiita -Set time and SSH connection to Intel Edison --Qiita
Log in to Edison with SSH. Install the packages needed to develop your application in Python.
#Install Python package manager
root@my-edison:~# opkg install python-pip
#AWS CLI installation
root@my-edison:~# pip install awscli
#Install Python package for AWS
root@my-edison:~# pip install boto3
Set up the connection to connect to AWS.
root@my-edison:~# aws configure
AWS Access Key ID [None]: <Key for Edison>
AWS Secret Access Key [None]: <Key for Edison>
Default region name [None]: <Region you want to use>
Default output format [None]: <To your liking>
Create a Kinesis stream to stream the data. I created it from the AWS CLI here, but there is no problem if I do it from the AWS Management Console.
Advance the stream name as Sample-Edison-Stream
. Replace it with any name you like.
$ aws kinesis create-stream --stream-name Sample-Edison-Stream --shard-count 1
Check if the stream is created.
$ aws kinesis describe-stream --stream-name Sample-Edison-Stream
{
"StreamDescription": {
"RetentionPeriodHours": 24,
"StreamStatus": "CREATING",
...
When StreamStatus
becomes ʻACTIVE, it is complete. In case of
CREATING`, wait for a while.
Create an application that sends the accelerometer value to Kinesis about once every 0.1 seconds. Please use the created application as it is published in the following repository. https://github.com/mia-0032/edison-adxl345-to-kinesis
Edison has a package called upm (Useful Packages & Modules) installed from the beginning, which makes it easy to use sensors etc. You can do it.
sender.py
# -*- coding: utf-8 -*-
import boto3
import json
import pyupm_adxl345 as adxl345
from socket import gethostname
from sys import argv
from time import sleep
#Kinesis stream name is specified by argument
stream_name = argv[1]
#Eaglet's Grove connector is connected to I2C No. 6, so specify 6.
adxl = adxl345.Adxl345(6)
kinesis = boto3.client('kinesis')
while True:
#Get acceleration
adxl.update()
force = adxl.getAcceleration()
data = {'X': force[0], 'Y': force[1], 'Z': force[2]}
#Send data to Kinesis
res = kinesis.put_record(
StreamName=stream_name,
Data=json.dumps(data),
PartitionKey=gethostname() #This time there is only one shard so it doesn't really matter
)
sleep(0.1)
It can be executed with the following command.
root@my-edison:~# python sender.py Sample-Edison-Stream
Check if the data is flowing to Kinesis. We have created a Python script for confirmation, so please use it.
receiver.py
# -*- coding: utf-8 -*-
import boto3
import logging
from sys import argv
from time import sleep
#Generation of logger
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#Kinesis stream name is specified by argument
stream_name = argv[1]
logger.info("Stream Name: {0}".format(stream_name))
kinesis = boto3.client('kinesis')
#Get Shard ID
stream = kinesis.describe_stream(
StreamName=stream_name
)
#This time, since the stream is created with 1 shard, the first shard ID is used.
shard_id = stream['StreamDescription']['Shards'][0]['ShardId']
#Get the first shard iterator
shard_iterator = kinesis.get_shard_iterator(
StreamName=stream_name,
ShardId=shard_id,
ShardIteratorType='TRIM_HORIZON'
)['ShardIterator']
#Get records in a loop
while True:
res = kinesis.get_records(
ShardIterator=shard_iterator,
Limit=100
)
for r in res['Records']:
logger.info(
'Time: {0}, Data: {1}'.format(
r['ApproximateArrivalTimestamp'],
r['Data']
)
)
shard_iterator = res['NextShardIterator']
sleep(1)
It can be executed with the following command.
root@my-edison:~# python receiver.py Sample-Edison-Stream
If the record flows like this, it is a success.
INFO:root:Time: 2015-12-20 14:40:59.522000+09:00, Data: {"Y": -0.10188677161931992, "X": -0.075471684336662292, "Z": 0.95703125}
INFO:root:Time: 2015-12-20 14:40:59.675000+09:00, Data: {"Y": -0.056603759527206421, "X": 0.0037735840305685997, "Z": 0.94921875}
INFO:root:Time: 2015-12-20 14:40:59.833000+09:00, Data: {"Y": -0.24150937795639038, "X": 0.018867921084165573, "Z": 0.90625}
INFO:root:Time: 2015-12-20 14:40:59.983000+09:00, Data: {"Y": -0.22264145314693451, "X": -0.0037735840305685997, "Z": 0.9296875}
INFO:root:Time: 2015-12-20 14:41:00.133000+09:00, Data: {"Y": -0.15094336867332458, "X": 0.011320752091705799, "Z": 0.9375}
You have successfully sent the data to Kinesis! thank you for your hard work.
Recommended Posts