def _load_hardware(self): address = self._address if len(address) >= 2 and terrariumUtils.is_float(address[1]): # Set / enable power management self._device['power_mngt'] = terrariumUtils.to_BCM_port_number(address[1]) if self.HARDWARE == terrariumDHT11Sensor.HARDWARE: device = DHT11(terrariumUtils.to_BCM_port_number(address[0])) elif self.HARDWARE in [terrariumDHT22Sensor.HARDWARE, terrariumAM2302Sensor.HARDWARE]: device = DHT22(terrariumUtils.to_BCM_port_number(address[0])) return device
def test_dht11(dht11gpio): if not dht11gpio: pytest.skip("Skipping. No GPIO Pin for DHT11 Provided.") return dht = DHT11(gpio=int(dht11gpio)) result = dht.read() print(result) dht11_result = result assert 'temp_c' in result assert 'temp_f' in result assert 'humidity' in result assert 'valid' in result assert 0 <= result['humidity'] <= 100
def main(): # メイン関数 この関数は末尾のif文から呼び出される client = mqtt.Client() # クラスのインスタンス(実体)の作成 client.on_connect = onConnect # 接続時のコールバック関数を登録 client.on_disconnect = onDisconnect # 切断時のコールバックを登録 client.on_publish = onPublish # メッセージ送信時のコールバック sensor = DHT11(GPIO) client.connect(HOST, 1883, 60) # 接続先は自分自身 # 通信処理スタート client.loop_start() # subはloop_forever()だが,pubはloop_start()で起動だけさせる # 永久に繰り返す while True: payload = json.dumps(sensor.read(), ensure_ascii=False, indent=4) print(payload) client.publish(TOPIC, payload) # トピック名とメッセージを決めて送信 sleep(3) # 3秒待つ
""" File: chapter09/dht_measure.py Measure temperature and humidity with DHT sensor. Dependencies: pip3 install pigpio pigpio-dht Built and tested with Python 3.7 on Raspberry Pi 4 Model B """ from pigpio_dht import DHT11, DHT22 # (1) SENSOR_GPIO = 21 sensor = DHT11(SENSOR_GPIO) # (2) #sensor = DHT22(SENSOR_GPIO) if __name__ == '__main__': result = sensor.read(retries=2) # (3) print(result) result = sensor.sample(samples=5) # (4) print(result)
mport subprocess import time #subprocess.call("sudo pigpiod", shell=True) #time.sleep(0.1) from pigpio_dht import DHT11, DHT22 while True: gpio = 4 # BCM Numbering sensor = DHT11(gpio) #sensor = DHT22(gpio) result = sensor.read() print(result) temperature=([value for value in result.values()][0]) print(temperature) humidity=([value for value in result.values()][2]) print(humidity) time.sleep(10)
def MSG(*args): if Verbose: msg = " ".join([str(a) for a in args]) print(msg) sys.stdout.flush() def sendWithRetry(am, data): for retry in range(6): try: ret = am.send(data) MSG('sent to Ambient (ret = %d)' % ret.status_code) break except requests.exceptions.RequestException as e: MSG('request failed.') time.sleep(10) am = ambient.Ambient(int(CHANNEL), WRITEKEY) sensor = DHT11(DHT11_PIN) while True: try: data = sensor.read(retries=5) except TimeoutError: data = {"valid": False} if data["valid"]: MSG("temperature: {} / humidity: {}".format(data["temp_c"], data["humidity"])) sendWithRetry(am, {'d1': data["temp_c"], 'd2': data["humidity"]}) time.sleep(180)
from common import config from common.logs import MAIN as logger from pigpio_dht import DHT11 from gpiozero import MCP3008, DigitalOutputDevice try: TEMP_HUMD_SENSOR = DHT11(config.DHT11_PIN) MOISTURE_SENSOR = MCP3008(channel=config.MCP3008_CHANNEL) MOISTURE_SENSOR_POWER = DigitalOutputDevice(config.MCP3008_POWER_PIN) FAN_RELAY = DigitalOutputDevice(config.FAN_RELAY_PIN, active_high=False, initial_value=False) LIGHT_RELAY = DigitalOutputDevice(config.LIGHT_RELAY_PIN, active_high=False, initial_value=False) WATER_RELAY = DigitalOutputDevice(config.WATER_RELAY_PIN, active_high=False, initial_value=False) except Exception as e: logger.error("Hardware doesn't start properly: %s", e)
folder = os.path.dirname(__file__) with PidFile('measurements', piddir=os.path.join(folder, '.pid/')) as p: #read settings file settings = configparser.ConfigParser() settings.read(os.path.join(folder, 'settings.ini')) #pigpio instance pi = pigpio.pi() #define DHT11 dht_pin = settings['DHT']['DataPin'] dht_timeout_secs = settings['DHT']['TimeoutSecs'] #gpio, timeout_secs=0.5, use_internal_pullup=True, pi=None dht_sensor = DHT11(int(dht_pin), float(dht_timeout_secs), True, pi) #define sampling period sampling_period = settings['MEASUREMENTS']['SamplingPeriod'] #define influxdb influx_server = settings['INFLUXDB']['Server'] influx_port = settings['INFLUXDB']['Port'] influx_db = settings['INFLUXDB']['Database'] influx_client = InfluxDBClient(host=influx_server, port=influx_port, database=influx_db) # print(f"Using database <{influx_db}> on <{influx_server}>") # print(f"Current sampling pariod is <{sampling_period} seconds>")
Built and tested with Python 3.7 on Raspberry Pi 4 Model B """ from pigpio_dht import DHT11, DHT22 from datetime import datetime from time import sleep import requests import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # DHT Temperature/Humidity Sensor GPIO. GPIO = 24 # (1) # Configure DHT sensor - Uncomment appropiate line based on the sensor you have. dht = DHT11(GPIO, use_internal_pullup=True, timeout_secs=0.5) # (2) #dht = DHT22(GPIO, use_internal_pullup=True, timeout_secs=0.5) # How often we check the temperature POLL_INTERVAL_SECS = 60 * 10 # 10 Minutes # Celsius or Fahrenheit USE_DEGREES_CELSIUS = True # (3) # At what high temperature will we send an email HIGH_TEMP_TRIGGER = 20 # Degrees # (4) # At what low temperature will we send an email LOW_TEMP_TRIGGER = 19 # Degrees # (5) # To track when a high temp event has been triggered