Iterating for better ch9 code.

This commit is contained in:
Danny Staple
2022-03-14 15:40:31 +00:00
parent 0925a35e1a
commit a4a6689087
4 changed files with 68 additions and 41 deletions
+6 -4
View File
@@ -11,7 +11,9 @@
.attr("y", 20)
.text("--");
d3.json("sensors").then(function(data) {
d3.select("#left_dist").text(data['left_distance'])
});
</script>
setInterval(function() {
d3.json("sensors").then(function(data) {
d3.select("#left_dist").text(data['left_distance'])
});
}, 300);
</script>
+6 -10
View File
@@ -1,9 +1,3 @@
# .deploy/send-it.sh \
# ch-9/3-sensors/sensor_remote.py \
# ch-9/3-sensors/robot_wifi.py \
# ch-9/3-sensors/robot.py \
# ch-9/3-sensors/pio_encoder.py \
# ch-9/3-sensors/sensor.html
import json
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
@@ -18,7 +12,7 @@ app = WSGIApp()
def index(request):
with open("sensor.html") as fd:
hello_html = fd.read()
return 200, [('Content-Type',"text/html")], hello_html
return 200, [('Content-Type',"text/html")], [hello_html]
@app.route("/sensors")
def sensors(request):
@@ -27,7 +21,7 @@ def sensors(request):
}
robot.left_distance.clear_interrupt()
return 200, [('Content-Type', 'application/json')], json.dumps({sensor_data})
return 200, [('Content-Type', 'application/json')], [json.dumps(sensor_data)]
print("Setting up wifi.")
wifi, esp = robot_wifi.connect_to_wifi()
@@ -49,13 +43,15 @@ ip_int = ".".join(str(int(n)) for n in esp.ip_address)
print(f"IP Address is {ip_int}")
while True:
try:
server.update_poll()
try:
server.update_poll()
except RuntimeError as e:
print(f"Server poll error: {type(e)}, {e}")
# background task
except:
print("Shutting down wifi on failure. resetting ESP")
wifi.reset()
raise
# Reader exercise
# add the right distance to the distance sensor remote. Consider where to position the meter for this, how to return both sensors in
# the sensors call. Don't forget to clear the interrupt to get new readings.