Lab 4


Sending email from Python

Do recipe 7.16. Download the code using
$ wget http://reed.cs.depaul.edu/lperkovic/csc299/lab3/sendmail.py
This command should be typed from within the directory in which you want to download the program.

Make sure to modify the values of GMAIL_USER and GMAIL_PASS appropriately. Please insure that the password of the gmail user is kept confidential.

For more info on the smtplib Python Standard Library module see
https://docs.python.org/3/library/smtplib.html
https://docs.python.org/3/library/smtplib.html#smtp-example ***
Exercise: When done, modify program pir.py from recipe 12.9 (and rename to pir_email.py) so that a short email is sent to you every time motion is detected.


Using the Raspberry Pi camera

Do recipe 1.14. Just test the commands raspistill and raspivid.

After taking a picture and a short video from the Linux shell, make sure that the camera Python API is installed
$ sudo apt-get install python3-picamera
$ sudo apt-get install python-picamera
For more info, see
http://picamera.readthedocs.org/en/release-1.12/install.html#raspbian-installation ***
http://picamera.readthedocs.org/en/release-1.12/quickstart.html ***
Download the Python program that illustrates how photos and videos are made from within Python:
$ wget http://reed.cs.depaul.edu/lperkovic/csc299/lab3/test_camera.py


Intercepting keypresses and mouse movements

Do recipes 12.11 and 12.12 (using the "Pygame" approach.)


Overview: Reading sensor input on the Pi

The Pi does not have analog I/O pins that can read an arbitrary voltage in a given range (e.g., 0V - 3.3V). Many sensors, however, output their sensor reading as a voltage in a certain range. For such sensors, it is necessary to use Analog-to-Digital Converter (ADC) hardware. A cheaper and more basic solution, using a capacitor and a couple of resistors, is also possible for a class of analog sensors called resistive sensors. We will see how to do that first.

Raspberry Pi Cookbook

Using resistive sensors

Do recipe 13.0, 13.1.

The resistive sensor in this recipe is a trimmer potentiometer (or trimpot). The trimpot has a dial that can be rotated within a certain (angular) range. In this recipe you will will build a circuit and use a Python program that illustrates how the trimpot dial position can be detected in a Python program.

A trimpot happens to be a variable resistor and the amount of resistance it provides depends on the dial position. So, to obtain the position of the trimpot dial, it is sufficient to measure its resistance. For more info about trimpots see
http://www.robotroom.com/Trimpots.html ***
So the goal of this recipe is to measure the resistance of the trimpot. This recipe uses capacitors. You can think of a capacitor as a rechargeable battery. It has two metal plates separated by an insulating material. When tied to a circuit, the plate on the positive side stores a positive charge and the plate on the negative side stores a negative charge. No current passes between the plates though. The amount of charge stored on the plates is the capacity of the capacitor. For more info about capacitors see
https://learn.sparkfun.com/tutorials/capacitors ***
The circuit you build in this recipe is shown below:
(from the textbook)
The purpose of this circuit is to measure the resistance at the variable resistor Rt. When pin A is set to output high (3.3V) and pin B is set to be an input pin, then the capacitor will charge. The speed at which it will charge will depend on the amount of current between pin A and the capacitor which has to go through variable resistor Rt. When the resistance of the variable resistor Rt is decreased, the amount of current is increased and the capacitor will fill up more quickly; when the resistance is increased, the amount of current is decreased and the capacitor will fill up more more slowly. By keeping track of the time it takes for the capacitor to fill we can effectively "read out" the resistance at the variable resistor Rt and, therefore, the position of the trimpot dial.
 
In order for all this to work properly, the capacitor must initially be empty which can be done by discharging the positive voltage (on the upper plate in the above image). This is done by setting pin B to output low (0V) and pin A to an input pin; the positive charge will then flow from the capacitor to pin B.


Measuring light intensity

Raspberry Pi Cookbook Do recipe 13.2. This is is really the same lab exercise as the previous recipe except that a photoresistor is used instead of a trimpot.

