Chapter 11 and 13 - wifi code (pre bluetooth)
This commit is contained in:
@@ -39,5 +39,4 @@
|
||||
console.log(error.name === 'AbortError');
|
||||
}
|
||||
}
|
||||
// }
|
||||
</script>
|
||||
|
||||
@@ -19,21 +19,20 @@ class RandomWalkSensors:
|
||||
self.last_time = self.start_time
|
||||
# self.app = None
|
||||
|
||||
def setup_wifi(self, app):
|
||||
# self.app = app
|
||||
print("Setting up wifi.")
|
||||
self.wifi, esp = robot_wifi.connect_to_wifi()
|
||||
def setup_server(self, esp, app):
|
||||
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
|
||||
adafruit_esp32spi_wsgiserver.set_interface(esp)
|
||||
print("Starting server")
|
||||
|
||||
self.server.start()
|
||||
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
||||
print(f"IP Address is {ip_int}")
|
||||
|
||||
def reconnect(self):
|
||||
print(f"{time.monotonic()} Resetting esp... ")
|
||||
self.wifi.reset()
|
||||
print(f"{time.monotonic()} Reconnecting wifi... ")
|
||||
self.wifi.connect()
|
||||
print(f"{time.monotonic()} starting server...")
|
||||
self.server.start()
|
||||
print(f"{time.monotonic()} started server...")
|
||||
|
||||
def data(self, request):
|
||||
|
||||
@@ -74,17 +73,21 @@ class RandomWalkSensors:
|
||||
self.server.update_poll()
|
||||
except RuntimeError as e:
|
||||
traceback.print_exception(BaseException, e, e.__traceback__)
|
||||
self.wifi.reset()
|
||||
self.reconnect()
|
||||
print("Reset complete.")
|
||||
if not "Failed to send" in str(e):
|
||||
self.reconnect()
|
||||
|
||||
def start(self):
|
||||
app = WSGIApp()
|
||||
app.route("/")(self.index)
|
||||
app.route("/data")(self.data)
|
||||
print("Starting")
|
||||
self.setup_wifi(app)
|
||||
self.main_loop()
|
||||
self.wifi, esp, spi = robot_wifi.connect_to_wifi()
|
||||
try:
|
||||
self.setup_server(esp, app)
|
||||
self.main_loop()
|
||||
finally:
|
||||
spi.unlock()
|
||||
spi.deinit()
|
||||
|
||||
|
||||
RandomWalkSensors().start()
|
||||
|
||||
@@ -18,14 +18,21 @@ def connect_to_wifi():
|
||||
|
||||
status_led = DigitalInOut(board.LED)
|
||||
status_led.switch_to_output()
|
||||
|
||||
|
||||
print("Setting up wifi.")
|
||||
spi = busio.SPI(board.GP14, MOSI=board.GP11, MISO=board.GP12)
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
esp.reset()
|
||||
print("SPI Configure")
|
||||
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset,
|
||||
baudrate=300*1000
|
||||
)
|
||||
esp.ready_timeout = 1
|
||||
|
||||
print("Firmware vers.", esp.firmware_version)
|
||||
|
||||
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
|
||||
wifi.connect()
|
||||
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
|
||||
print(f"IP Address is {ip_int}")
|
||||
|
||||
status_led.value = 1
|
||||
|
||||
return wifi, esp
|
||||
return wifi, esp, spi
|
||||
|
||||
Reference in New Issue
Block a user