import time from umqtt import MQTTClient import machine, ubinascii, gc, json from machine import I2C from machine import Pin from machine import DHT import network import wifi_connect as wlan wlan.connect() d = DHT(Pin(17), DHT.DHT2X) CLIENT_ID = ubinascii.hexlify(machine.unique_id()) gc.collect() _connected = True def conncb(task): print("[{}] Connected".format(task)) _connected = True def disconncb(task): print("[{}] Disconnected".format(task)) def subscb(task): print("[{}] Subscribed".format(task)) def pubcb(pub): print("[{}] Published: {}".format(pub[0], pub[1])) def datacb(msg): print("[{}] Data arrived from topic: {}, Message:\n".format(msg[0], msg[1]), msg[2]) mqtts = network.mqtt("myx", "iot.eclipse.org", secure=True) mqtts.config(clientid=CLIENT_ID,connected_cb=conncb,subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb) time.sleep(2) gc.collect() def loop(): while True:
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, " %")
for i in sample_tds: buf_tds.append(tds.read()) sleep(1) avgValue_tds = Average(buf_tds) tdsVoltage = (avgValue_tds * (3.3 / 4095)) # read the voltage in mV tdsValue = round(ReadTDS(tdsVoltage, ds18_temp), 3) ecValue = round((tdsValue / 640), 3) # conversion factor ppm to dS/m print(ecValue, " dS/m") print(tdsValue, " ppm") #####----- DHT22 -----##### dht_sensor = DHT(14, DHT.DHT2X) result, temperature, humidity = dht_sensor.read() x = result temp_dht22 = float(temperature) hum_dht22 = int(humidity) print(temp_dht22, " ºC") print(hum_dht22, " %") #####----- Water flow HC-SR04 -----##### trig = Pin(4, Pin.OUT) echo = Pin(2, Pin.IN)
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)
import time from umqtt import MQTTClient import machine, ubinascii, gc, json import _thread as th from machine import Pin from machine import DHT import urequests class Btn1(Pin): pass class Btn2(Pin): pass TOKEN = 'IX1RkhYoWtVnfpRKkxVHJKedPosV7hO/XYHsWRQ09ai0DV0YuBHN9SNOFFXijiU2IYlTNFJB/qkdx49RuztNbdr3JyLb4Q7duN48ulGeUrWN8rzj3g5aDeWg+baY4akHER3FKDAaa7mtVZQ2xvnM4AdB04t89/1O/w1cDnyilFU=' URL = 'https://api.line.me/v2/bot/message/push' btn1 = Btn1(18, Pin.IN)btn2 = Btn2(19, Pin.IN) led1 = Pin(21, Pin.OUT) led2 = Pin(22, Pin.OUT) d = DHT(Pin(17), DHT.DHT2X) CLIENT_ID = ubinascii.hexlify(machine.unique_id()) gc.collect() def sub_cb(topic, msg): global oled print((topic, msg)) if topic.decode('utf-8') == '/line/bot/gpio': _obj = json.loads(msg); print(topic, _obj) if _obj['pin'] == 21: led1.value(_obj['status']) if _obj['pin'] == 22: led2.value(_obj['status']) # get gpio status
# 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)