Embedded Systems report(Raspberry PI)

DXARTS 490: Embedded Systems report.
http://wiki.roberttwomey.com/Raspberry_Pi

1) Audio Routing

A really easy way to switch audio output routes while logged into pi@raspberrypi is to run a

sudo amixer cset numid=3 1

#where 0 is automatic
#1 is 3.5mm jack
#2 is HDMI

2) Load VNCserver on boot/reboot

establish VNCserver connection automatically on boot so that you can run VNCViewer immediately:

(instructions from http://www.penguintutor.com/linux/tightvnc)

#first download text file from this address and move it into the proper ‘init.d’ folder
wget http://www.penguintutor.com/otherfiles/tightvncserver-init.txt
sudo mv tightvncserver-init.txt /etc/init.d/tightvncserver

#chmod execute 7-read write execute for use, 5 read execute for group, 5-read execute for world.  this executes the file name /etc/init.d/tightvncserver

sudo chmod 755 /etc/init.d/tightvncserver

#add the script to default run levels with this command
sudo update-rc.d tightvncserver defaults

3) Load any file on boot/reboot

#enter text edit with nano command  (alter ‘testfile’ as you wish)

sudo nano /etc/init.d/testfile

#enter text here; example runs a pd patch entitled “testpatch” on boot, alter script and name of file accordingly, exit, save and now you have a file created in the /etc/init.d/ folder:

  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          runs PD patch ‘testpatch’ on startup
  4. # Required-Start:    $local_fs
  5. # Required-Stop:     $local_fs
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: Start/stop a sample puredata patch
  9. ### END INIT INFO
  10. # Set the USER variable to the name of the user to start pd under
  11. export USER=’Pi’
  12. ### End customization required
  13.  eval cd ~$USER
  14. case “$1” in
  15.   start)
  16.     su $USER -c ‘pd -nogui -noadc -alsa testPatch.pd’
  17.     echo “Starting testPatch for $USER “
  18.     ;;
  19.   stop)
  20.     ps kill-9 ‘pd -nogui -noadc -alsa testPatch.pd’
  21.     echo “testPatch stopped”
  22.     ;;
  23.   *)
  24.     echo “Usage: /etc/init.d/testpatch {start|stop}”
  25.     exit 1
  26.     ;;
  27. esac
  28. exit 0

#make your script executable
sudo chmod 755 /etc/init.d/testfile

#test running script
sudo /etc/init.d/testfile start
sudo /etc/init.d/testfile stop

#register to make it run
sudo update-rc.d testfile defaults

#TO REMOVE SCRIPT
sudo update-rc.d -f testfile remove

#TO KILL THE RUN OF ANY SCRIPT
$ ps ux
$ kill-9 ‘4 digit PID number’

4) Working with I2C

for this example the sensor used is a digital luminosity sensor TSL2561 from Adafruit.
The TSL2561 takes measurements of luminosity across IR spectrums and visible light spectrums to optimize ambient light data harvest.  It can be used to monitor ambient light conditions for use in responsive LCD/LED monitors, More information can be found here: http://www.adafruit.com/products/439
http://www.adafruit.com/datasheets/TSL2561.pdf

