simple web server implemented
This commit is contained in:
parent
34072ddffa
commit
bd05b3dd02
70
main.py
70
main.py
@ -4,6 +4,7 @@ import ssd1306
|
||||
import time
|
||||
import utime
|
||||
from mpu6050 import accel
|
||||
import picoweb
|
||||
|
||||
screen = {}
|
||||
screen['rst'] = Pin(16, Pin.OUT)
|
||||
@ -26,24 +27,73 @@ 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))
|
||||
return (
|
||||
'%02d/%02d/%02d' % (mo,d,y-2000),
|
||||
'%02d:%02d:%02d' % (h,mi,s)
|
||||
)
|
||||
|
||||
def ip():
|
||||
return WLAN(STA_IF).ifconfig()[0]
|
||||
|
||||
def output_positions(arg):
|
||||
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: %(AcX)+05d' % arg, 5, 5)
|
||||
oled.text('Y: %(AcY)+05d' % arg, 5, 15)
|
||||
oled.text('Z: %(AcZ)+05d' % arg, 5, 25)
|
||||
(d,t) = date_time()
|
||||
oled.text(d,5,35)
|
||||
oled.text(t,5,45)
|
||||
oled.text(ip(),5,55)
|
||||
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():
|
||||
output_positions(accelerometer.get_values())
|
||||
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("""
|
||||
<table>
|
||||
<tr><th>X</th><td>%(x)+05d</td></tr>
|
||||
<tr><th>Y</th><td>%(y)+05d</td></tr>
|
||||
<tr><th>Z</th><td>%(z)+05d</td></tr>
|
||||
<tr><th>Position</th><td>%(position)s</td></tr>
|
||||
</table>
|
||||
""" % current_data)
|
||||
|
||||
app.run(debug=True,host=ip(),port=80)
|
||||
|
||||
|
||||
16
notes.org
16
notes.org
@ -87,6 +87,22 @@ rtc = machine.RTC()
|
||||
rtc.datetime()
|
||||
#+END_SRC
|
||||
|
||||
* Web Server
|
||||
|
||||
#+BEGIN_SRC python
|
||||
import picoweb
|
||||
from network import WLAN, STA_IF
|
||||
|
||||
app = picoweb.WebApp('main')
|
||||
|
||||
@app.route("/")
|
||||
def index(req,resp):
|
||||
yield from picoweb.start_response(resp)
|
||||
yield from resp.awrite("Hello world from picoweb running on the ESP32")
|
||||
|
||||
app.run(debug=True,host=WLAN(STA_IF).ifconfig()[0],port=80)
|
||||
#+END_SRC
|
||||
|
||||
* Connections
|
||||
|
||||
Pin 23 ESP -> SDA MPU
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user