自动刷新网页

0h4hbjxa  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(336)

这是我的代码,它检测到婴儿的位置,并插入到我的数据库,其中只有一行。我使用limit删除旧的行并保留最新的行。


# !/usr/bin/python

import RPi.GPIO as GPIO
import time
import MySQLdb

db = MySQLdb.connect("localhost", "root", "raspberry", "cribdb")

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN) #Right level-1
GPIO.setup(16, GPIO.IN) #Right level-2
GPIO.setup(18, GPIO.IN) #Right level-3

last_status = None

while True:

input12 = GPIO.input(12)

input16 = GPIO.input(16)

input18 = GPIO.input(18)

if (input12 == 1 and input16 == 0 and input18 == 0):
    status = "Lying down"
elif (input12 == 1 and input16 == 1 and input18 == 0):
    status = "Sitting"
elif (input12 == 1 and input16 == 1 and input18 == 1):
    status = "Standing"
elif (input12 == 0 and input16 == 1 and input18 == 1):
    status = "Trying to climb out"
elif (input12 == 0 and input16 == 0 and input18 == 1):
    status = "Almost out" 
else:
    status = "Out of the crib"

time.sleep(1)
if status != last_status:
    print(status)
    last_status = status
    curs = db.cursor()
    curs.execute(
    """INSERT INTO tbstatus values(NULL, %s)""", (status,)
)
db.commit()

number_of_rows= curs.execute("SELECT * FROM tbstatus")

if (number_of_rows >= 2:
    curs.execute("""DELETE FROM tbstatus order by id LIMIT 1""")
    db.commit()
    print("\n 1st row deleted ")

这是我用php编写的代码,用于显示数据库中的数据。它只需获取数据库中的状态值并将其显示在网页上。

<?php require("connection.php"); ?>
<!DOCTYPE html>
 <html>
<head>
    <title> BABY MONITORING SYSTEM </title>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div id="wrapper">

    <?php  

        $sql_cmd = "SELECT * FROM tbstatus ORDER BY id desc";

        $stmt = $con->prepare($sql_cmd);

        $stmt->execute();

        echo "<table>";
        echo "<tr>";
            echo "<td>STATUS OF THE BABY: </td>";
        echo "</tr>";

        while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {

            echo "<tr>";
                echo "<td>" . $result['status'] . "</td>";
            echo "</tr>";

        }

        echo "</table>";
    ?>

</div>

</body>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/main.js"></script>
</html>

一旦我的数据库从我的传感器得到一个新的状态,网页将如何重新加载?

xyhw6mcr

xyhw6mcr1#

我不认为每15秒刷新一次页面就是好的用户体验。为什么不使用为这种工作量身定做的服务呢。firebase实时数据库是一个非常好的选择,它允许您在应用程序中进行实时更新。而且它是由谷歌运营的,所以这肯定是件好事。
否则,您必须实现自己的服务器端脚本来为您进行更新。我要提醒你,一次又一次地刷新同一个页面,特别是在移动网络上,对你和用户来说都是昂贵的。

5t7ly7z5

5t7ly7z52#

您需要指示客户端(即浏览器)从服务器获取新数据。最简单的方法是告诉浏览器每隔一段时间刷新一次页面,例如在 <head> html的名称:

<meta http-equiv="refresh" content="15">

中的值 content 刷新之间的秒数,因此在本例中,页面将每15秒刷新一次。当然,你可以根据自己的需要来调整。
如果您需要更高级的重新加载,您将不得不使用javascript来重新加载页面或刷新页面的一部分,但是如果这是一个简单的爱好项目,那么这可能会有点过分-只是刷新可能足够有效。
编辑:编辑 head 页面的一部分将如下所示:

<head>
    <title> BABY MONITORING SYSTEM </title>
    <link rel="stylesheet" href="css/styles.css">
    <meta http-equiv="refresh" content="15">
</head>

相关问题