def get_temperature(): try: # Connect a DHT22 sensor on GPIO pin 4 from adafruit_dht import DHT22 from board import D4 except (ImportError, NotImplementedError): # No DHT library results in an ImportError. # Running on an unknown platform results in a NotImplementedError when getting the pin return None return DHT22(D4).temperature
def get_relative_humidity(): try: # Connect to a DHT22 sensor on pin 4 from adafruit_dht import DHT22 from board import D4 except (ImportError, NotImplementedError): # No DHT library results in an ImportError. # Running on an unknown platform results in a NotImplementedError when getting the pin return None return DHT22(D4).humidity
def value(self) -> Optional[float]: try: from adafruit_dht import DHT22 from board import D20 except (ImportError, NotImplementedError): # No DHT library results in an ImportError. # Running on an unknown platform results in a # NotImplementedError when getting the pin return None try: return DHT22(D20).humidity / 100 except RuntimeError: return None
def value(self): try: # Connect to a DHT22 on pin 4 from adafruit_dht import DHT22 from board import D4 except (ImportError, NotImplementedError): # No DHT library results in an ImportError. # Running on an unknown platform results in a NotImplementedError when getting the pin return None try: return DHT22(D4).humidity / 100 except RuntimeError: return None
def __init__(self, pin, dht11): super().__init__() self._sensor = DHT11(pin) if dht11 else DHT22(pin)
def modify_thr_sleep_time(sub: MQTTClient, usr: str, psw: str, con_str: str, topic: str): sub.connect(usr, psw, con_str, False) sub.subscribe(topic) def thread_start(args, iter): for i in range(iter): thr = threading.Thread(target=send_a_sample_to_topic, args=args) thr.start() thr.join() if __name__ == '__main__': devices = [DHT22(board.D4, False), DHT22(board.D18, False)] client1, client2 = MQTTClient("sensor1"), MQTTClient("sensor2") sub1, sub2 = MQTTClient("sensor1sub"), MQTTClient("sensor2sub") conn = "connection_string" start = time.time() th1 = threading.Thread(target=thread_start, args=[[ client1, devices[0], "username1", "password1", conn, "customer/Azienda1/frigo", "comunication/Azienda1/frigo", sub1 ], 100]) th2 = threading.Thread(target=thread_start, args=[[ client2, devices[1], "username2", "password2", conn, "customer/Azienda1/frigo1",
from json import dumps from math import floor from os import environ from time import sleep, time from adafruit_dht import DHT22 from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient from board import D4 from dotenv import load_dotenv from gpiozero import InputDevice load_dotenv() InputDevice(4, pull_up=True) dhtDevice = DHT22(D4, use_pulseio=False) start_time = time() myAWSIoTMQTTClient = AWSIoTMQTTClient(environ.get("CLIENT_ID")) myAWSIoTMQTTClient.configureEndpoint(environ.get("HOST_NAME"), int(environ.get("PORT"))) myAWSIoTMQTTClient.configureCredentials("root-CA.crt", "temperatureSensor.private.key", "temperatureSensor.cert.pem") myAWSIoTMQTTClient.connect() def create_reading(): try: if dhtDevice.temperature and dhtDevice.humidity: return { "clientId": environ.get("CLIENT_ID"),
def __init__(self): self.sensor = DHT22(getattr(board, f"D{self.CONFIG['connection']}"), use_pulseio=False)
def __init__(self): self.tempSensor = OneWire.TempSensor() self.rhSensor = DHT22(board.D16)
> class Temperature: > title = "Ambient Temperature" > def value(self): ! try: # Connect to a DHT22 on pin 4 ! from adafruit_dht import DHT22 ! from board import D4 ! except (ImportError, NotImplementedError): # No DHT library results in an ImportError. # Running on an unknown platform results in a NotImplementedError when getting the pin ! return None ! try: ! return DHT22(D4).temperature ! except RuntimeError: ! return None > @staticmethod > def celsius_to_fahrenheit(cls, value: float) -> float: ! return value * 9 / 5 + 32 > @classmethod > def format(cls, value): ! if value is None: ! return "Unknown" ! else: ! return "{:.1}C ({:.1}F)".format(value, self.celsius_to_fahrenheit(value)) > def __str__(self):