I am building a DIY sonar using a Raspberry Pi, an ultrasonic sensor and a servo motor and so far I've managed to make it generate a picture/map on each 180 degree sweep but the problem is that I also want to show this picture on a local webpage and I want to make it auto-update on each sweep without the user having to manually refresh the page (each sweep does not last the exact same as the previous one - that is one problem). I have thought of using the BottlePy micro web framework as well as some jSON. Here is the code:
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>
Thanks in advance!