这也是一个9元全套的货色,带一个18B20温度传感器,带一个ESP01芯片,电压输入是3.3-12V电压,可以直接上电池供电都不用转换的。
这位模块大兄弟不但价格便宜而且体长苗条,2.5厘米的身形比拇指还要小一圈。但功能就可圈可点了,挂载上MICROPYTHON v1.12固件。测温后有WIFI的地方可一直MQTT传入云平台,无WIFI地方可以自己开热点形成网页显示温度,当然也可以和9元继电器版本的好兄弟自己组个局域网,来个分体式嵌入控制。
闲话少说本次记录的目的不是推销模块的,这个模块调试起来非常的不方便。因为micropython一般开发都是串口联调的,这个模块封锁了所有的引脚,在模块底下焊线也不是个正经办法,导致我特地做了一个调试座,调完了以后考虑到以后不管是挂接什么设备都应该有个显示方式,所以本次是传感器拿数据,模块自己开热点然后网页显示传感器数据。
看看调试座:
第一步,贴上ds18x20.py这个库
from onewire import OneWire
class DS18X20(object):
def __init__(self, pin):
self.ow = OneWire(pin)
# Scan the 1-wire devices, but only keep those which have the
# correct # first byte in their rom for a DS18x20 device.
self.roms = [rom for rom in self.ow.scan() if rom[0] == 0x10 or rom[0] == 0x28]
def read_temp(self, rom=None):
"""
Read and return the temperature of one DS18x20 device.
Pass the 8-byte bytes object with the ROM of the specific device you want to read.
If only one DS18x20 device is attached to the bus you may omit the rom parameter.
"""
rom = rom or self.roms[0]
ow = self.ow
ow.reset()
ow.select_rom(rom)
ow.write_byte(0x44) # Convert Temp
while True:
if ow.read_bit():
break
ow.reset()
ow.select_rom(rom)
ow.write_byte(0xbe) # Read scratch
data = ow.read_bytes(9)
return self.convert_temp(rom[0], data)
def read_temps(self):
"""
Read and return the temperatures of all attached DS18x20 devices.
"""
temps = []
for rom in self.roms:
temps.append(self.read_temp(rom))
return temps
def convert_temp(self, rom0, data):
"""
Convert the raw temperature data into degrees celsius and return as a float.
"""
temp_lsb = data[0]
temp_msb = data[1]
if rom0 == 0x10:
if temp_msb != 0:
# convert negative number
temp_read = temp_lsb >> 1 | 0x80 # truncate bit 0 by shifting, fill high bit with 1.
temp_read = -((~temp_read + 1) & 0xff) # now convert from two's complement
else:
temp_read = temp_lsb >> 1 # truncate bit 0 by shifting
count_remain = data[6]
count_per_c = data[7]
temp = temp_read - 0.25 + (count_per_c - count_remain) / count_per_c
return temp
elif rom0 == 0x28:
return (temp_msb << 8 | temp_lsb) / 16
else:
assert False
第二步,自己弄一个ds18b20test.py
这个库
#Hardware Platform: FireBeetle-ESP32
#Result: measure enviromental temperature.
#Hardware Connection: this test need to connect a 'DS18B20' digital temperature sensor in IO25.
#The info below shows that ds18b20Demo is available for the current version.
# IO0 IO2 IO4 IO5 IO9 IO10 IO12~19
# IO21 IO22 IO23 IO25 IO26 IO27
from machine import Pin
import onewire
import ds18x20
import time
class Ds18b20_test(object):
def __init__(self,ow):
self.ow=ow
self.ow.scan()
self.ds=ds18x20.DS18X20(self.ow) #create ds18x20 object
def read_data(self):
roms=self.ds.scan() #scan ds18x20
self.ds.convert_temp() #convert temperature
for rom in roms:
rec_data=self.ds.read_temp(rom) #display
print(rec_data)
return rec_data
if __name__=='__main__':
ow = onewire.OneWire(Pin(25)) #Init wire
a=Ds18b20_test(ow)
a.read_data()
第三步:介绍下这上俩个库都下到板子里后,主函数调用方式,比较简单。
from ds18b20test import Ds18b20_test
import onewire
ow = onewire.OneWire(Pin(25)) #Init wire
a=Ds18b20_test(ow)
a.read_data()
第四步:针对8266esp01的18650 模块他的传感器接在GPIO2号引脚所以针对这个情况写了一个网页显示的程序,如果对网页部分感兴趣,建议看我另一个博客的9元继电器部分,那里对网页显示有详细的注释,而这个博客关注的是传感器拿数据的部分会有几个注释。
try:
import usocket as socket
except:
import socket
import network
import machine,time
from machine import Pin
login_html = """
<html>
<html>
<head>
<title>登录平台</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
h2
{
margin-top:4%;
margin-bottom:40px;
}
</style>
</head>
<body>
<center>
<h2>登录服务器</h2>
<form action="/" method="get" accept-charset="utf-8">
<p>账号: <input type="text" name="name" /></p>
<p>密码: <input type="text" name="pwd" /></p>
<p>
灯号:
<input type="radio" name="led" value="1" />打开
<input type="radio" name="led" value="0" />关闭
</p>
<input type="Submit" value="登录" />
</form>
</center>
</body>
</html>
"""
def CreatNetwork(ssid,pwd):
ap_if = network.WLAN(network.AP_IF)#AP 模式
ap_if.active(True)
ap_if.config(essid=ssid,password=pwd)
return ap_if.ifconfig()[0]
def main(ip):
s = socket.socket()
ai = socket.getaddrinfo(ip, 80)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://",ip,":80/")
#默认的登录用户
username='xiaohai'
userpwd='123456'
ow = onewire.OneWire(Pin(2)) #Init wire
try :#####################################这里这里初始化
a=Ds18b20_test(ow)
tmp=a.read_data()
except:
print('1')
while True:
try :#############################这里这里开始读
tmp=a.read_data()
except:
tmp=1
res = s.accept()
client_s = res[0]
client_addr = res[1]
req =client_s.readline()
while True:
h = client_s.readline()
if h == b"" or h == b"\r\n":
break
req+=(h.decode('utf-8').lower())
print("Request:")
s_data=login_html#默认页面为登录页面
req=req.decode('utf-8').lower().split('\r\n')
#http header 解析
req_data=req[0].lstrip().rstrip().replace(' ','')
print(req_data)
if req_data.find('favicon.ico')>-1:
client_s.send(s_data)
client_s.close()
continue
else:
if len(req_data)>12:
print('-----',req_data)
req_data=req_data.replace('get/?','').replace('http/1.1','')
_name=req_data.find('name')
_pwd=req_data.find('pwd')
print('++++++++',_name,_pwd,req_data)
if _name>-1 and _pwd>-1:
#判断是否是用户登录
if req_data.find(username)>-1 and req_data.find(userpwd)>-1:
if req_data.find('led=1')>-1:
tmp=str(tmp)#############这里这里
htmlr="<html><html><body><h1>获取到传感器数据:%s </h1></body></html></html>"%str(tmp)############这里这里
s_data=htmlr
p0=Pin(0,Pin.OUT)
p0.value (0)
print('Login Success!')
elif req_data.find('led=0')>-1:
s_data=html
p0=Pin(0,Pin.OUT)
p0.value (1)
print('Login Success!')
else:
s_data=login_html
pass
client_s.send(s_data)
client_s.close()
from ds18b20test import Ds18b20_test
import onewire
myip=CreatNetwork('8266x18b20','139368')#建立一个AP
main(myip)#搭建web服务器