Wednesday, December 28, 2016

Temperature Sensing w/BeagleBone Black and Adafruit PT100 RTD

Here is a simple example of collecting temperature w/BeagleBone Black (BBB) and the Adafruit PT100 RTD temperature sensor.  The PT100 sensor resistance varies directly w/temperature.  I can measure voltage drop across the PT100 sensor using an analog to digital converter (ADC) on the BBB and discover the temperature.

The BBB offers eight 12 bit ADC inputs.  ADC values range from 0 to 4096 (2^12).  Reference voltage is 1.8VDC.  Schematic below indicates Vref is on P9/pin 32, and the ADC input is P9/pin 39.  ADC ground (P9/pin 34) is tied to P9/pin 1 ground.  


My use case ranges from 0C to an upper bound of 400C.  The temperature sensor offers a resistance of 100 ohms at 0C and then .385 ohm per degree C or (in this case) a maximum  resistance of 100 + 400 * 0.385 = 254 ohms.


Given that Vref = 1.8 VDC and Rmin = 100 ohms, the current at minimum resistance is 18mA (this is a problem).  To mitigate loading, I added a LM358 op-amp as a voltage follower.  Put another way, simply reading the PT100 voltage drop might damage the BBB so I decide for additional circuitry as insurance.  


I want to use the temperature sensor as half of a voltage divider.  What value for the other half?  Old school teaches that R = sqrt(Rmin * Rmax) = sqrt(100 * 254) = 160 ohms.  Amazingly, I had a 160 ohm resistor available.

Schematic of temperature sensor demonstration

My PT100 came w/three wires ending w/a blue and two red terminals.  I only needed the blue and one of the red terminals.

To enable analog inputs requires some configuration.  This example uses Debian 7 image (bone-debian-7.11-lxde-4gb-armhf-2016-06-15-4gb.img).


root@beaglebone:~# echo $SLOTS
/sys/devices/bone_capemgr.9/slots
root@beaglebone:~# cat $SLOTS
 0: 54:PF--- 
 1: 55:PF--- 
 2: 56:PF--- 
 3: 57:PF--- 
 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
 6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
root@beaglebone:~# echo "BB-ADC" > $SLOTS
root@beaglebone:~# cat $SLOTS
 0: 54:PF--- 
 1: 55:PF--- 
 2: 56:PF--- 
 3: 57:PF--- 
 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
 5: ff:P-O-- Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
 6: ff:P-O-- Bone-Black-HDMIN,00A0,Texas Instrument,BB-BONELT-HDMIN
 7: ff:P-O-L Override Board Name,00A0,Override Manuf,BB-ADC

To read an ADC sample:
root@beaglebone:~# cd /sys/bus/iio/devices/iio:device0
root@beaglebone:/sys/bus/iio/devices/iio:device0# ls
dev              in_voltage2_raw  in_voltage5_raw  name       uevent
in_voltage0_raw  in_voltage3_raw  in_voltage6_raw  power
in_voltage1_raw  in_voltage4_raw  in_voltage7_raw  subsystem
root@beaglebone:/sys/bus/iio/devices/iio:device0# cat in_voltage0_raw
2441

For verification, I placed the PT100 in a pan of water and heated it to boiling while reading BBB ADC (voltage) values every 5 degree C (from cooking thermometer), which yields this graph (circles are samples, line is regression model).

Graph illustrates temperature change is linear (and suggests a conversion model).  

To convert ADC to temperature use: 822 - 0.329512(ADC value)

Table (below) illustrates observed vs calculated temperatures.  Real world rarely match calculated world, but these values seem close enough for my intended application.


ADCObserved CCalculated C
2479105.14
24371518.98
24242023.26
24192524.91
24033030.18
23893534.80
23784038.42
23584545.01
23475048.64
23325553.58
23186058.19
22996564.45
22847069.39
22677575.00
22518080.27
22358585.54
22189091.14
22089594.44
219510098.72


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

Sunday, December 18, 2016

Infrared "break beam" detection using BeagleBone Black and Adafruit 2167

Here is a simple example of monitoring a infrared (IR) light beam w/BeagleBone Black.  There are many use cases for IR break beam sensors and for US $1.95 the Adafruit 2167 is an excellent choice.

The example provided by Adafruit is for the Arduino.  Here is a simple example using the BBB.

Schematic of "break beam" demo.
  Any GPIO will do.  Below is a runtime example from a BBB using Debian 7 (wheezy).  Note that "value" contains a 1 when the IR beam is detected and a 0 when I block the beam.

root@beaglebone:~# cd /sys/class/gpio
root@beaglebone:/sys/class/gpio# echo 115 > export
root@beaglebone:/sys/class/gpio# cd gpio115
root@beaglebone:/sys/class/gpio/gpio115# echo in > direction
root@beaglebone:/sys/class/gpio/gpio115# cat value
1
root@beaglebone:/sys/class/gpio/gpio115# cat value
0

Sunday, April 24, 2016

AWS API Gateway and Lamba (Python) Demonstration

I have a customer interested in AWS API Gateway and Lambda (Python), so I created a small demonstration application to explore this option.

The resulting application is a simple 4 function calculator web service.  There is significant overlap between this demo and the LambdaGate example provided by AWS.  This application is different because:
  • Employs Cloud Formation as much as possible
  • Python (instead of node.js) Lambda function