|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # Doorbell server (long story) by Gareth Halfacree <[email protected]>
|
3 | 3 |
|
| 4 | +import urllib2 |
4 | 5 | import RPi.GPIO as GPIO
|
5 |
| -import time, socket |
| 6 | +from twython import Twython |
| 7 | +import time, datetime, socket, urllib |
| 8 | +from smbus import SMBus |
6 | 9 |
|
7 |
| -setDelay = 0.05 |
| 10 | +setDelay = 0.025 |
| 11 | + |
| 12 | +CMD_ENABLE_OUTPUT = 0x00 |
| 13 | +CMD_ENABLE_LEDS = 0x13 |
| 14 | +CMD_SET_PWM_VALUES = 0x01 |
| 15 | +CMD_UPDATE = 0x16 |
| 16 | + |
| 17 | +class PiGlow: |
| 18 | + i2c_addr = 0x54 # fixed i2c address of SN3218 ic |
| 19 | + bus = None |
| 20 | + |
| 21 | + def __init__(self, i2c_bus=1): |
| 22 | + self.bus = SMBus(i2c_bus) |
| 23 | + |
| 24 | + # first we tell the SN3218 to enable output (turn on) |
| 25 | + self.write_i2c(CMD_ENABLE_OUTPUT, 0x01) |
| 26 | + |
| 27 | + # then we ask it to enable each bank of LEDs (0-5, 6-11, and 12-17) |
| 28 | + self.write_i2c(CMD_ENABLE_LEDS, [0xFF, 0xFF, 0xFF]) |
| 29 | + |
| 30 | + def update_leds(self, values): |
| 31 | + print "update pwm" |
| 32 | + self.write_i2c(CMD_SET_PWM_VALUES, values) |
| 33 | + self.write_i2c(CMD_UPDATE, 0xFF) |
| 34 | + |
| 35 | + # a helper that writes the given value or list of values to the SN3218 IC |
| 36 | + # over the i2c protocol |
| 37 | + def write_i2c(self, reg_addr, value): |
| 38 | + # if a single value is provided then wrap it in a list so we can treat |
| 39 | + # all writes in teh same way |
| 40 | + if not isinstance(value, list): |
| 41 | + value = [value]; |
| 42 | + |
| 43 | + # write the data to the SN3218 |
| 44 | + self.bus.write_i2c_block_data(self.i2c_addr, reg_addr, value) |
| 45 | + |
| 46 | +# a list of 18 values between 0 - 255 that represent each LED on the PiGlow. |
| 47 | +# to change the LEDs we set the values in this array and then pass it to the |
| 48 | +# update_leds() function to actually update the LDEs |
| 49 | +values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF] |
| 50 | + |
| 51 | +# create an instance of our PiGlow class and tell it that "1" is the I2C bus |
| 52 | +# index (should be 0 for old old old Pis) |
| 53 | +piglow = PiGlow(0) |
| 54 | + |
| 55 | +screenName = 'XXXXXX' |
| 56 | + |
| 57 | +api_token = 'XXXXXXX' |
| 58 | +api_secret = 'XXXXXXXXX' |
| 59 | +access_token = 'XXXXXXX' |
| 60 | +access_token_secret = 'XXXXXXX' |
| 61 | + |
| 62 | +#def sendSMS(to, message, hash): |
| 63 | +# params = urllib.urlencode({'to': to, 'message': message, 'hash': hash}) |
| 64 | +# f = urllib.urlopen('http://www.smspi.co.uk/send/', params) |
| 65 | +# return (f.read(), f.code) |
8 | 66 |
|
9 | 67 | GPIO.setmode(GPIO.BCM)
|
10 | 68 | GPIO.setwarnings(False)
|
11 | 69 |
|
12 | 70 | ledPins = [4, 17, 21, 18, 22, 23, 24, 25]
|
13 | 71 |
|
| 72 | +twitter = Twython(api_token, api_secret, access_token, access_token_secret) |
| 73 | + |
14 | 74 | for pin in ledPins:
|
15 | 75 | GPIO.setup(pin, GPIO.OUT)
|
16 | 76 | GPIO.output(pin, False)
|
|
25 | 85 | print 'Listening for connection.'
|
26 | 86 | c, addr = s.accept()
|
27 | 87 | print 'Connection accepted from', addr
|
28 |
| - for _ in range(4): |
29 |
| - for pin in ledPins: |
30 |
| - GPIO.output(pin, True) |
31 |
| - time.sleep(setDelay) |
32 |
| - for pin in reversed(ledPins): |
33 |
| - GPIO.output(pin, False) |
34 |
| - time.sleep(setDelay) |
| 88 | + print 'Alerting Trioptimum...' |
| 89 | + try: |
| 90 | + ss = socket.socket() |
| 91 | + ss.settimeout(1) |
| 92 | + ss.connect(('192.168.0.50', 4242)) |
| 93 | + ss.close() |
| 94 | + except socket.error: |
| 95 | + print 'Network error, Trioptimum offline.' |
| 96 | + print 'Notifying IFTTT...' |
| 97 | + urllib2.urlopen('https://maker.ifttt.com/trigger/doorbell_rang/with/key/XXXXXXXXXXXX') |
| 98 | + print 'IFTTT notified.' |
| 99 | + for _ in range(64): |
| 100 | + values.append(values.pop(0)) |
| 101 | + time.sleep(setDelay) |
| 102 | + piglow.update_leds(values) |
| 103 | + values = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] |
| 104 | + piglow.update_leds(values) |
| 105 | + values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF] |
35 | 106 | print 'Lit the lights.'
|
36 | 107 | c.send('Lit the lights.\n')
|
37 | 108 | c.close()
|
38 |
| - |
| 109 | +# print 'Trioptimum alerted. Sending Twitter DM...' |
| 110 | +# twitter.send_direct_message(screen_name=screenName, text='DING-DONG at %s' %datetime.datetime.today().strftime("%A %d %B %Y, %R")) |
| 111 | +# print 'Sending SMS via SMSPi.co.uk...' |
| 112 | +# resp, code = sendSMS('XXXXXXXXXXX', 'DING-DONG at %s' %datetime.datetime.today().strftime("%A %d %B %Y, %R"), 'XXXXXXXXXX') |
| 113 | +# print 'Message sent. All done!' |
| 114 | +# print |
39 | 115 | except KeyboardInterrupt:
|
| 116 | + values = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] |
| 117 | + piglow.update_leds(values) |
40 | 118 | GPIO.cleanup()
|
41 | 119 | s.close()
|
0 commit comments