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.
You can try this on your own computer or try logging into uncacsci-pi3-a.cs.unca.edu .
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()
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 devices are magic. You have many of them in your car. It’s too complicated. Look it up on YouTube.
Always look for example code!
Here are links to three web CGI scripts that produce output in JSON.
http://uncacsci-pi3-k.cs.unca.edu/cgi-bin/getuuid.py
http://uncacsci-pi3-k.cs.unca.edu/cgi-bin/acceleration.py
http://uncacsci-pi3-k.cs.unca.edu/cgi-bin/pressure.py
We’ll try to get you writing a CGI script before the end of the semester.