DHT11+OLED+IFTTT+LINE-notity-micropython

 DHT11+OLED+LINE-notity-micropython

前言

這次要來實驗菜市場傳感器😂-DHT11在OLED上顯示並且在LINE上通知
溫濕度。

接線

關於DHT11/12這裡

        OLED這裡

IFTTT申請和設定



程式碼

import dht, ssd1306, urequests, network, gc
from machine import Pin, SoftI2C
from time import sleep

#wifi連線
def wifiConn():
    ssid = "---"
    password = "-----"
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.connect(ssid, password)
    while wifi.isconnected() == False:
        pass
    print("connect successful!")
    print("IP: {}".format(wifi.ifconfig()))

#偵測溫濕度
def detectDHT():
    global d11
    d11=dht.DHT11(Pin(5))
    d11.measure()
    temp=d11.temperature()
    hum=d11.humidity()
    return temp, hum

#OLED顯示
def oledDisplay():
    i2c=SoftI2C(sda=Pin(21), scl=Pin(22))
    oled=ssd1306.SSD1306_I2C(128, 64, i2c)
    oled.fill(0)
    oled.text('~ TEMP & HUMID ~',0,0)
    oled.text('Temp = {} C'.format(t),0,16)
    oled.text('Humid= {} %'.format(h),0,32)
    oled.show()

if __name__ == '__main__':
    wifiConn()
    #IFTTT網頁裡給定的KEY
    token="hVvZLHqWXg1d0hNaIVr1lw1XXXXXXXXXXXXXXX"
    while True:
        sleep(2)
        t, h=detectDHT()
        #IFTTT網頁裡,文件告知的獲取數值的方法
        url="https://maker.ifttt.com/trigger/dhtTest/with/key/"+\
        "{}?value1={}&value2={}".format(token,t, h)
        try:
            lineUrl=urequests.get(url)
            print('code={},content={}'.format(lineUrl.status_code,lineUrl.text))
        except Exception as e:
            print(e)
            wifiConn()
        lineUrl.close()
        sleep(10)
        oledDisplay()
        print("Temperature:{}*C".format(t))
        print("Humiduty:{}%".format(h))



留言

這個網誌中的熱門文章

使用PWM控制伺服馬達-micropython

BMP280氣壓,溫度模組-micropython