【发布时间】:2021-10-19 15:44:05
【问题描述】:
我正在使用 Raspberry Pi、超声波传感器和伺服电机构建一个 DIY 声纳,到目前为止,我已经设法让它在每次 180 度扫描时生成一张图片/地图,但问题是我也想在本地网页上显示这张图片,我想让它在每次扫描时自动更新,而无需用户手动刷新页面(每次扫描的持续时间与前一次不同 - 这是一个问题)。我曾想过使用 BottlePy 微型 Web 框架以及一些 JSON。代码如下:
Python:
import pigpio
import time
import RPi.GPIO as GPIO
import numpy as np
import cv2
import math
from PIL import Image as im
import bottle
from bottle import route, run, template, BaseTemplate, static_file
app = bottle.default_app()
BaseTemplate.defaults['get_url'] = app.get_url
@route('/')
def index():
return template('index.html')
@route('/<filename:path>', name='static')
def serve_static(filename):
return static_file(filename, root='static')
@route('/refresh')
def refresh():
...
#matrix generation for map - newmapp
...
data=im.fromarray(newmapp)
data.save('/home/pi/Desktop/servo_web/static/map.png')
run(host='localhost', port=8080)
HTML/JS:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<title>HARTA</title>
</head>
<body>
<img src="{{ get_url('static', filename='map.png') }}" />
<script>
$(document).ready(function(){
setInterval(refreshFunction,300);
});
function refreshFunction(){
$.getJSON('/refresh', function(){
});
}
</script>
</body>
</html>
提前致谢!
【问题讨论】:
-
如果您使用 JavaScript 将 url 更改为图像,那么浏览器将重新加载图像。您可以将随机参数添加到 url,如
map.png?random_value。某些系统使用当前时间重新加载image或css文件 - 比如map.png?2021.08.17.20.49.43(year.month.day.hour.minut.second)
标签: javascript python html bottle