comparison service/piNode/devices.py @ 304:e7cbf250188a

influx output, fade support, switch to Adafruit_DHT, start of Lcd8544 Ignore-this: dd9cae16daafd780a301728a1ce8eb38
author drewp@bigasterisk.com
date Fri, 16 Sep 2016 00:55:04 -0700
parents 7b5cff542078
children f420207c7fb4
comparison
equal deleted inserted replaced
303:66fe7a93753d 304:e7cbf250188a
1 """
2 https://github.com/juniorug/libsensorPy is a similar project
3 """
1 from __future__ import division 4 from __future__ import division
2 5
3 import time, logging, os 6 import time, logging, os
4 from rdflib import Namespace, URIRef, Literal 7 from rdflib import Namespace, URIRef, Literal
5 from twisted.internet import reactor 8 from twisted.internet import reactor
138 # compare motion sensor lib at 141 # compare motion sensor lib at
139 # https://gpiozero.readthedocs.org/en/v1.2.0/api_input.html#motion-sensor-d-sun-pir 142 # https://gpiozero.readthedocs.org/en/v1.2.0/api_input.html#motion-sensor-d-sun-pir
140 # which is a bit fancier 143 # which is a bit fancier
141 deviceType = ROOM['MotionSensor'] 144 deviceType = ROOM['MotionSensor']
142 145
143 def setup(self): 146 def __init__(self, graph, uri, pi, pinNumber):
144 self.pi.set_mode(17, pigpio.INPUT) 147 super(MotionSensorInput, self).__init__(graph, uri, pi, pinNumber)
145 self.pi.set_pull_up_down(17, pigpio.PUD_DOWN) 148 self.pi.set_mode(pinNumber, pigpio.INPUT)
149 self.pi.set_pull_up_down(pinNumber, pigpio.PUD_DOWN)
146 150
147 def hostStateInit(self): 151 def hostStateInit(self):
148 self.lastRead = None 152 self.lastRead = None
149 self.lastMotionStart30 = 0 153 self.lastMotionStart30 = 0
150 self.lastMotionStart90 = 0 154 self.lastMotionStart90 = 0
151 155
152 def poll(self): 156 def poll(self):
153 motion = self.pi.read(17) 157 motion = self.pi.read(self.pinNumber)
154 now = time.time() 158 now = time.time()
155 159
156 oneshot = [] 160 oneshot = []
157 if self.lastRead is not None and motion != self.lastRead: 161 if self.lastRead is not None and motion != self.lastRead:
158 oneshot = [(self.uri, ROOM['sees'], ROOM['motionStart'])] 162 oneshot = [(self.uri, ROOM['sees'], ROOM['motionStart'])]
253 257
254 258
255 @register 259 @register
256 class TempHumidSensor(DeviceType): 260 class TempHumidSensor(DeviceType):
257 deviceType = ROOM['TempHumidSensor'] 261 deviceType = ROOM['TempHumidSensor']
262 pollPeriod = 5
258 263
259 def __init__(self, *a, **kw): 264 def __init__(self, *a, **kw):
260 DeviceType.__init__(self, *a, **kw) 265 DeviceType.__init__(self, *a, **kw)
261 sys.path.append('/opt/pigpio/EXAMPLES/Python/DHT22_AM2302_SENSOR') 266 import Adafruit_DHT
262 import DHT22 267 self.mod = Adafruit_DHT
263 self.sensor = DHT22.sensor(self.pi, self.pinNumber)
264 268
265 def poll(self): 269 def poll(self):
266 self.sensor.trigger() 270 for tries in range(1):
267 humid, tempC = self.sensor.humidity(), self.sensor.temperature() 271 # fails a lot, but I don't want to add too much delay in
272 # here- the next poll is coming soon
273 humid, tempC = self.mod.read(self.mod.DHT22, self.pinNumber)
274 if humid and tempC:
275 break
268 276
269 stmts = set() 277 stmts = set()
270 if humid is not None: 278 if humid is not None:
271 stmts.add((self.uri, ROOM['humidity'], Literal(round(humid, 2)))) 279 stmts.add((self.uri, ROOM['humidity'], Literal(round(humid, 2))))
272 else: 280 else:
333 Also see /my/proj/ansible/roles/raspi_io_node/tasks/main.yml for 341 Also see /my/proj/ansible/roles/raspi_io_node/tasks/main.yml for
334 some system config that contains the pin number that you want to 342 some system config that contains the pin number that you want to
335 use for onewire. The pin number in this config is currently ignored. 343 use for onewire. The pin number in this config is currently ignored.
336 """ 344 """
337 deviceType = ROOM['OneWire'] 345 deviceType = ROOM['OneWire']
346 pollPeriod = 2
338 # deliberately written like arduinoNode's one for an easier merge. 347 # deliberately written like arduinoNode's one for an easier merge.
339 def __init__(self, *a, **kw): 348 def __init__(self, *a, **kw):
340 DeviceType.__init__(self, *a, **kw) 349 DeviceType.__init__(self, *a, **kw)
341 log.info("scan for w1 devices") 350 log.info("scan for w1 devices")
342 self._sensors = w1thermsensor.W1ThermSensor.get_available_sensors() 351 self._sensors = w1thermsensor.W1ThermSensor.get_available_sensors()
414 class LedOutput(DeviceType): 423 class LedOutput(DeviceType):
415 deviceType = ROOM['LedOutput'] 424 deviceType = ROOM['LedOutput']
416 425
417 def hostStateInit(self): 426 def hostStateInit(self):
418 self.value = 0 427 self.value = 0
419 self.fv = FilteredValue(self._setPwm) 428 if (self.uri, ROOM['fade'], None) in self.graph:
429 # incomplete- the object could be fade settings
430 self.fv = FilteredValue(self._setPwm)
431 else:
432 _setPwm = self._setPwm
433 class Instant(object):
434 def set(self, goal):
435 _setPwm(goal)
436 self.fv = Instant()
420 self.gamma = float(self.graph.value(self.uri, ROOM['gamma'], default=1)) 437 self.gamma = float(self.graph.value(self.uri, ROOM['gamma'], default=1))
421 438
422 def setup(self): 439 def setup(self):
423 setupPwm(self.pi, self.pinNumber) 440 setupPwm(self.pi, self.pinNumber)
424 441
532 'element': 'output-rgb', 549 'element': 'output-rgb',
533 'subj': px, 550 'subj': px,
534 'pred': ROOM['color'], 551 'pred': ROOM['color'],
535 } for px in self.pixelUris] 552 } for px in self.pixelUris]
536 553
554 @register
555 class Lcd8544(DeviceType):
556 """PCD8544 lcd (nokia 5110)"""
557 deviceType = ROOM['RgbStrip']
558
559 @classmethod
560 def findInstances(cls, graph, board, pi):
561 for row in graph.query("""
562 SELECT DISTINCT ?dev ?din ?clk ?dc ?rst WHERE {
563 ?dev a :Lcd8544 .
564 ?board :hasPin ?dinPin . ?dev :din ?dinPin . ?dinPin :gpioNumber ?din .
565 ?board :hasPin ?clkPin . ?dev :clk ?clkPin . ?clkPin :gpioNumber ?clk .
566 ?board :hasPin ?dcPin . ?dev :dc ?dcPin . ?dcPin :gpioNumber ?dc .
567 ?board :hasPin ?rstPin . ?dev :rst ?rstPin . ?rstPin :gpioNumber ?rst .
568 } ORDER BY ?dev""",
569 initBindings=dict(board=board),
570 initNs={'': ROOM}):
571 log.debug('found lcd %r', row)
572 yield cls(graph, row.dev, pi,
573 int(row.din), int(row.clk),
574 int(row.dc), int(row.rst))
575
576 def __init__(self, graph, uri, pi, din, clk, dc, rst):
577 super(Lcd8544, self).__init__(graph, uri, pi, None)
578
579
580 import RPi.GPIO
581 import Adafruit_Nokia_LCD
582 import Adafruit_GPIO.SPI
583 self.lcd = Adafruit_Nokia_LCD.PCD8544(
584 dc=8, rst=7,
585 spi=Adafruit_GPIO.SPI.BitBang(
586 Adafruit_Nokia_LCD.GPIO.RPiGPIOAdapter(RPi.GPIO),
587 sclk=clk,
588 mosi=din))
589 self.lcd.begin(contrast=60)
590
591 def hostStatements(self):
592 return []
593 return [(self.uri, ROOM['color'], Literal(self.value))]
594
595 def outputPatterns(self):
596 return []
597 return [(self.uri, ROOM['color'], None)]
598
599 def sendOutput(self, statements):
600 return
601 assert len(statements) == 1
602 assert statements[0][:2] == (self.uri, ROOM['color'])
603
604 rgb = self._rgbFromHex(statements[0][2])
605 self.value = statements[0][2]
606
607 for (i, v) in zip(self.rgb, rgb):
608 self.pi.set_PWM_dutycycle(i, v)
609
610 def outputWidgets(self):
611 return []
612 return [{
613 'element': 'output-rgb',
614 'subj': self.uri,
615 'pred': ROOM['color'],
616 }]
617
618
537 619
538 def makeDevices(graph, board, pi): 620 def makeDevices(graph, board, pi):
539 out = [] 621 out = []
540 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__): 622 for dt in sorted(_knownTypes, key=lambda cls: cls.__name__):
541 out.extend(dt.findInstances(graph, board, pi)) 623 out.extend(dt.findInstances(graph, board, pi))