import time from umqtt import MQTTClient import machine, ubinascii, gc, json from machine import I2C from machine import Pin from machine import DHT CLIENT_ID = ubinascii.hexlify(machine.unique_id()) gc.collect() d = DHT(Pin(17), DHT.DHT2X) client = MQTTClient(CLIENT_ID, 'q.emqtt.com') client.connect() time.sleep(2) while True: try: result, t, h = d.read() if result: msg = json.dumps({ 'heap': gc.mem_free(), 'Type': 7, 'Id': CLIENT_ID, 'temperature': '{0:.2f}'.format(t), 'humidity': '{0:.2f}'.format(h) }) print(msg) client.publish('micro/python/temperature', msg) time.sleep(5) except OSError as e: print(e)
from machine import Pin, I2C, time_pulse_us, Onewire, DHT, ADC, deepsleep from time import sleep, sleep_ms, sleep_us #from onewire import OneWire #from ds18x20 import DS18X20 #import dht # Channel ID 902540 # KEY IL9VIMCHEXM9H3W4 # GET https://api.thingspeak.com/update?api_key=IL9VIMCHEXM9H3W4&field1=0 # Channel ID 985682 # KEY 2IDFEOWYZCNDP7YW # GET https://api.thingspeak.com/update?api_key=2IDFEOWYZCNDP7YW&field1=0 #####----- DHT22 -----##### dht_sensor = DHT(19, DHT.DHT2X) result, temperature, humidity = dht_sensor.read() print(dht_sensor.read()) x = result temp_dht22 = float(temperature) hum_dht22 = int(humidity) print(temp_dht22, " ºC") print(hum_dht22, " %")
# Tested on M5Stack Gray # DHT11 sensor module. see https://www.switch-science.com/catalog/818/ from m5stack import lcd from machine import DHT, Pin import time dht = DHT(Pin(22)) lcd.clear() lcd.font(lcd.FONT_Default, fixedwidth=True) while True: result, temperature, humidity = dht.read() if result: lcd.println('Temperature: {} degrees'.format(temperature), 0, 0, color=lcd.GREEN) lcd.println('Humidity: {} %'.format(humidity), color=lcd.GREEN) time.sleep(60)