from machine import I2C, Pin, Timer, RTC from network import STA_IF, WLAN import ssd1306 import time import utime from mpu6050 import accel import picoweb screen = {} screen['rst'] = Pin(16, Pin.OUT) screen['rst'].value(1) screen['scl'] = Pin(15, Pin.OUT, Pin.PULL_UP) screen['sda'] = Pin(4, Pin.OUT, Pin.PULL_UP) screen['i2c'] = I2C(scl=screen['scl'], sda=screen['sda'], freq=450000) oled = ssd1306.SSD1306_I2C(128, 64, screen['i2c'], addr=0x3c) oled.fill(0) oled.text('Initializing',5,5) oled.show() mpu = {} mpu['scl'] = Pin(19, Pin.OUT, Pin.PULL_UP) mpu['sda'] = Pin(23, Pin.OUT, Pin.PULL_UP) mpu['i2c'] = I2C(scl=mpu['scl'], sda=mpu['sda']) accelerometer = accel(mpu['i2c']) def date_time(): (y,mo,d,wd,h,mi,s,*v) = RTC().datetime() (y,mo,d,h,mi,s,wd,yd) = utime.localtime(utime.mktime((y,mo,d,h,mi,s,wd,0))-4*3600) return ( '%02d/%02d/%02d' % (mo,d,y-2000), '%02d:%02d:%02d' % (h,mi,s) ) def ip(): return WLAN(STA_IF).ifconfig()[0] last_pos = 'UN' def position(x,y,z): global last_pos if abs(z) > 8192 and abs(y) < 4096 and abs(x) < 4096: last_pos = 'TOP' if z > 0 else 'BOT' if abs(y) > 8192 and abs(z) < 4096 and abs(x) < 4096: last_pos = 'RGT' if y > 0 else 'LFT' if abs(x) > 8192 and abs(z) < 4096 and abs(y) < 4096: last_pos = 'FRO' if x > 0 else 'BAK' return last_pos def build_data(arg): [x,y,z] = [arg[idx] for idx in ['AcX','AcY','AcZ']] (date, time) = date_time() return { 'x': x, 'y': y, 'z': z, 'date': date, 'time': time, 'ip': ip(), 'position': position(x,y,z) } def output_positions(data): oled.fill(0) oled.text('X: %(x)+05d' % data, 5, 5) oled.text('Y: %(y)+05d' % data, 5, 15) oled.text('Z: %(z)+05d' % data, 5, 25) oled.text(data['date'],5,35) oled.text(data['position'],80,35) oled.text(data['time'],5,45) oled.text(data['ip'],5,55) oled.show() current_data = {} def run_cycle(): global current_data current_data = build_data(accelerometer.get_values()) output_positions(current_data) timer = Timer(0) timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:run_cycle()) app = picoweb.WebApp('main') @app.route("/") def index(req,resp): yield from picoweb.start_response(resp) yield from resp.awrite("""
X%(x)+05d
Y%(y)+05d
Z%(z)+05d
Position%(position)s
""" % current_data) app.run(debug=True,host=ip(),port=80)