1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
import sys
from Pubnub import Pubnub
from Adafruit_PWM_Servo_Driver import PWM
import thread
import time
import random
import Adafruit_BMP.BMP085 as BMP085
import Adafruit_DHT
import RPi.GPIO as GPIO
# Initialize Humid / Temp
sensor = BMP085.BMP085()
# Initialize Stepper
GPIO.setmode(GPIO.BOARD)
stepPins = [26,24,22,19]
waitTime = 0.000001
for pin in stepPins:
print "Setup pins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, True)
publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo-36'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo-36'
secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo-36'
cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
channel = 'futureChannel'
# pulse lengths have a max of 4096
leds = [
{'name': 'iceCaveLamp', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'iceCaveCrystal', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'fireplaceOrange', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'campfire', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'porchLight', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'fireplaceRed', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005},
{'name': 'stove', 'minPulseLength': 150, 'maxPulseLength': 2600, 'waitFloor': 0.0001, 'waitCeiling': 0.005}
]
def pubMessage(message):
print(message)
def getLEDStatus():
pubnub.publish(channel, leds, callback=pubMessage, error=pubMessage)
def callback(message, channel):
# LED Getters
if 'getLEDStatus' in message:
getLEDStatus()
# LED Setters
if 'ledID' in message:
print "message received for LED: " + leds[message['ledID']]['name']
if 'minPulseLength' in message:
print "Setting minPulseLength to: " + str(message['minPulseLength'])
leds[message['ledID']]['minPulseLength'] = message['minPulseLength']
if 'maxPulseLength' in message:
print "Setting maxPulseLength to: " + str(message['maxPulseLength'])
leds[message['ledID']]['maxPulseLength'] = message['maxPulseLength']
if 'waitCeiling' in message:
print "Setting waitCeiling to: " + str(message['waitCeiling'])
leds[message['ledID']]['waitCeiling'] = message['waitCeiling']
if 'waitFloor' in message:
print "Setting waitFloor to: " + str(message['waitFloor'])
leds[message['ledID']]['waitFloor'] = message['waitFloor']
getLEDStatus()
if 'getEnviro' in message:
enviro = {}
enviro['temp1'] = '{0:0.2f} *C'.format(sensor.read_temperature())
enviro['pres1'] = '{0:0.2f} Pa'.format(sensor.read_pressure())
enviro['alt1'] = '{0:0.2f} m'.format(sensor.read_altitude())
enviro['pres2'] = '{0:0.2f} Pa'.format(sensor.read_sealevel_pressure())
enviro['humidity1'], enviro['temp2'] = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
pubnub.publish(channel, enviro, callback=pubMessage, error=pubMessage)
if 'openDoor' in message:
moveDoor("open")
elif 'closeDoor' in message:
moveDoor("close")
def error(message):
print("ERROR : " + (message))
def connect(message):
print("CONNECTED")
startCycling()
def reconnect(message):
print("RECONNECTED")
def disconnect(message):
print("DISCONNECTED")
pubnub.subscribe(channel, callback=callback, error=callback,
connect=connect, reconnect=reconnect, disconnect=disconnect)
# http://www.raspberrypi.org/forums/viewtopic.php?f=37&t=32826
def moveDoor(direction):
stepCounter = 0
stepCount = 8
Seq = []
Seq = range(0, stepCount)
if direction == "open":
Seq[0] = [1,0,0,0]
Seq[1] = [1,1,0,0]
Seq[2] = [0,1,0,0]
Seq[3] = [0,1,1,0]
Seq[4] = [0,0,1,0]
Seq[5] = [0,0,1,1]
Seq[6] = [0,0,0,1]
Seq[7] = [1,0,0,1]
elif direction == "close":
Seq[7] = [1,0,0,0]
Seq[6] = [1,1,0,0]
Seq[5] = [0,1,0,0]
Seq[4] = [0,1,1,0]
Seq[3] = [0,0,1,0]
Seq[2] = [0,0,1,1]
Seq[1] = [0,0,0,1]
Seq[0] = [1,0,0,1]
else:
return
maxSteps = 1600
completedSteps = 0
# Start main loop
while completedSteps != maxSteps:
for pin in range(0, 4):
xpin = stepPins[pin]
pinVal = Seq[stepCounter][pin]
if pinVal!=0:
#print " Step %i Enable %i" %(StepCounter,xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
stepCounter += 1
completedSteps +=1
print " Stepcounter: %i" %(stepCounter)
if (stepCounter==stepCount):
stepCounter = 0
if (stepCounter<0):
stepCounter = stepCount
time.sleep(waitTime)
# Initialise the PWM device using the default address
pwm = PWM(0x40, debug=False)
pwm.setPWMFreq(60) # Set frequency to 60 Hz
def cycleLEDs(x):
pwm.setPWM(x, 0, leds[x]['minPulseLength'])
time.sleep(random.uniform(leds[x]['waitCeiling'], leds[x]['waitFloor']))
pwm.setPWM(x, 0, leds[x]['maxPulseLength'])
time.sleep(random.uniform(leds[x]['waitCeiling'], leds[x]['waitFloor']))
def startCycling():
while (True):
for x in range(0,7) :
# print str(x) + ": " + str(leds[x]['minPulseLength']) + " " + str(leds[x]['maxPulseLength']) + " " + str(leds[x]['waitFloor']) + " " + str(leds[x]['waitCeiling'])
# Change speed of continuous servo on channel O
try:
t = thread.start_new_thread( cycleLEDs, (x,) )
except:
print "Error: unable to start thread"
#cycleLEDs(x)
|