Tuesday 5 February 2013

More Pi? First Python, Web Server, More Wifi

Getting a Web Server Running

JM again. He makes a recommendation to change the memory split between the GPU and the CPU, which I tested out before doing other things. A 24 bit HDMI screen takes about 6MB just to hold the data, so you probably need a bunch more to manage overlays and stuff. So I used raspi-config to let the GPU have 32 MB of memory.

I ran through the rest of the steps and the upgrade ran quickly this time with only a couple of packages... The rpi-update failed on it's first few connection attempts to github, but auto retried and eventually succeeded. I made no changes to the apache config and voila... I moved a copy of the Made in the Shade web site on to the Pi and it works just fine, except for some hard coded absolute links that jump it out to the real site.

Multiple wifi options

It would be nice to get the Pi to automatically join whichever wifi it can find from a list, or even to allow roaming like a phone...  I followed the approach in that link and it will pick up the BELL989 network from the DSL modem, the "165 Bagot" network on the AirPort Express and the "165 Bagot Street" network running on the LinkSys EA3500. I'm not sure how it chooses which one, but it can be forced by commenting out lines. It refused to pick up "165 Bagot" when it didn't have a link to the internet.

Looking like I'm Working

Press the button and all the lights go on, otherwise they just
flash on and off randomly at 1/4 second intervals.
In the seventies I worked in a department that had a Data General minicomputer. The Boss knew that computer time was highly valuable and didn't want to see it sit idle, so we wrote a program to blink its console lights at random. Just wrote the same program for the Pi in Python, but I had to build the console lights first ;-)

This is my first ever program in Python:

#!/usr/bin/env python

import RPi.GPIO as GPIO, feedparser, time, random

DEBUG = 1

GPIO.setmode(GPIO.BCM)
GREEN_LED = 27
RED_LED = 4
YELLOW_LED = 25
BUTTON = 24

GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)
GPIO.setup(YELLOW_LED, GPIO.OUT)
GPIO.setup(BUTTON, GPIO.IN)

while True:
    if GPIO.input(BUTTON): 
        n = random.getrandbits(32)

        if n % 2 == 0:
                GPIO.output(GREEN_LED, True)
        else:
                GPIO.output(GREEN_LED, False)
        if n % 3 == 0:
                GPIO.output(RED_LED, True)
        else:
                GPIO.output(RED_LED, False)
        if n % 3 == 1:
                GPIO.output(YELLOW_LED, True)
        else:
                GPIO.output(YELLOW_LED, False)
        
        time.sleep(0.25)
    else:
        GPIO.output(GREEN_LED,True)
        GPIO.output(RED_LED,True)
        GPIO.output(YELLOW_LED,True)


The LEDs are red, green and yellow tied to pins 4,  27, and 25 respectively and grounded through 560 ohm resistors. The result is dim lighting, but keeps the current down well below the 16 mA max per pin that I've seen posted multiple places. I pulled pin 24 high through an 18K resistor and added a button to ground it when it's pushed. (4, 24 and 25 were geometrically convenient on the Adafruit Pi Plate and 27 can do PWM. None of them are earmarked for I2C, SPI etc.)

No comments:

Post a Comment