小小的森 发表于 2018-9-30 15:26:40

TPYBoard v102+v202 家庭无线温湿度检测

一、实验器件

    1、TPYBoard v102 1块
    2、TPYBoard v202 1块
    3、Nokia 5110LCD显示屏 1块
    4、DHT11温湿度传感器 1个
    5、micro USB 数据线 2根
    6、面包板 1块
    7、杜邦线 若干

   二、实验步骤

    TPYBoard v102连接DHT11,采集温湿度信息;
    TPYBoard v102将采集到的温湿度信息显示在LCD5110上;
    TPYBoard v102通过串口将温湿度信息发送给TPYBoard v202;
    TPYBoard v202 将串口的数据读取出来,同时传递到服务器上。

    三、实验方法

    1、Nokia 5110 LCD显示屏说明

    LCD5110是一款84x48 的点阵LCD,可以显示4 行汉字。采用串行接口与主处理器进行通信,支持多种串行通信协议,传输速率高达4Mbps,可全速写入显示数据。

http://c.51hei.com/d/forum/201809/30/144250ocnf9fi7icxis23c.jpg
    Nokia 5110 LCD共有8个引脚,不同的板子可能标注的英文略有不同,具体参照如下:

http://c.51hei.com/d/forum/201809/30/144250x54b1yk4tgyfxypz.png
    2、DHT11温湿度说明

    DHT11是一款有已校准数字信号输出的温湿度传感器。 精度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃。

http://c.51hei.com/d/forum/201809/30/144250rhbhyhfukuhybujy.jpg
    DHT11温湿度共有3个引脚,具体参照如下:

http://c.51hei.com/d/forum/201809/30/144251i8x6xdn1urkh8nnk.png
    四、接线方式
    1、针脚对应图

    DHT11和LCD5110的使用方法上面都介绍过了,接下来,就要动手和我们的TPYBoard v102接起来了。

    TPYBoard v102连接LCD5110

http://c.51hei.com/d/forum/201809/30/144251ikdt792klj925y44.png
    TPYBoard v102连接DHT11

http://c.51hei.com/d/forum/201809/30/144251am1z44t6661o468y.png
    TPYBoard v102连接TPYBoard v202

http://c.51hei.com/d/forum/201809/30/144251n3gsmogzjqqtjosn.png
    2、实物连接图
http://c.51hei.com/d/forum/201809/30/144251hsedkyrryw8vre6s.jpg


http://c.51hei.com/d/forum/201809/30/144251mmsi151fzfi15ei2.jpg
   四、源代码
    TPYBoard v102 源代码

http://c.51hei.com/d/forum/201809/30/144251kq81pzlqvniyy7tq.png
    TPYBoard v102 main.py文件内容,如下:

#main.pyimportpybimportupcd8544frommachineimportSPI,Pinfromdht11importDHT11defmain(lcd_5110,dht,uart6):    data_=dht.read_data()    lcd_5110.lcd_write_string(' ',0,1)#添加一个分隔行   lcd_5110.lcd_write_string('Temp:'+str(data_),2,2)    lcd_5110.lcd_write_string(' ',0,3)    lcd_5110.lcd_write_string(' Hum:'+str(data_),2,4)   uart6.write(str(data_)+','+str(data_))#通过串口将数据发送给v202if__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('TPYBoardv102',1,0)    whileTrue:      main(lcd_5110,dht,u6)      pyb.delay(2000)
    *其他文件详见最下方压缩包*

    TPYBoard v202 源代码

    只有1个main.py 文件TPYBoard v202 main.py文件内容,如下:
importmachineimportnetworkimportsocketfrommachineimportPinfrommachineimportUARTimporttimeu2=UART(0,115200)#串口初始化led =Pin(2, Pin.OUT).value(1)#板载小蓝灯 默认关闭defhttp_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)[-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'))    whileTrue:      data = s.recv(50)      ifdata:            recive=str(data, 'utf8').upper()            #print(str(data, 'utf8'), end='')            if(recive.find('YES')>-1):               print('Send Data OK')      else:            break    s.close()defdo_connect():    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    ifnot 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成功 点亮LEDwhile 1:    data_=u2.readall()    if data_!=None:      data_=data_.decode('utf8')#数组转成字符串      data_a=data_.split(',')#分割      temp_=str(data_a)#温度      hum_=str(data_a)#湿度      http_get(temp_,hum_)#发送给服务器    time.sleep(2)
    服务器源码

    SensorData.php 文件内容如下:

<!DOCTYPEhtml><html><head><metacharset=\"utf-8\"><title>温湿度传感器实验</title><scripttype=\"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><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_.\"

\".$data_;    fwrite($myfile, $txt);    fclose($myfile);    echo$state_;?>

    五、效果图

   实物效果图

http://c.51hei.com/d/forum/201809/30/144252tkhjjqnqyj0j0jjp.jpg
    网页效果图
http://c.51hei.com/d/forum/201809/30/144252l5fbyh2x6v068chl.png
页: [1]
查看完整版本: TPYBoard v102+v202 家庭无线温湿度检测