DHT11+OLED+LINE-notity-micropython


DHT11+OLED+LINE-notity-micropython

tags: micropython line dht11/dht12 esp32

前言

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

溫濕度。

接線

關於DHT11/12往這裡

OLED往這裡

IFTTT申請和設定

https://ifttt.com

程式碼

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))


文章作者: blairan
版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY 4.0 許可協議。轉載請註明來源 blairan !
评论
  目錄