Created
December 7, 2017 15:25
-
-
Save tista3/e300166539d912b0e4d4ecce4014d0fd to your computer and use it in GitHub Desktop.
intervalometer ESP32 using picoweb
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
# Dependencies | |
import upip | |
upip.install('picoweb') | |
# Save web.py on device | |
# Run | |
import web | |
webstart(host="0.0.0.0", port=80, debug=True) |
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 picoweb | |
import gc | |
import uasyncio as asyncio | |
import utime as time | |
state = { | |
"interval": 30, | |
"running": False, | |
"start": 1, | |
"last_shoot": 1 | |
} | |
app = picoweb.WebApp(__name__) | |
@app.route("/") | |
def index(req, resp): | |
yield from picoweb.start_response(resp) | |
yield from resp.awrite(''' | |
{text}, {interval} <br /> | |
<form action="/action" method=POST> | |
<input type="text" name="interval" value="{interval}"><br> | |
<input type="submit" method="post" value="{button_text}"> | |
</form>'''.format( | |
text="RUNNING" if state['running'] else "STOPPED", | |
interval=str(state['interval']), | |
button_text="START/STOP" | |
)) | |
@app.route("/action") | |
def index(req, resp): | |
yield from picoweb.start_response(resp) | |
yield from req.read_form_data() | |
state['interval'] = int(req.form['interval'][0]) | |
state['running'] = not state['running'] | |
state['start'] = int(time.time()) | |
yield from resp.awrite(''' | |
<head> | |
<meta http-equiv="refresh" content="0; url=/" /> | |
</head> | |
<body> | |
{text}, {interval} <br /> | |
<a href="/">Go back<a> | |
</body> | |
'''. format( | |
text="RUNNING" if state['running'] else "NOT/RUNNING", | |
interval=str(state['interval']), | |
)) | |
#import logging | |
#logging.basicConfig(level=logging.INFO) | |
#app.run(debug=True) | |
def on_machine(): | |
try: | |
import machine | |
return True | |
except ImportError: | |
return False | |
if on_machine(): | |
from machine import Pin | |
pin4 = Pin(4, Pin.OUT) | |
else: | |
pin4 = None | |
def pin(value): | |
global pin4 | |
if on_machine(): | |
pin4.value(value) | |
def simple_loop(): | |
while True: | |
now = int(time.time()) | |
pin(0) | |
if state['running']: | |
if state['interval'] == 0: | |
pin(1) | |
elif now % state['interval'] == 0 and state['last_shoot'] != now: | |
state['last_shoot'] = now | |
pin(1) | |
else: | |
pin(0) | |
yield from asyncio.sleep(0.1) | |
def start(**kwargs): | |
loop = asyncio.get_event_loop() | |
loop.create_task(simple_loop()) | |
app.run(**kwargs) | |
if __name__ == "__main__": | |
start(host="0.0.0.0", port=80, debug=True) | |
#start(debug=True)web.start(host="0.0.0.0", port=80, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment