zoukankan      html  css  js  c++  java
  • 树莓派 基于Web的温度计

    前言:家里的树莓派吃灰很久,于是拿出来做个室内温度展示也不错。

    板子是model b型。

    使用Python开发,web框架是flask,温度传感器是ds18b20

    1 硬件连接

    ds18b20的vcc连接树莓派的vcc , gnd连接gnd,DS连接GPIO4

    2 ssh登录树莓派查看ds18b20的连接

    sudo modprobe w1-gpio
    sudo modprobe w1-therm
    cd /sys/bus/w1/devices
    ls

    如果ls看不到东西,使用下面的命令

    打开/boot/config.txt 在最后一行手动添加这个:dtoverlay=w1-gpio-pullup,gpiopin=4
    

    然后 sudo reboot

    3 目录结构

    /static
    
      /js
    
        jquery.min.js
    /templates
    
      hello.html
    
    ds18b20.py
    
    hello_flask.py

    4 代码展示

    hello.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
    
        <h2>当前温度:<span id="temp"></span></h2>
    
    
        <script src="/static/js/jquery.min.js" charset="utf-8"></script>
        <script type="text/javascript">
            // 前端每10s向后台发起ajax请求,获取当前温湿度数据
            setInterval(function() {
                $.get('/update',function(data){
                    $("#temp").html(data)
                })
            },2000)
        </script>
    
    </body>
    </html>

    ds18b20.py

    import os
    import glob
    import time
    
    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')
    
    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')[0]
    device_file = device_folder + '/w1_slave'
    
    def read_temp_raw():
        f = open(device_file, 'r')
        lines = f.readlines()
        f.close()
        return lines
    
    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string) / 1000.0
            temp_f = temp_c * 9.0 / 5.0 + 32.0
            return temp_c
    
    # while True:
    #     print(read_temp())
    #     time.sleep(1)

    hello_flask.py

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from flask import Flask,render_template
    import ds18b20
    tmp = 0.0
    app= Flask(__name__)
    @app.route('/')
    def hello():
        return render_template("hello.html")
    
    #获取最新温度
    @app.route('/update')
    def update():
        tmp = ds18b20.read_temp()
        return str(tmp)
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0",port=8080, debug=True)

    5 运行

    sudo python hello_flask.py

    打开 对应树莓派的ip:8080 查看温度

    6 后语

    原本打算使用nodejs开发的,可是在树莓派上调校gpio各种坑,还有express框架的安装也是很多问题,于是转而使用python开发了。

      

  • 相关阅读:
    [国嵌攻略][097][U-Boot新手入门]
    [国嵌攻略][070-095][Linux编程函数手册]
    自己写的切图工具(转)
    【总结整理】关于切图
    【总结整理】冯诺依曼体系结构
    【总结整理】面试需了解
    【总结整理】如何解决跨域问题
    【总结整理】WebGIS基础
    【总结整理】空间数据库基础
    【总结整理】WMS、WMTS、WFS
  • 原文地址:https://www.cnblogs.com/bhaltair/p/6250392.html
Copyright © 2011-2022 走看看