1
Fork 0
cruisecontrol/python-controller/main.py
2024-09-28 11:47:21 -04:00

64 lines
1.2 KiB
Python

import time
import network
import socket
from machine import Pin
from machine import I2C
import VL53L0X
import MPU6050
ssid = "cruise"
password = "rust"
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid)
ap.active(True)
i2c_tof = I2C(1, scl=Pin(27), sda=Pin(26))
tof = False
try:
tof = VL53L0X.VL53L0X(i2c_tof, address=0x29)
except Exception as e:
tof = False
i2c_gyro = machine.I2C(0, sda=machine.Pin(20), scl=machine.Pin(21));
mpu = False
try:
mpu = MPU6050.MPU6050(i2c_gyro, address = 0x68)
mpu.wake()
except Exception as e:
mpu = False
while (ap.active() == False):
pass
print("network active")
print("ip: "+ap.ifconfig()[0])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 1337))
s.listen(5)
while True:
try:
cl, addr = s.accept()
request = cl.recv(1024)
print(request)
cl.send("recvd")
cl.close()
except OSError as e:
cl.close()
print("closed")
pass
while True:
if tof:
tof.start()
print(tof.read())
if mpu:
gyro = mpu.read_gyro_data()
accel = mpu.read_accel_data()
print("Gyro: " + str(gyro) + ", Accel: " + str(accel))
time.sleep(0.1)