Skip to content

Instantly share code, notes, and snippets.

@tista3
Created December 7, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tista3/e300166539d912b0e4d4ecce4014d0fd to your computer and use it in GitHub Desktop.
Save tista3/e300166539d912b0e4d4ecce4014d0fd to your computer and use it in GitHub Desktop.
intervalometer ESP32 using picoweb
# Dependencies
import upip
upip.install('picoweb')
# Save web.py on device
# Run
import web
webstart(host="0.0.0.0", port=80, debug=True)
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