Building on the work of others

Something to try out

This is a bit of a review to make sure you have GPIO working. Here’s a picture to help you figure out which pin is which.
Pi Cobbler pinout

You can try this on your own computer or try logging into uncacsci-pi3-a.cs.unca.edu .

Running Python directly

First take a look at Sparkfun’s Raspberry GPIO tutorial.

Start up python3 and type the following commands to test our your setup. You will need to sometimes press the button to check the input you read with GPIO.input(9). (That’s why you’ll do better using your own Pi.)

from RPi import GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(9, GPIO.IN, GPIO.PUD_UP)
GPIO.input(9)
GPIO.input(9)
GPIO.input(9)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
GPIO.output(11, GPIO.LOW)
exit()

Running Python from files

Download and save the following three files. There is a reason for the odd first line.

First create ledon.

#! /usr/bin/python3
ledpin=11
from RPi import GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.HIGH)

Next create ledoff .

#! /usr/bin/python3
ledpin=11
from RPi import GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.LOW)

Next create readbutton .

#! /usr/bin/python3
buttonpin=11
from RPi import GPIO
import json
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(9, GPIO.IN, GPIO.PUD_UP)
if GPIO.input(9)==0:
    state = 'on'
else:
    state = 'off'
print(json.dumps({'state':state}))

To make these files executable type the following commmand.

chmod a+x ledon ledoff buttonread

Because buttonread is producing JSON, it is ready for use with AJAX.

MEMS and I2C

MEMS

MEMS devices are magic. You have many of them in your car. It’s too complicated. Look it up on YouTube.

I2C the hard way

I2C the easy way

Always look for example code!

Speaking of AJAX

Here are links to three web CGI scripts that produce output in JSON.

We’ll try to get you writing a CGI script before the end of the semester.