Showing posts with label IoT. Show all posts
Showing posts with label IoT. Show all posts

Tuesday, January 24, 2017

BeagleBone Green Wireless IoT Developer Prototyping Kit

Have you noticed that Google Cloud is promoting an IoT Developer Prototyping Kit which features the BeagleBone Green Wireless (BBGW)? I purchased one, because I was hoping for better instructions on using the Grove System w/BBGW.

Sadly, the only documentation included w/the proto kit is a lame brochure w/a cloud solutions URL with unhelpful Arduino examples.  Yaay for Arduino, but I bought the BBGW kit.

Fine.  Challenge accepted.  Here are some notes on using BBGW and Grove System.

There are dedicated images for the BBGW and any Debian reference (within this post) is tied to bone-debian-8.6-seeed-iot-armhf-2016-11-06-4gb.img

"Grove is a modular, standardized connector prototyping system." which is a good single line summary.  The catalog is full of many sensors, switches and shiny, blinky things which plug into standard connectors.  The BBGW exposes two Grove connectors, one for I2C and the other to UART.  Two connections are OK to start, but you most likely will want the Grove Base Cape (included in proto kit) because this offers more connections and access to the ADC.

If you already know the BB, you will not be surprised to learn that the Grove connectors map directly to P8/P9 and there is no added functionality apart from the connectors.  Put another way, anything you can implement w/Grove could also be implemented the usual way via overlays and P8/P9.

There has to be software to control the interfaces and BBGW Grove promotes MRAA as a wrapper to the BBGW devices.  MRAA comes w/the Debian Seeed image and has (yet another) pin encoding scheme.  Look here for pin mapping.  The Debian Seeed image also has example python code in /usr/share/mraa/examples/python

Start easy w/a GPIO demo "blink-io8.py"

import mraa
import time


# Using BBG GPIO_51
x = mraa.Gpio(62)
x.dir(mraa.DIR_OUT)

while True:
  x.write(1)
  time.sleep(0.2)
  x.write(0)
  time.sleep(0.2)


Note the "mraa.Gpio(62)" which maps to P9_16 on BBGW and has a dedicated socket on the Grove Base Cape (look at the middle row, the GPIO are silk screened next to the Grove sockets).

Use the 3 axis accelerometer from the proto kit as an I2C example.  One of the Seeed example worked, scroll to item "03 How to use Grove - 3-Axis digital Accelerometer (16g)" for a python example.

Now for an ADC example, look at this Seeed project (scroll to "02 How to use Grove - Light Sensor & Temperature Sensor").  The wiring diagram is OK (i.e light sensor to AIN0 and temperature to AIN2) but the code is not quite working.  Here is the corrected code:
import time
import pyupm_grove as grove

light = grove.GroveLight(1)
temp = grove.GroveTemp(3)

while True:
 print light.name() + " raw value is %d" % light.raw_value() + ", which is roughly %d" % light.value() + " lux"
 celsius = temp.value()
 fahrenheit = celsius * 9.0/5.0 + 32.0
 print "%d degrees Celsius, or %d degrees Fahrenheit" % (celsius, fahrenheit)
 time.sleep(2)

What about those two Grove connectors on the BBGW?  One is for UART, the other is I2C, both support GPIO.  Here is an example Python script which uses the UART connector w/a button:
import mraa
import time

x = mraa.Gpio(68)
x.dir(mraa.DIR_IN)

while True:
 time.sleep(1)
 if x.read() > 0:
  print 'button press'
 else:
  print 'button not press'


Thursday, December 22, 2016

AWS IoT and BeagleBone Green Wireless

This post demonstrates how to configure a BeagleBone Green Wireless for Amazon Web Services "Internet of Things" using the AWS CLI.

The ingredients:
  • AWS Account
  • BeagleBone Green Wireless (BBGW)
    • Working WiFi
    • Flashed w/image bone-debian-8.6-seeed-iot-armhf-2016-11-06-4gb.img
    • apt-get update
    • apt-get upgrade
Install AWS CLI on BBGW.  Perform the following steps:
  • Create a IAM user for the BeagleBone
    • download the access key ID/secret access key
  • Install AWS CLI
    • pip install awscli
  • Configure AWS CLI (using the access key ID/secret access key from above)
    • aws configure
  • Verify AWS CLI installation by performing simple command
    • aws s3 ls (s3 directories are returned)
Generate security certificates:
  • mkdir aws_certs
  • cd aws_certs
  • openssl genrsa -out privateKey.pem 2048
  • openssl req -new -key privateKey.pem -out cert.csr
  • aws iot create-certificate-from-csr --certificate-signing-request file://cert.csr --set-as-active > certOutput.txt
  • grep certificateId certOutput.txt
  • aws iot describe-certificate --certificate-id <certificateId from last step> --output text --query certificateDescription.certificatePem > cert.pem
Create a policy and attach generated certificate:
  • Create policy document (policy.doc)
    {
    "Version": "2012-10-17",
    "Statement": [{
    "Effect": "Allow",
    "Action":["iot:*"],
    "Resource": ["*"]
    }]
    }
  • aws iot create-policy --policy-name PubSubToAnyTopic --policy-document file://policy.doc
  • grep certificateArn certOutput.txt
  • aws iot attach-principal-policy --principal <certifcateArn from last step> --policy-name "PubSubToAnyTopic" 
At this point, you should be able to see the certificates/policy in the AWS console.  From the AWS IoT page, select "certificates" or "policies".
Alternatively, ask for certificates using AWS CLI:
  • aws iot list-certificates
Register your BeagleBone (thing) and attach to Principal (policy):
  • aws iot create-thing --thing-name bbgw
  • aws iot attach-thing-principal --thing-name bbgw --principal  <certifcateArn from previous step>
At this point you should be able to see the thing in the AWS console.  From the AWS IoT page, select "Registry/Things"
Alternatively, ask for things using AWS CLI:
  • aws iot list-things
Update BBGW status:
  • aws iot update-thing --thing-name bbgw --attribute-payload attributes={key1=value1}
Verify update:
  • aws iot list-things