Closed meatalhead closed 4 years ago
Here's an (untested) example with an LED, using the gpiozero module:
from gpiozero import LED
from webthing import Property, SingleThing, Thing, Value, WebThingServer
import logging
class LedThing(Thing):
def __init__(self):
Thing.__init__(
self,
'urn:dev:ops:led',
['Light'],
'A web connected GPIO LED'
)
self._led = LED(17)
self.add_property(
Property(
self,
'on',
Value(self.get_led(), self.set_led),
metadata={
'@type': 'OnOffProperty',
'title': 'On/Off',
'type': 'boolean',
'description': 'Whether the LED is turned on',
}
)
)
def get_led(self):
return self._led.is_lit
def set_led(self, value):
if value:
self._led.on()
else:
self._led.off()
def run_server():
thing = LedThing()
# 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()
logging.info('done')
if __name__ == '__main__':
logging.basicConfig(
level=10,
format="%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
)
run_server()
Does anyone have any examples of using a webthing to turn raspi GPIO pins on and off?