I'm trying to make my RGB tree into a webthing to use with Mozilla IoT, https://github.com/mozilla-iot/webthing-python. I've tried to set the tree off using threading with a common boolean that the IoT can control to turn the tree on and off. The tree code works fine on it's own but when run it as a thread I get the following:
gpiozero.exc.GPIOPinInUse: pin 8 is already in use by <gpiozero.PWMLED object on pin GPIO8, active_high=True, is_active=False>
Here's my code, my python's pretty poor but any help would be great.
from __future__ import division, print_function
from webthing import (Action, Event, Property, SingleThing, Thing, Value, WebThingServer)
from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
from tree import RGBXmasTree
from colorzero import Color, Hue
from time import sleep
import logging
import _thread
import time
import uuid
import random
tree = LEDBoard(*range(2,28),pwm=True)
tree_on = True
tl = RGBXmasTree(brightness=0.2)
colors = [Color('red'),Color('green'),Color('blue'),Color('yellow')]
tl.color = (0.5,0.5,0.5)
def random_color():
r = random.random()
g = random.random()
b = random.random()
return(r,g,b)
def rgb():
print('rgb')
for sequence in range (1,50):
for color in colors:
tl.color = color
sleep(3)
def onebyone():
print('onebyone')
for sequence in range(1,20):
for color in colors:
for pixel in tl:
pixel.color = color
def hue():
print('hue')
tl.color = Color('red')
for sequence in range(1,200):
tl.color += Hue(deg=1)
def sparkle():
print('sparkle')
for sequence in range(1,80):
pixel = random.choice(tl)
pixel.color = random_color()
#sleep(0.1)
def tree_thread():
print('Running tree thread')
while tree_on:
for sequence in range(1,6):
if sequence == 1:
sparkle()
if sequence == 2:
hue()
if sequence == 3:
onebyone()
if sequence == 4:
sparkle()
if sequence == 5:
rgb()
while not tree_on:
tree.color=(0,0,0)
print('Tree off')
def make_thing():
thing = Thing(
'urn:dev:ops:rgb-tree',
'RGB Tree',
['OnOffSwitch', 'Light'],
'A web connected RGB Tree'
)
thing.add_property(
Property(thing,
'on',
Value(True, lambda tree_on: print('On-State is now', tree_on)),
metadata={
'title': 'On/Off',
'type': 'boolean',
'description': 'Whether the RGB-Tree is turned on',
}))
thing.add_property(
Property(thing,
'speed',
Value(50),
metadata={
'@type': 'BrightnessProperty',
'title': 'Speed',
'type': 'integer',
'description': 'The speed from 0-100',
'minimum': 0,
'maximum': 100,
'unit': 'percent',
}))
return thing
def run_server():
print('starting run server')
thing = make_thing()
print('thing = make_thing')
# If adding more than one thing, use MultipleThings() with a name.
# In the single thing case, the thing's name will be broadcast.
server = WebThingServer(SingleThing(thing), port=8888)
try:
logging.info('starting the server')
server.start()
except KeyboardInterrupt:
logging.info('stopping the server')
server.stop()
GPIO.cleanup()
logging.info('done')
if __name__ == '__main__':
logging.basicConfig(
level=10,
format="%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
)
_thread.start_new_thread(tree_thread, ())
run_server()
I'm trying to make my RGB tree into a webthing to use with Mozilla IoT, https://github.com/mozilla-iot/webthing-python. I've tried to set the tree off using threading with a common boolean that the IoT can control to turn the tree on and off. The tree code works fine on it's own but when run it as a thread I get the following:
Here's my code, my python's pretty poor but any help would be great.