溫濕度傳感器(DHT11/12)+ThingSpeak


溫濕度傳感器(DHT11/12)+ThingSpeak

tags: micropython thingspeak dht11/dht12


回顧DHT11/12實作紀錄

基本介紹和實驗參考-https://esp32-blairan.blogspot.com/2021/07/dht11dht12-esp32.html


import dht

from machine import Pin

from time import sleep


def measureTemp():

    sensor=dht.DHT11(Pin(4))

    while True:

        try:

            sleep(2)

            sensor.measure()

            t=sensor.temperature()

            t_f=t*(9/5)+32

            h=sensor.humidity()

            print("Temp: {:.1f}.C".format(t))

            print("Temp: {:.1f}.F".format(t_f))

            print("Humidity: {:.1f}".format(h))

        except OSError as o:

            print("Failed to read sensor.")

if __name__ == '__main__':

    measureTemp()

載入dht庫(內建)

import dht

from machine import Pin

from time import sleep

指定DHT11腳位給sensor變數

sensor=dht.DHT11(Pin(4))

給予while()每2秒不斷偵測

sleep(2)

sensor.measure()

攝氏溫度(t)/華氏溫度(t_f)/濕度

其中攝氏轉換華氏的公式:攝氏x(9/5)+32

t=sensor.temperature()

t_f=t*(9/5)+32

h=sensor.humidity()

print("Temp: {:.1f}.C".format(t))

print("Temp: {:.1f}.F".format(t_f))

print("Humidity: {:.1f}".format(h))

結果

>>> Temp: 26.0.C

Temp: 78.8.F

Humidity: 27.0

Temp: 26.0.C

Temp: 78.8.F

Humidity: 27.0

Temp: 26.0.C

Temp: 78.8.F

Humidity: 27.0

如何上傳數據到ThingSpeak

先到此ThingSpeak網站註冊一帳號



程式碼

import network, urequests

from machine import Pin

import dht

from time import sleep

  

ssid="基地台名稱"

password="基地台密碼"

api_key="VCJQHQFVSMB-----"

dht11=dht.DHT11(Pin(27))

sleep(1)

def wifiContent():

    global ssid, password

    wifi=network.WLAN(network.STA_IF)

    wifi.active(True)

    wifi.connect(ssid, password)

    while wifi.isconnected() == False:

        pass

    print("connected successful!")

    print("IP: {}".format(wifi.ifconfig()))

def getDHT():

    while (1):

        dht11.measure()

        d=dht11.temperature()

        rh=dht11.humidity()

        print("Temp: {:.1f}*C,  Humi: {:.1f}%RH".format(d, rh))

        uloadUrl="https://api.thingspeak.com/update?api_key={}&field1={}&field2={}".format(api, d, rh)

        try:

            res=urequests.get(uloadUrl)

            print(res.text)

        except OSError as e:

            print(e)

        sleep(16)

if __name__=="__main__":

    wifiContent()

    getDHT()

解析

匯入對應的模組,其urequest是小型爬蟲,可用於抓取和上傳

import network, urequests

from machine import Pin

import dht

from time import sleep

基地台名稱和密碼先給予變數,還有剛在ThingSpeak給的api_key

dht腳位設定,停止一秒使其初始化完全

ssid="基地台名稱"

password="基地台密碼"

api="VCJQHQFVSMB-----"

dht11=dht.DHT11(Pin(27))

sleep(1)

這個封包裡是連接wifi的流程

def wifiContent():

    global ssid, password

    wifi=network.WLAN(network.STA_IF)

    wifi.active(True)

    wifi.connect(ssid, password)

    while wifi.isconnected() == False:

        pass

    print("connected successful!")

    print("IP: {}".format(wifi.ifconfig()))

這個封包裡是讀取dht11/12數據,和上傳到ThingSpeak同步

*如果有一個以上的數據,除了ThingSpeak網站上要設定一個以上的對應欄位,再來就是程式的網址格式,例如:

api_key

api_key={}

2個數據上傳到網站的2個欄位,field1代表第一個欄位,field2代表第二個欄位

&field1={}&field2={}
def getDHT():

    while (1):

        dht11.measure()

        d=dht11.temperature()

        rh=dht11.humidity()

        print("Temp: {:.1f}*C,  Humi: {:.1f}%RH".format(d, rh))

        uloadUrl="https://api.thingspeak.com/update?api_key={}&field1={}&field2={}".format(api, d, rh)

        try:

            res=urequests.get(uloadUrl)

            print(res.text)

        except OSError as e:

            print(e)

        sleep(16)


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