there is also a tutorial for use with Arduino, and a tutorial that will allow you to get Lux readings out of the box.  But for our purposes here’s a quick introduction to get the TS2561 working with the Raspberry/Python to get ambient light sensor readings

  • Follow the instructions for configuring GPIO and I2C on the Raspberry
    http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
  • Connect the hardware.
    TSL2561: VCC —> 3.3V: RPi
    TSL2561: SDA –> 2 SDA: RPi
    TSL2561 SCL –> 3 SCL: RPi
    TSL2561 GND –> GND: RPi
    #note: the ADDR pin can be connected to GND to add a new address 0x29 for lining another TSL2561 :
    http://learn.adafruit.com/tsl2561/wiring
  • check to see if the sensor is in place with
    $ sudo i2cdetect -y 1
    If one TSL2561 is connected, it should show up in the 0X39 address
  • Open a python editor (i used Komodo Edit 8) **editors minimize problems with scripting
  • Enter this code (adapted from http://forums.adafruit.com/viewtopic.php?f=8&t=34922):
    1. #!/usr/bin/python
    2. import sys
    3. import smbus
    4. import time
    5. from Adafruit_I2C import Adafruit_I2C
    6. from time import sleep
    7. #this code has been adapted for Python 2 that converts the raw values from the TSL2561 to Lux units.
    8. #Math and code adapted from discussions on http://forums.adafruit.com/viewtopic.php?f=8&t=34922
    9. class Luxmeter:
    10. i2c = None
    11. def __init__(self, address=0x39, debug=0, pause=0.8):
    12. self.i2c = Adafruit_I2C(address)
    13. self.address = address
    14. self.pause = pause
    15. self.debug = debug
    16. self.gain = 0 # no gain preselected
    17. self.i2c.write8(0x80, 0x03) # enable the device
    18. def setGain(self,gain=1):
    19. “”” Set the gain “””
    20. if (gain != self.gain):
    21. if (gain==1):
    22. self.i2c.write8(0x81, 0x02) # set gain = 1X and timing = 402 mSec
    23. if (self.debug):
    24. print “Setting low gain”
    25. else:
    26. self.i2c.write8(0x81, 0x12) # set gain = 16X and timing = 402 mSec
    27. if (self.debug):
    28. print “Setting high gain”
    29. self.gain=gain; # safe gain for calculation
    30. time.sleep(self.pause) # pause for integration (self.pause must be bigger than integration time)
    31. def readWord(self, reg):
    32. “””Reads a word from the I2C device”””
    33. try:
    34. wordval = self.i2c.readU16(reg)
    35. newval = self.i2c.reverseByteOrder(wordval)
    36. if (self.debug):
    37. print(“I2C: Device 0x%02X returned 0x%04X from reg 0x%02X” % (self.address, wordval & 0xFFFF, reg))
    38. return newval
    39. except IOError:
    40. print(“Error accessing 0x%02X: Check your I2C address” % self.address)
    41. return -1
    42. def readFull(self, reg=0x8C):
    43. “””Reads visible+IR diode from the I2C device”””
    44. return self.readWord(reg);
    45. def readIR(self, reg=0x8E):
    46. “””Reads IR only diode from the I2C device”””
    47. return self.readWord(reg);
    48. def getLux(self, gain = 0):
    49. “””Grabs a lux reading either with autoranging (gain=0) or with a specified gain (1, 16)”””
    50. if (gain == 1 or gain == 16):
    51. self.setGain(gain) # low/highGain
    52. ambient = self.readFull()
    53. IR = self.readIR()
    54. elif (gain==0): # auto gain
    55. self.setGain(16) # first try highGain
    56. ambient = self.readFull()
    57. if (ambient < 65535):
    58. IR = self.readIR()
    59. if (ambient >= 65535 or IR >= 65535): # value(s) exeed(s) datarange
    60. self.setGain(1) # set lowGain
    61. ambient = self.readFull()
    62. IR = self.readIR()
    63. if (self.gain==1):
    64. ambient *= 16 # scale 1x to 16x
    65. IR *= 16 # scale 1x to 16x
    66. ratio = (IR / float(ambient)) # changed to make it run under python 2
    67. if (self.debug):
    68. print “IR Result”, IR
    69. print “Ambient Result”, ambient
    70. if ((ratio >= 0) & (ratio <= 0.52)):
    71. lux = (0.0315 * ambient) – (0.0593 * ambient * (ratio**1.4))
    72. elif (ratio <= 0.65):
    73. lux = (0.0229 * ambient) – (0.0291 * IR)
    74. elif (ratio <= 0.80):
    75. lux = (0.0157 * ambient) – (0.018 * IR)
    76. elif (ratio <= 1.3):
    77. lux = (0.00338 * ambient) – (0.0026 * IR)
    78. elif (ratio > 1.3):
    79. lux = 0
    80. return lux
    81. oLuxmeter=Luxmeter()
    82. while(true):
    83. print “LUX HIGH GAIN “, oLuxmeter.getLux(16),
    84. print “LUX LOW GAIN “, oLuxmeter.getLux(1),
    85. print “LUX AUTO GAIN “, oLuxmeter.getLux(), #!/usr/bin/env python
    86. sleep(3);
    87. #take 3 recordings at 3 second intervals
  • save (luxsensor.py) and use the scp command to move to folder on the Raspberry Pi
  • ideally save this with the rest of the Adafruit tutorials, but if not, you will need to create a shortcut to the file Adafruit_I2C in the same folder as the luxsensor.py file
  • on the Raspberry IDE, open root terminal and type
    sudo idle
    #this opens python 2 on your machine as a root user to allow you to change I2C/GPIO settings
  • Open luxsensor.py and run – you should begin getting lux sensor recordings (3 values) every 3 seconds at address 0x39

From here, you can adapt code to draw graphs, add to an online repository (Xively.com) or control other responsive systems and appendages.

Created: Jun 8 2013

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: