def check_connection(self): """ Check connection to node and DHT """ if self.node.connected: if not self._dht: self._dht = DHT(self.pin, self.model, connection=self.node.connection)
class HumiditySensor(Sensor): def __init__(self, pin, name='HumiditySensor', key=None, connection=default_connection, model='11'): super().__init__(pin, name=name, key=key, connection=connection) self.type = model #DHT11 or DHT22 maybe AM2302 return def init_sensor(self): # prepare sensor on specified pin sensor_types = { '11': DHT.DHT11, '22': DHT.DHT22, '2301': DHT.AM2301 } if len(self.type) == 3 and self.type in sensor_types: self.sensor = sensor_types[self.type] else: # print('Sensor Type Error: Defaulting to DHT11') self.sensor = DHT.DHT11 self.dht = DHT(self.pin, self.sensor, connection=self.connection) def read(self): #Pass true to read in american degrees :) temperature = self.dht.readTemperature(True) humidity = self.dht.readHumidity() data = {'temperature': round(temperature, 2), 'humidity': round(humidity, 2)} variables.r.set(self.key + '_temperature', temperature) variables.r.set(self.key + '_humidity', humidity) variables.r.set(self.key, json.dumps(data)) return data def readRaw(self): return self.read()
class HumiditySensor(Sensor): def __init__(self, pin, name='HumiditySensor', key=None, connection=default_connection): super().__init__(pin, name=name, key=key, connection=connection) return def init_sensor(self): # prepare sensor on specified pin self.dht = DHT(self.pin, DHT.DHT11, connection=self.connection) def read(self): #Pass true to read in american degrees :) temperature = self.dht.readTemperature(True) humidity = self.dht.readHumidity() data = {'temperature': temperature, 'humidity': humidity} variables.r.set(self.key + '_temperature', temperature) variables.r.set(self.key + '_humidity', humidity) variables.r.set(self.key, json.dumps(data)) return data def readRaw(self): return self.read()
def init_sensor(self): # prepare sensor on specified pin sensor_types = {'11': DHT.DHT11, '22': DHT.DHT22, '2301': DHT.AM2301} if self.type in sensor_types: self.sensor = sensor_types[self.type] else: # print('Sensor Type Error: Defaulting to DHT11') self.sensor = DHT.DHT11 self.dht = DHT(self.pin, self.sensor, connection=self.connection)
class HumiditySensor(): def __init__(self, pin, connection=default_connection): self.pin = pin self.connection = connection self.api = ArduinoApi(connection) return def init_sensor(self): # prepare sensor on specified pin self.dht = DHT(self.pin, DHT.DHT11, connection=self.connection) def read(self): #Pass true to read in american degrees :) temperature = self.dht.readTemperature(True) humidity = self.dht.readHumidity() data = {'temperature': temperature, 'humidity': humidity} #r.set('temperature', temperature) #r.set('humidity', humidity) return data def readRaw(self): return self.read()
for device in devices: if (device["sensor"] == False): # This is an output device. SET the values! a.pinMode(device["port"], a.OUTPUT) type_values = device["type_values"] for type_value in type_values: name = type_value["name"] device_values = device["device_values"] a.analogWrite(device["port"], device_values[name]) elif (device["sensor"] == True): sensor = device["type"] if (sensor == "dht22"): dht = DHT(device["port"], DHT.DHT22) type_values = device["type_values"] for type_value in type_values: name = type_value["name"] device_values = device["device_values"] if (name == "temperature"): device_values[name] = dht.readTemperature( False) elif (name == "humidity"): device_values[name] = dht.readHumidity() elif (sensor == "ldr"): a.pinMode(device["port"], a.INPUT) intensity = a.analogRead(device["port"]) type_values = device["type_values"] for type_value in type_values:
#!/usr/bin/env python # a simple example to read from a DHT sensor from nanpy import DHT # http://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor # DHT sensor connected to digital pin 10 dhts = [ DHT(6, DHT.DHT11), DHT(7, DHT.DHT11), DHT(8, DHT.DHT11) ] for i, dht in enumerate(dhts): print("DHT %d" % i) print("Temperature is %.2f degrees Celcius" % dht.readTemperature(False)) print("Temperature is %.2f degrees Fahrenheit" % dht.readTemperature(True)) print("Humidity is %.2f %%" % dht.readHumidity())
from nanpy import ArduinoApi from nanpy import SerialManager import serial import struct import time from time import sleep import datetime from nanpy.sockconnection import SocketManager from nanpy import DHT from firebase import firebase connection = SerialManager() a = ArduinoApi(connection=connection) dhts = [DHT(6, DHT.DHT11)] a.pinMode(0, a.INPUT) a.pinMode(1, a.INPUT) a.pinMode(2, a.INPUT) def BoardStatus(): print("Status Broadcast") time.sleep(1) for i in range(3): DHTSensor() SoilMoistureSensor() time.sleep(0.5) def DHTSensor():
#!/usr/bin/env python # a simple example to read from a DHT sensor from nanpy import DHT # http://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor # DHT sensor connected to digital pin 10 dht = DHT(10,DHT.DHT22) print("temperature is %f degrees Celcius" % dht.readTemperature()) print("temperature is %f degrees Celcius" % dht.readTemperature(False)) print("temperature is %f degrees Fahrenheit" % dht.readTemperature(True)) print("humidity is %f %%" % dht.readHumidity())
from nanpy import DHT dhts = [DHT(6, DHT.DHT11), DHT(7, DHT.DHT11), DHT(8, DHT.DHT11)] for i, dht, in enumerate(dhts): print("DHT %d" % i) print("Temperature is %.2f degree Celcius" % dht.readTemperature(False)) print("Temperature is %.2f degrees Fahrenheit" % dht.readTemperature(True)) print("Humidity is %.2f %%" % dht.readHumidity())
class NanpyDHTSensor(Sensor): """ Nanpy DHT Sensor Get readings from DHT device. """ models = {'11': DHT.DHT11, '22': DHT.DHT22, '2301': DHT.AM2301} """ Properties """ @property def id(self): """ Return a unique id for the component """ return self.config['key'] @property def name(self): """ Return the display name of the component """ return self.config.get( 'name') or f"{self.id.replace('_', ' ').title()}" @property def state(self): """ Return the state of the component (from memory, no IO!) """ return self._state @property def classifier(self): """ Classification further describing it, effects the data formatting """ return self.config.get('classifier', 'general') @property def analog(self): """ Return if gpio is digital or analog """ return self.config.get('analog', False) @property def pin(self): """ Return if gpio is digital or analog """ return self.config.get('pin') @property def model(self): """ Return DHT model """ if self.config.get('model') not in NanpyDHTSensor.models: conf['model'] = '11' return self.models[self.config.get('model', '11')] """ Methods """ def init(self): """ Connect to the Parent Device """ self._state = None self._dht = None # Attribute to track from DHT device self._attribute = self.config.get('attribute', 'temperature') return True def check_connection(self): """ Check connection to node and DHT """ if self.node.connected: if not self._dht: self._dht = DHT(self.pin, self.model, connection=self.node.connection) def update(self): """ Get data from DHT through nanpy""" if self.node.connected: try: self.check_connection() if self._dht: _temp_format = self.mudpi.unit_system == IMPERIAL_SYSTEM temperature = self._dht.readTemperature(_temp_format) humidity = self._dht.readHumidity() data = { 'temperature': round(temperature, 2), 'humidity': round(humidity, 2) } self._state = data except (SerialManagerError, SocketManagerError, BrokenPipeError, ConnectionResetError, OSError, socket.timeout) as e: if self.node.connected: Logger.log_formatted( LOG_LEVEL["warning"], f'{self.node.key} -> Broken Connection', 'Timeout', 'notice') self.node.reset_connection() self._dht = None else: self._dht = False return None
def init_sensor(self): # prepare sensor on specified pin self.dht = DHT(self.pin, DHT.DHT11, connection=self.connection)
from time import sleep from nanpy import ArduinoApi from nanpy import DHT from nanpy import SerialManager print 'Booting system' LED_PIN = 13 DHT_PIN = 12 PRES_PIN = 'A0' WET_PIN = 'A1' CONNECTION = SerialManager(device='/dev/cu.usbmodem1411') ARDUINO = ArduinoApi(connection=CONNECTION) ENV_SENSOR = DHT(DHT_PIN, DHT.DHT22, connection=CONNECTION) # Setup ARDUINO.pinMode(LED_PIN, ARDUINO.OUTPUT) ARDUINO.pinMode(DHT_PIN, ARDUINO.INPUT) ARDUINO.pinMode(PRES_PIN, ARDUINO.INPUT) ARDUINO.pinMode(WET_PIN, ARDUINO.INPUT) # MEANINGLESS DATA STILL # Loop try: while True: ARDUINO.digitalWrite(LED_PIN, ARDUINO.HIGH) print 'Gathering data...' pres = ARDUINO.analogRead(PRES_PIN) print 'Light Resistance: ', pres temp = ENV_SENSOR.readTemperature(True)
#!/usr/bin/env python # a simple example to read from a DHT sensor from nanpy import DHT # http://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor # DHT sensor connected to digital pin 10 dht = DHT(10, DHT.DHT22) print("Temperature is %.2f degrees Celcius" % dht.readTemperature(False)) print("Temperature is %.2f degrees Fahrenheit" % dht.readTemperature(True)) print("Humidity is %.2f %%" % dht.readHumidity())