Lab 1

Objective

The purposes of this lab are to:

  • Learn to enumerate requirements from use cases and user stories
  • Learn to utilize the general purpose input/output (GPIO) pins on the Raspberry Pi to control LEDs.
  • Learn to develop a minimum viable product, and deliver new functionality through subsequent updates until the requirements are met, and all of the deliverables are acceptable.
  • Learn to use Github for progressive code commits and version tracking.

 

Materials

The items used to complete this lab were:

  • Raspberry Pi 3
  • Sparkfun Pi Wedge and ribbon cable
  • Red LED
  • Yellow LED
  • Green LED
  • Three 330 Ohm resistors
  • Assorted male-male jumper wires for breadboard
  • Breadboard

 

References

I used the following references in this lab. All of them concern smaller problems I was having with remembering specific syntax or how certain functions work.

Procedures

  • Connect the Sparkfun Pi Wedge to the Raspberry PI pins using the provided ribbon cable, and mount the T-shaped portion to the breadboard, straddling the break in the middle of the breadboard which indicates a break in the circuit of the board.
  • Take a jumper cable and connect it from a ground (GND) output of the Pi Wedge to one of the negative rails running along the sides of the breadboard. This will send the ground along the entire row of that breadboard.
  • Connect three jumper cables to three GPIO pin outputs of your choice. In this example, we will be using G17, 16, and 13.
  • Connect the other ends of those jumper cables on rows 23, 25, and 27 of your breadboard, as close to the side as possible. These are the positive leads for your LEDs.
  • Insert the long end of each LED into a hole which is on the same row as a jumper cable from the previous step. You’ll note that there is conveniently an extra hole between each jumper cable from step 4; this is for the ground.
  • Insert one end of each resistor into the negative rail you established in step 2. Insert the other end along a row where you placed the negative leg of an LED. Once this is done, you should have a positive and negative connection for each LED.
  • Power on your RPi. It is assumed you have already installed an operating system such as Raspbian.
  • Ensure that the pins are prepared for output by running the command gpio -g mode <pin number> out for each of the pins listed in step three.
  • Install the Apache web server by issuing the command sudo apt-get install apache2.
  • Enable CGI (the protocol necessary for running our light-controlling scripts from a browser) by running the command sudo a2enmod cgi
  • Create a directory for scripts by running the command sudo mkdir /var/www/html/cgi-bin
  • Navigate to the directory just created and create script files by issuing the command sudo touch <file name>, with these file names:
    • alloff.py
    • red.py
    • green.py
    • yellow.py
    • status.py
    • stoplight.py
    • stoppython.py
  • Open /var/www/html/index.html in a file editor (sudo nano <file name>) and erase its contents, replacing them with the code for index.html found in the appendix.
  • Fill each of the files mentioned in step 11 with their respective contents from the appendix.
  • Make each script from step 11 executable by navigating to the cgi-bin directory and running the command sudo chmod +x *
  • Navigate to the web page locally on the Pi by opening up a browser to localhost/
  • Press the buttons on the screen to operate the stoplight

 

Thought Questions

  • What language did you choose for implementing this project? Why?
    • For the web front-end, I used HTML and JS (jQuery). HTML is a necessary no-brainer, but I chose jQuery because it’s straightforward to get simple scripts stood up and run asynchronous commands (such as the buttons changing their status depending on the actual status of the LED as reported from the Pi). For the back end, I chose Python because of its ability to quickly and efficiently access GPIO pins as well as send JSON back to jQuery, which Is necessary for status reporting.
  • What is the purpose of the resistor in this simple circuit? What would happen if you omitted it?
    • The resistor is there to reduce the amount of electricity reaching the LED so that it doesn’t overload it and melt (or explode). Omitting this critical piece would eventually result in failure of the LED.
  • What are practical applications of this device? What enhancements or modifications would you make?
    • Honestly, this device isn’t particularly practical in its current state. Considering the premise of the lab was that Don Borthwick was simply wanting to refresh his skills, I’d say it accomplished its task. For a real stoplight, perhaps one which could be placed outside a bathroom or other office room (hey, I’m just being imaginative), some major aesthetic changes would need to be made, and it’d make more sense to allow the light to also be controlled by hardware. Additionally, measures would need to be taken so that multiple users could (or could not) control the stoplight as appropriate.
  • Please estimate the total time you spent on this lab and report.
    • I’d say somewhere in the vicinity of three to three-and-a-half hours.

 

Certification of Work

I certify that the solution presented in this lab represents my own work. In the case where I have borrowed code or ideas from another person, I have provided a link to the author’s work in the references and included a citation in the comments of my code.

 

 

Appendix

Web Page

Code

Index.html

<!DOCTYPE HTML>

 

<html>

<head>

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>

</head>

<body>

<h1>TRAFFIC LIGHT</h1>

<button id=”redbutt” type=”button” onclick=”red()”>HELLO</button>

<button id=”yellowbutt” type=”button” onclick=”yellow()”>HELLO</button>

<button id=”greenbutt” type=”button” onclick=”green()”>HELLO</button>

<button id=”offbutt” type=”button” onclick=”off()”>ALL OFF</button>

<button id=”stopbutt” type=”button” onclick=”stoplight()”>STOPLIGHT TIME</button>

<button id=”stopstopbutt” type=”button” onclick=”stopstoplight()”>STOP STOPLIGHT TIME</button>

</body>

<script>

$(document).ready(function(){

var green = “”;

var red = “”;

var yellow = “”;

$.getJSON(‘cgi-bin/status.py’,function(data){

data.Green==”Off” ? green=”On” : green=”Off”;

data.Yellow==”Off” ? yellow=”On” : yellow=”Off”;

data.Red==”Off” ? red=”On” : red=”Off”;

$(“#greenbutt”).html(“Turn Green “+green);

$(“#yellowbutt”).html(“Turn Yellow “+yellow);

$(“#redbutt”).html(“Turn Red “+red);

});

})

 

function green(){

$.getJSON(‘cgi-bin/green.py’,function(data){

localgreen = “”;

data.Status==”Off” ? localgreen=”On” : localgreen=”Off”;

$(“#greenbutt”).html(“Turn Green “+localgreen);

});

}

 

function yellow(){

$.getJSON(‘cgi-bin/yellow.py’,function(data){

localyellow = “”;

data.Status==”Off” ? localyellow=”On” : localyellow=”Off”;

$(“#yellowbutt”).html(“Turn Yellow “+localyellow);

});

}

 

function red(){

$.getJSON(‘cgi-bin/red.py’,function(data){

localred = “”;

data.Status==”Off” ? localred=”On” : localred=”Off”;

$(“#redbutt”).html(“Turn Red “+localred);

});

}

 

function off(){

fetch(‘cgi-bin/alloff.py’);

$(“#redbutt”).html(“Turn Red On”);

$(“#yellowbutt”).html(“Turn Yellow On”);

$(“#greenbutt”).html(“Turn Green On”);

}

 

function stoplight(){

off();

fetch(‘cgi-bin/stoplight.py’);

}

 

function stopstoplight(){

fetch(‘cgi-bin/stoppython.py’);

off();

}

 

 

</script>

</html>

 

Alloff.py

#!/usr/bin/python

 

import os

print(“Content-Type: text/plain”)

print

 

os.system(“gpio -g write 17 0”)

os.system(“gpio -g write 16 0”)

os.system(“gpio -g write 13 0”)

 

Green.py

#!/usr/bin/python

 

import os,json

print(“Content-Type: application/json”)

print

response = “”

 

results = os.popen(“gpio -g read 17”).read()

 

if(results==’1\n’):

os.system(“gpio -g write 17 0”)

response={‘Status’:’Off’}

else:

os.system(“gpio -g write 17 1”)

response={‘Status’:’On’}

 

print(json.JSONEncoder().encode(response))

 

Red.py

#!/usr/bin/python

 

import os,json

print(“Content-Type: application/json”)

print

response = “”

 

results = os.popen(“gpio -g read 13”).read()

 

if(results==’1\n’):

os.system(“gpio -g write 13 0”)

response={‘Status’:’Off’}

else:

os.system(“gpio -g write 13 1”)

response={‘Status’:’On’}

 

print(json.JSONEncoder().encode(response))

 

Status.py

#!/usr/bin/python

 

import os,json

print(“Content-Type: application/json”)

print

response = “”

 

green = os.popen(“gpio -g read 17”).read().replace(‘1\n’,’On’).replace(‘0\n’,’Off’)

yellow = os.popen(“gpio -g read 16”).read().replace(‘1\n’,’On’).replace(‘0\n’,’Off’)

red = os.popen(“gpio -g read 13”).read().replace(‘1\n’,’On’).replace(‘0\n’,’Off’)

response = {‘Green’:green,’Yellow’:yellow,’Red’:red}

print(json.JSONEncoder().encode(response))

 

Stoplight.py

#!/usr/bin/python

 

import os,json,time

print(“Content-Type: application/json”)

print

response = “”

 

while(1==1):

os.system(“gpio -g write 13 1”)

time.sleep(5)

os.system(“gpio -g write 13 0”)

os.system(“gpio -g write 17 1”)

time.sleep(7)

os.system(“gpio -g write 17 0”)

os.system(“gpio -g write 16 1”)

time.sleep(2)

os.system(“gpio -g write 16 0”)

 

Stoppython.py

#!/usr/bin/python

 

import os

 

print(“content-type: text/plain\n\n”)

 

os.system(“pkill -9 -f stoplight.py”)

 

Yellow.py

#!/usr/bin/python

 

import os,json

print(“Content-Type: application/json”)

print

response = “”

 

results = os.popen(“gpio -g read 16”).read()

 

if(results==’1\n’):

os.system(“gpio -g write 16 0”)

response={‘Status’:’Off’}

else:

os.system(“gpio -g write 16 1”)

response={‘Status’:’On’}

 

print(json.JSONEncoder().encode(response))