A photoresistor is a variable resistor whose resistance depends on the amount of ambient light. More ambient light decreases the resistance (down to several KOhms in very bright light) and less ambient light increases the resistance (up to several hundreds KOhms in complete darkness). For more info about photoresistors see
https://learn.adafruit.com/photocells/  ***


Measuring Temperature with a Thermistor

Do recipe 13.3.

Raspberry Pi Cookbook

Measuring voltage

As we said before, the Pi GPIO pins can't handle analog input. In other words, they cannot sense the whole range of voltages between 0V and 3.3V; they can only sense low (from 0 to 1.65V) and high (from 1.65V to 3.3V) voltage.

Many sensors output analog voltages. To read the output from these sensors, we need to use an Analog-to-Digital Converter chip. The chip we will use is the MCP3008 eight-channel ADC chip. For more info on this chip, including the roles played by its 16 pins and an explanation of the wiring between the chip and the Pi, see
https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/connecting-the-cobbler-to-a-mcp3008  ***
The communication between the MCP3008 chip and the Pi is via the Serial Peripheral Interface, a full-duplex, synchronous communication protocol. For more info, read
https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi  ***
Do recipe 9.5 to get the Serial Peripheral Interface (SPI) enabled if you have not done so already. Then do recipe 13.5. In the code, you will find function
import spidev, time

spi = spidev.SpiDev()
spi
.open(0, 0)

Raspberry Pi Cookbook
def
analog_read(channel):

    r = spi.xfer2([1, (8 + channel) << 4, 0])
    adc_out = ((r[1]&3) << 8) + r[2]
    return adc_out
Raspberry Pi Cookbook Object spi is of type SpiDev defined in module spidev, Python's SPI API. Method xfer2() initiates a SPI transaction. It takes as input a list of 3 bytes (which are sent, bit by bit, to the MCP3008 chip) and returns a list of 3 bytes (which are synchronously received, bit by bit, from the MCP 3008 chip). The following illustrates the sending and receiving of these 48 bits:

Of the 24 bits sent to the chip, only the bits 8-12 are relevant. Bits 8 and 9 contains 1 and bits 10, 11 and 12 (D2, D1, and D0) contain the channel number (0 through 7) in binary notation (000 through 111). In the example above, list [1, (8 + channel) << 4, 0] represents bits (signals)
00000001 10000000 00000000
which requests the voltage at channel 0 (binary 000).

Of the 24 bits received back (list of 3 bytes r in the above code), only the last 10 (bits B9, B8, B7, ..., B1, B0 in the above image) contain relevant information, i.e. the voltage in 10-bit binary notation. These bits are the last two bits in byte r[1] and all 8 bits in byte r[2]. Together, these bits defined a 10-bit binary number whose decimal value is ((r[1]&3) << 8) + r[2]. Raspberry Pi Cookbook

Documentation for the Python spidev module is at
http://tightdev.net/SpiDev_Doc.pdf
For a more in-depth overview of SPI, see
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
as well as
http://www.byteparadigm.com/applications/introduction-to-i2c-and-spi-protocols/
This last reference also discusses I2C, an alternative serial communication protocol. Finally, here is the all important MCP3008 datasheet:
http://www.adafruit.com/datasheets/MCP3008.pdf
Recipe 13.6 shows how to read sensors that output a voltage outside the ususal 0 - 3.3V range.


Using resistive sensors with an ADC

Do recipe 13.7. This uses a similar setup as recipe 12.4.


Measuring temperature with an ADC

Do recipe 13.8. This uses a similar setup as recipe 12.4.


Measuring temperature with a digital sensor

Do recipe 13.11. Note: the packet containing the sensor includes a 4.7kOhm resistor.

Raspberry Pi Cookbook

Homework

Re-read the recipes in the textbook we have covered in Lab 4. Also go through the starred (***) online tutorials and online documentation covered in Lab 4.