Last active
January 3, 2017 23:47
-
-
Save betatim/3893685cff1c56b992deff3523f0e413 to your computer and use it in GitHub Desktop.
Basics for a wifi enabled speedometer for your bike π². Designed for the ESP8266.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import array | |
import machine | |
import micropython | |
import socket | |
import time | |
html = """<!DOCTYPE html><html><head><meta http-equiv="refresh" content="5"></head><body><h1>Speed</h1>%s</body></html>""" | |
micropython.alloc_emergency_exception_buf(100) | |
index = 0 | |
ticks = array.array('i', [0] * 10) | |
p = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP) | |
def cb(p): | |
global ticks | |
global index | |
if p.value(): | |
ticks[index] = time.ticks_us() | |
index = (index + 1) % 10 | |
irq = p.irq(trigger=machine.Pin.IRQ_RISING, handler=cb) | |
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] | |
s = socket.socket() | |
s.bind(addr) | |
s.listen(1) | |
print('listening on', addr) | |
while True: | |
cl, addr = s.accept() | |
irq.trigger(False) | |
vals = list(ticks) | |
i = index - 1 | |
then, now = vals[i-1], vals[i] | |
cl_file = cl.makefile('rwb', 0) | |
while True: | |
line = cl_file.readline() | |
if not line or line == b'\r\n': | |
break | |
deltaT = (now - then) / 1000000 | |
txt = "deltaT: %ss freq: %sHz" % (deltaT, 1/deltaT) | |
cl.send(html % txt) | |
cl.close() | |
irq.trigger(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment