Assignments, Raspberry Pi

Brief intro. & assignment #1

For the purpose of a brief introduction I can tell you the following: I am a senior-year Systems Engineering student at Universidad El Bosque in Bogotá, Colombia. Currently, I am a visiting Computer Engineering student at Villanova University for the 2016 Spring term, taking several courses (graduate and undergraduate) from different colleges and departments. One of these courses is called Internet of Things, which is the primary reason I started this blog. My posts here will be a way for me to register my progress and learning experiences throughout the course, as well as an opportunity for me to share my thoughts and ideas.

Now that that’s out of the way…

IoT Kits

For this first assignment, we had to acquire the following IoT Kits:

Each kit is very different from the other. On the first hand, the Raspberry Pi 2 Model B is a single board computer which, as a regular desktop computer, is able to perform several tasks at any given time, as well as serving multiple purposes (desktop computer, server, controlling some other hardware/software, etc.). Even more interestingly, the Raspberry Pi 2 has 40 General Purpose Input/Output (GPIO for short) pins that are fully programmable via software and available to the user. On the other hand the ESP8266 is a microcontroller which is a very small computer that can be used to perform diverse but limited tasks that serve a single purpose (measure and report temperature, react to certain conditions, elc.). What is particular to this small piece of hardware is that it has built-in WiFi capability! So it is a perfect piece of equipment for projects delving into the universe of internet-connected devices.

On the following pictures you can observe what’s included in the Vilros kit (the HDMI cable and power adapter are not shown). Aditional to it, I bought a 2.4GHz wireless keyboard with integrated trackpad to control the Raspberry Pi and an HDMI-to-VGA adapter in order to connect it to my LCD monitor.

On the following set pictures you can observe that’s included in the OddWires kit, but I will not describe it’s contents because they are not relevant for this first assignment. This first post will revolve entirely on the Raspberry Pi.

NOOBS & Raspbian

After obtaining the kits, I proceeded to install Raspbian into the Raspberry Pi. Raspbian is a flavor of Linux (Debian) specifically designed to run on Raspberry Pis. Being an experienced Linux, user I am no stranger to system installs, but even the most inexperienced user will find it is a breeze to install Raspbian using the microSD Card that came pre-loaded with NOOBS: it just works out of the box.

Having some previous experience with Raspberry Pis (I built a media center using one not long ago), and having installed both heatsinks onto the main board (CPU and I/O controller) I overclocked the CPU to 950MHz to amp the little guy’s performance a little bit.

Chapters 6 & 7

After having this tiny linux box up and running, I proceeded to read Chapters 6 and 7 from the Vilros User’s Guide which treated an introduction to python programming and a tutorial on GIPO Input/Output respectively.
2016-01-25 11.48.37

Chapter 6

Regarding Chapter 6 I don’t have much to report: I followed the tutorials which refreshed my memory on python basics.

[sourcecode language=”python” wraplines=”false” collapse=”false”]
#Python3 Conventions

#Class Definition
class MyClass:

#Init Functions (Constructors, within a class)

_init_ (args …)

#Inheritante
#NewClass inherits from MyClass
class NewClass (MyClass)

#Private variables and methods (only accesible to its containing class)
–varname=…
–methodName():

[/sourcecode]

Chapter 7

Chapter 7’s two main activities are using the GPIO as outputs and reading inputs from the GPIO.

GPIO Outputs

For the first experiment the following circuit was to be assembled:20160124_204449I managed to get the circuit right the seccond time around. The first time I had unintentionally bypassed the resistor and subsequently burned the LED (oops! A typical example of relearning by breaking things). After propperly assembling the circuit, it worked as intended:20160124_192312Having powered a LED with Pins 1 (3.3V) and 6 (GND), it was time to control the LED via software plugging it to Pins 2 (GPIO2) and 6 (GND). First this was done via the command line, ‘exporting’ the GPIO controls and modifying the files that controll the state of a given GPIO pin:

20160124_193212

0 for OFF.

20160124_193537

Before moving forward with the tutorial, the GPIO controls where ‘unexported’.

The GPIO pins can also be programmed using Python. To demonstrate this a small python script was written and executed with administrator privileges:

[sourcecode language=”python” wraplines=”false” collapse=”false”]
import RPi.GPIO
import time

#NOTE: GPIO(n) = PIN(n+1)
#Sets BCM mode for GPIO(n) referencing
RPi.GPIO.setmode(RPi.GPIO.BCM)
#Configure GPIO2 (PIN3) as an OUTPUT
RPi.GPIO.setup(2, RPi.GPIO.OUT)
#Blink LED
while true:
RPi.GPIO.output(2, True)
time.sleep(1)
RPi.GPIO.output(2, False)
time.sleep(1)
[/sourcecode]

The script made the LED turn on and off with a one second delay in between:
20160124_195015

GPIO Inputs

For this experiment the following circuit was to be assembled:20160124_204502So I did:20160124_201006

And just as in the previous experiment, initially the capture of the input caused by the switch was caught via the command line using files that control and register the state of a given GPIO pin:

20160124_201844

It works!

After capturing the input with the command line, a python script was written and executed with administrator privileges:

[sourcecode language=”python” wraplines=”false” collapse=”false”]
import RPi.GPIO

#NOTE: GPIO(n) = PIN(n+1)
#Sets BCM mode for GPIO(n) referencing
RPi.GPIO.setmode(RPi.GPIO.BCM)
#Configure GPIO2 (PIN3) as an INPUT using a pull-up resistor
RPi.GPIO.setup(2, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)
#Read the pressing of the Switch
while true:
if RPi.GPIO.input(2) == RPi.GPIO.LOW:
print("Switch pressed.")
break
#Release the GPIO pins
RPi.GPIO.cleanup()

[/sourcecode]

The script executed an infinite loop waiting for the signal coming from GPIO2, and as soon as the signal was detected (the switch was pressed) the loop was interrupted and the process ended after ‘cleaning up’ (‘unexporting’) the GPIO controls:

20160124_203144

Eureka!

Conclusions:

I was able to obtain the required materials for the assignment, install and configure Raspbian on the Raspberry Pi and succesfully follow the tutorials from the Vilros user’s guide booklet. I had lots of fun doing so, and even though I have had experience with Python, Linux embedded systems and Raspberry Pis, I had never done anything quite like these activities.

I really look forward to getting my hands dirty with not only the Raspberry Pi, but the ESP8266 as well. See you next week!

-J4D!

Disclaimer: English is not my first language, though I do handle it well. Please excuse any typos and grammatical errors you might find. If you’re kind enough to point them out, I’ll make sure to correct them. 
Thanks!

1 Comment

  1. German Campos

    English isn´t my mother tongue as well, but is fair enough..! Hehehe… no, que va!! Que buen trabajo Jorge… mucho por aprenderle, el problema será el cuando… pero mientras siga haciendo semejantes vitácoras de avance, me doy por bien servido, jeje! Un abrazo!

Leave a Reply