Contact Form

Name

Email *

Message *

Search This Blog

Total Pageviews

Popular Posts

Translate

Sunday, July 12, 2015

Pre-SVFIG talk: 3 Computers (Surface, ZOTAC, Raspberry Pi w/ Weaved WebIOPi) on 1 screen

LED-RaspberryPi3
Starting out here: https://code.google.com/p/webiopi/wiki/Tutorial_Basis, I've already had to retype this line, because my Pi died, so once again, save often and prepare to reset when things go south. I also figured out how to use DopBox here: http://raspi.tv/2013/how-to-use-dropbox-with-raspberry-pi.
In [16]:
import webiopi
import datetime
from IPython.core.display import HTML

GPIO = webiopi.GPIO
Unlike the sample code, I have a dual 7 segment LED display from the OSEPP™ 101 Arduino Basics Starter Kit @ http://osepp.com/products/kits/101-arduino-basic-starter-kit/. So, there's going to be 16 pins to describe and wire up. I have a picture of how I wired it up to the Pi, so let's see if that works here:

or see https://dl.dropboxusercontent.com/u/49100658/LED-RaspberryPi.png.
Had to split this cell, because the Download as PDF via LaTeX couldn't wrap the page overflow.
The "BCM numbering" reference, mentioned above, was a new reference to me, so I had to look it up here: http://raspberrypi.stackexchange.com/questions/12966/what-is-the-difference-between-board-and-bcm-for-gpio-pin-numbering. I guess that confirms that I have Raspberry Pi 1 model B revision 2.
Note that, at the moment, the Right LED is mostly not working, due to my poor soldering skills, but the Left one appears to be working fine. Maybe, since solder appears to "age", getting better or worse over time. Need to go to a GPIO expander, like https://www.sparkfun.com/products/8130 or https://learn.adafruit.com/mcp230xx-gpio-expander-on-the-raspberry-pi/overview since that chip is supported by https://code.google.com/p/webiopi/wiki/DIGITAL. I'm pretty sure that I still need a couple of Model 4116R-1-RC (8 Isolated Resistors) dips. The only question now is when I should switch.
In [2]:
# Left LED GPIO pin using BCM numbering
LeftPin = [2,3,4,7,8,9,10,11]
def Left (n):
    return LeftPin[n]
# GEN pin using "2nd func" @ https://en.wikipedia.org/wiki/Raspberry_Pi
Gen  = [17,18,27,22,23,24,25]
# Right LED using GEN pin numbering
def Right (n):
    return Gen[n]
# There are no more pins for the Right DP LED, so it's "extra"
Now, I need to define numbers as arrays of 7 "segments".
In [3]:
# Order a,b,c,d,e,f,g
Num0 = [1,1,1,1,1,1,0]
Num1 = [1,1,0,0,0,0,0]
Num2 = [1,1,0,1,1,0,1]
Num3 = [1,1,1,1,0,0,1]
Num4 = [0,1,1,0,0,1,1]
Num5 = [1,0,1,1,0,1,1]
Num6 = [1,0,1,1,1,1,1]
Num7 = [1,1,1,0,0,0,0]
Num8 = [1,1,1,1,1,1,1]
Num9 = [1,1,1,0,0,1,1]

Nums = [Num0,Num1,Num2,Num3,Num4,Num5,Num6,Num7,Num8,Num9]
Getting back to the Tutorial, we have to setup the LED's GPIOs. I'll use the left DP as the LIGHT and just turn it on here. I've started to add the initialization of all the other GPIOs, but I'm not running this update yet.
In [4]:
# setup function is automatically called at WebIOPi startup
def setup():
    # set the GPIO used by the 2 7 segment LEDS to output
    for x in LeftPin:
        GPIO.setFunction(Left(x), GPIO.OUT)
    for x in Gen:
        GPIO.setFunction(Left(x), GPIO.OUT)

    # retrieve current datetime
    now = datetime.datetime.now()

    # test the pin by turning it on
    GPIO.digitalWrite(Left(7), GPIO.HIGH)
In the loop function, again, I will start with minimal changes.
In [5]:
# loop function is repeatedly called by WebIOPi 
def loop():
    # retrieve current datetime
    now = datetime.datetime.now()

    # toggle light ON all days at the correct time
    #if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
    if ((now.second & 1) == 0):
        if (GPIO.digitalRead(Left(7)) == GPIO.LOW):
            GPIO.digitalWrite(Left(7), GPIO.HIGH)

    # toggle light OFF
    #if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
    if ((now.second & 1) == 1):
        if (GPIO.digitalRead(Left(7)) == GPIO.HIGH):
            GPIO.digitalWrite(Left(7), GPIO.LOW)

    # gives CPU some time before looping again
    webiopi.sleep(1)
Finally, we need to "destroy" the device, which just seems wrong, but here we go.
In [6]:
# destroy function is called at WebIOPi shutdown
def destroy():
    GPIO.digitalWrite(Left(7), GPIO.LOW)
By following the rest of the tutorial, I was able to get the left DP LED blinking, reliably until I had to just reset it and it appears that it did crash, or something...
I'm making these notes on the PC side, cause ipython is a little easier to use in Windows. However, due to the DropBox backup I'm using, this file appears to be write protected, so I had to rename it. Not a good solution yet.