【发布时间】:2020-08-18 21:57:46
【问题描述】:
我有一个 arduino 程序和设置,可以检测停车场的汽车数量。每次传感器检测到有车辆在路障前时,都会在序列号上打印车辆数量。 我想将停车场的汽车数量打印到一个小型网络应用程序中。我使用 Tera Term 扫描我的串行总线并将输出数据放入文件文本 (data.txt) 中。然后我使用 python 从该文本文件中读取值并将其呈现到 Web 应用程序上的 HTML 页面中。
这里是python代码:
from flask import Flask, render_template
import logging
import requests
# Initialize the Flask application
app = Flask(__name__)
# Define a route for the default URL, which loads the form
@app.route("/")
def form():
with open('date.txt') as f:
data = []
lines = f.read().splitlines()
data.append(lines)
for i in data:
return render_template('index.html', variable=data)
if __name__ == "__main__":
app.run()
这里是 index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Arduino Project</title>
<style>
body{
background: antiquewhite;
}
h1 {color:red;
text-align: center;}
h2 {color:blue;
text-align: center;}
</style>
</head>
<body>
<h1> - Arduino - </h1>
<h2> Number of cars in the parking place: {{ variable }}<br></h2>
<script> setTimeout(function(){
window.location.reload(1);
}, 5000);
</script>
</body>
</html>
它工作正常,但我只想有一个每 5 秒更新一次的变量,当页面刷新时。我不想看到 date.txt 中的所有值,只是最后一个。
到目前为止,这是我的网络应用程序在这段代码中的样子: (忽略错误信息) enter image description here
【问题讨论】:
标签: python html flask server arduino