[Micropython]TPYBoard v202 v102+v202 家庭无线温湿度检测
一、实验器件
- 1、TPYBoard v102 1块
- 2、TPYBoard v202 1块
- 3、Nokia 5110LCD显示屏 1块
- 4、DHT11温湿度传感器 1个
- 5、micro USB 数据线 2根
- 6、面包板 1块
- 7、杜邦线 若干
- 1、TPYBoard v102连接DHT11,采集温湿度信息;
- 2、TPYBoard v102将采集到的温湿度信息显示在LCD5110上;
- 3、TPYBoard v102通过串口将温湿度信息发送给TPYBoard v202;
- 4、TPYBoard v202 将串口的数据读取出来,同时传递到服务器上。
TPYBoard v102 源代码TPYBoard v102 main.py文件内容如下:#main.py
import pyb
import upcd8544
from machine import SPI,Pin
from dht11 import DHT11
def main(lcd_5110,dht,uart6):
data_=dht.read_data()
lcd_5110.lcd_write_string(‘ ‘,0,1)#添加一个分隔行
lcd_5110.lcd_write_string(‘Temp:’+str(data_[0]),2,2)
lcd_5110.lcd_write_string(‘ ‘,0,3)
lcd_5110.lcd_write_string(‘ Hum:’+str(data_[1]),2,4)
uart6.write(str(data_[0])+’,’+str(data_[1]))#通过串口将数据发送给v202
if __name__ == ‘__main__’:
#init UART
u6=pyb.UART(6,115200)
#init DHT11
dht=DHT11(‘X12’)
#init LCD5110
SPI = pyb.SPI(1)
RST = pyb.Pin(‘Y11’)
CE = pyb.Pin(‘Y10’)
DC = pyb.Pin(‘Y9’)
LIGHT = pyb.Pin(‘X4’)
#DIN=>X8-MOSI/CLK=>X6-SCK
#DIN =>SPI(1).MOSI ‘X8’ data flow (Master out, Slave in)
#CLK =>SPI(1).SCK ‘X6’ SPI clock
lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
lcd_5110.lcd_write_string(‘TPYBoard v102’,1,0)
while True:
main(lcd_5110,dht,u6)
pyb.delay(2000)
TPYBoard v202 源代码只有1个main.py 文件:
TPYBoard v202 main.py文件内容,如下:
import machine
import network
import socket
from machine import Pin
from machine import UART
import time
u2=UART(0,115200)#串口初始化
led = Pin(2, Pin.OUT).value(1)#板载小蓝灯 默认关闭
def http_get(temp,hum):
url=’http://www.tpyboard.com/esp8266/SensorTest.php?t=’+temp+’&h=’+hum+”
_, _, host, path = url.split(‘/’, 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes(‘GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n’ % (path, host), ‘utf8’))
while True:
data = s.recv(50)
if data:
recive=str(data, ‘utf8’).upper()
#print(str(data, ‘utf8’), end=”)
if(recive.find(‘YES’)>-1):
print(‘Send Data OK’)
else:
break
s.close()
def do_connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print(‘connecting to network…’)
wlan.connect( ‘ssid’, ‘pwd’)
while not wlan.isconnected():
pass
print(‘network config:’, wlan.ifconfig())
do_connect()
led = Pin(2, Pin.OUT).value(0)#连接wifi成功 点亮LED
while 1:
data_=u2.readall()
if data_!=None:
data_=data_.decode(‘utf8’)#数组转成字符串
data_a=data_.split(‘,’)#分割
temp_=str(data_a[0])#温度
hum_=str(data_a[1])#湿度
http_get(temp_,hum_)#发送给服务器
time.sleep(2)服务器源码SensorData.php 文件内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>温湿度传感器实验</title>
<script type=”text/javascript”>
var t;
var te_html_str=”N/A”;
function timedCount()
{
<?php
$myfile = fopen(“sensor.txt”, “r”);
$txt =fread($myfile,filesize(“sensor.txt”));
fclose($myfile);
if($txt!=””)
{
echo “te_html_str='”.$txt.”‘;”;
}
?>
document.getElementById(‘test’).innerHTML=te_html_str;
t=setTimeout(“javascript:location=location;”,1000)
}
</script>
</head>
<body onload=”timedCount()”>
<center>
<div style=”margin-top:80px”>
<h2>TPYBoardV202_温湿度传感器实验</h2>
<div id=”test”></div>
</div>
</center>
</body>
</html>
SensorTest.php 文件内容如下:
<?php
$time_=”获取时间:”.date(‘Y-m-d H:i:s’);
$data_=”传感器数据:Sensor Error!”;
$state_=”No”;
if(is_array($_GET)&&count($_GET)>1)
{
$data_=””;
//获取温度
if(isset($_GET[“t”]))
{
$para=$_GET[“t”];
$data_.=”传感器数据:温度:”.$para.” ℃ – “;
}
//获取湿度
if(isset($_GET[“h”]))
{
$para=$_GET[“h”];
$data_.=”湿度:”.$para.” % “;
$state_=”Yes”;
}
}
$myfile = fopen(“sensor.txt”, “w”);
$txt = $time_.”<br /><br />”.$data_;
fwrite($myfile, $txt);
fclose($myfile);
echo $state_;
?>