DS18B20溫度感應模組+顯示網頁-micropython
tags: micropython
ds18b20
DS18B20 溫度傳感器是一種單線數字溫度傳感器。這意味著它只需要一根數據線(和 GND)即可與您的 ESP32 或 ESP8266 通信。
它可以由外部電源供電,也可以從數據線獲取電源(稱為“寄生模式”),從而無需外部電源。
以下是 DS18B20 溫度傳感器最相關規格的摘要:
通過單線總線通信
電源範圍:3.0V 至 5.5V
工作溫度範圍:-55ºC 至 +125ºC
精度 +/-0.5 ºC(介於 -10ºC 至 85ºC 之間)**
接線
程式碼
from machine import Pin
from time import sleep
import time
import onewire, ds18x20
dsSensor=ds18x20.DS18X20(onewire.OneWire(Pin(4)))
scan=dsSensor.scan()
print("Devices: ",scan)
while True:
rows=ds_sensor.scan()
print("The devices: ", rows)
sleep(1)
for row in rows:
ds_sensor.convert_temp()
print(row)
temp=ds_sensor.read_temp(row)
print(temp)
解析
匯入Pin,sleep,單總線,溫度模組
from machine import Pin
from time import sleep
import time
import onewire, ds18x20
構造函數
ow=onewire.OneWire(machine.Pin(id))
構建單總線對象。 id:引腳編號;
使用方法
ow.scan()
構造函數
ds=ds18x20.DS18X20(ow)
構建 DS18B20 傳感器對象。 ow:定義好的單總線對象;
使用方法
ds.scan()
掃描總線上的設備。返回設備地址,支持多設備同時掛載。
ds.convert_temp()
溫度轉換。
ds.read_temp(rom)
獲取溫度值。 rom:表示對應的設備號。
dsSensor=ds18x20.DS18X20(onewire.OneWire(Pin(4)))
scan=dsSensor.scan()
print("Devices: ",scan)
while True:
rows=ds_sensor.scan()
print("The devices: ", rows)
sleep(1)
for row in rows:
ds_sensor.convert_temp()
print(row)
temp=ds_sensor.read_temp(row)
print(temp)
顯示結果
Devices: \[bytearray(b'(t~\\x19\\r\\x00\\x00.')\]
bytearray(b'(t~\\x19\\r\\x00\\x00.')
26.4375
bytearray(b'(t~\\x19\\r\\x00\\x00.')
26.375
bytearray(b'(t~\\x19\\r\\x00\\x00.')
26.375
bytearray(b'(t~\\x19\\r\\x00\\x00.')
26.5625
加上網頁顯示數據
首先動手寫一個html,並把它封包起來方便呼叫
def web_html():
html="""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ESP32 TO DS18B20</title>
<style type="text/css">
body {
color: rgb(9, 39, 95);
background-color: rgb(214, 205, 231);
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
h1 {text-align: center;}
h2 {text-align: center; color: rgb(60, 12, 114);}
h3 {text-align: center; color: deeppink;}
dd {text-align: center;}
</style>
</head>
<body>
<h1>ESP32 TO DS18D20</h1>
<p Align=center><img src="https://media.istockphoto.com/photos/fahrenheit-and-celsius-scale-meteorology-thermometer-for-measuring-picture-id1138308244" height="150" width="100" ></p>
<dl>
<dt><h2>Temperature</h2></dt>
<h3><dd class="val">"""+str(round(temp, 2))+""" *C</dd><h3>
</dl>
<br></br>
<dl>
<p><i class="fas fa-temperature-high"></i></p>
<dt><h2>Temperature</h2></dt>
<h3><dd class="val">"""+str(round(temp * (9/5) + 32.0, 2))+""" *F</dd></h3>
</dl>
</body>
</html>"""
return html
之後加入wifi和socket套接客戶端的相關的接收和請求
整個程式碼如下:
import socket
from time import sleep
import time
from machine import Pin
import onewire, ds18x20
import network
ssid="基地台名稱"
password="基地台密碼"
def connected_wifi():
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 read_sensor():
global temp
ds_sensor=ds18x20.DS18X20(onewire.OneWire(Pin(4)))
while True:
rows=ds_sensor.scan()
print("The devices: ", rows)
sleep(1)
for row in rows:
ds_sensor.convert_temp()
print(row)
temp=ds_sensor.read_temp(row)
return temp
def web_html():
html="""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ESP32 TO DS18B20</title>
<style type="text/css">
body {
color: rgb(9, 39, 95);
background-color: rgb(214, 205, 231);
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
h1 {text-align: center;}
h2 {text-align: center; color: rgb(60, 12, 114);}
h3 {text-align: center; color: deeppink;}
dd {text-align: center;}
</style>
</head>
<body>
<h1>ESP32 TO DS18D20</h1>
<p Align=center><img src="https://media.istockphoto.com/photos/fahrenheit-and-celsius-scale-meteorology-thermometer-for-measuring-picture-id1138308244" height="150" width="100" ></p>
<dl>
<dt><h2>Temperature</h2></dt>
<h3><dd class="val">"""+str(round(temp, 2))+""" *C</dd><h3>
</dl>
<br></br>
<dl>
<p><i class="fas fa-temperature-high"></i></p>
<dt><h2>Temperature</h2></dt>
<h3><dd class="val">"""+str(round(temp * (9/5) + 32.0, 2))+""" *F</dd></h3>
</dl>
</body>
</html>"""
return html
connected_wifi()
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s'%str(addr))
request = conn.recv(1024)
print("content: %s"%str(request))
read_temp=read_sensor()
print(read_temp)
res=web_html()
conn.send("http/1.1 200 OK\n")
conn.send("Content-Type: text/html\n")
conn.send("Connection: close\n\n")
conn.sendall(res)
conn.close()
解析
匯入machine, sockt, network, HC_SR04模組
from machine import Pin
from HC_SR04 import HCSR04
import socket, network
設定wifi連線
def connected_wifi():
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()))
讀取DS18B20數據
def read_sensor():
global temp
ds_sensor=ds18x20.DS18X20(onewire.OneWire(Pin(4)))
while True:
rows=ds_sensor.scan()
print("The devices: ", rows)
sleep(1)
for row in rows:
ds_sensor.convert_temp()
print(row)
temp=ds_sensor.read_temp(row)
return temp
socket套接
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s'%str(addr))
request = conn.recv(1024)
print("content: %s"%str(request))
read_temp=read_sensor()
print(read_temp)
res=web_html()
conn.send("http/1.1 200 OK\\n")
conn.send("Content-Type: text/html\n")
conn.send("Connection: close\n\n")
conn.sendall(res)
conn.close()