Skip to content

Commit

Permalink
First basic functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzzw committed Nov 7, 2018
1 parent 5a4d0dc commit 5cdeafd
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 0 deletions.
13 changes: 13 additions & 0 deletions boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import uos, machine
uos.dupterm(machine.UART(0, 115200), 1)
import gc
import webrepl
import wifi
import port_io
port_io.board_init()
wifi.do_connect()
webrepl.start()
gc.collect()
14 changes: 14 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

outputs = { 1: { 'pin': 4, 'active': 'high', 'obj' : ''}, # relay port 1
2: { 'pin': 12, 'active': 'high', 'obj' : ''}, # LED green
3: { 'pin': 13, 'active': 'high', 'obj' : ''} # LED red
}
inputs = { 1: { 'pin': 5}} # on/off switch

RELAY = 1
LED_G = 2
LED_R = 3
ON_OFF = 1

ssid = 'my_ssid'
pw = 'my_password'
Binary file added firmware/firmware-combined.bin
Binary file not shown.
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import obi_socket
75 changes: 75 additions & 0 deletions obi_socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from machine import Pin
import picoweb
import ujson
import config as cfg
import port_io
import time
import os
import gc

app = picoweb.WebApp("myApp")

def qs_parse(qs):
parameters = {}
ampersandSplit = qs.split("&")
for element in ampersandSplit:
equalSplit = element.split("=")
parameters[equalSplit[0]] = equalSplit[1]
return parameters

@app.route('/debug')
def debug_action(req, resp):
port_io.toggle_each_port()
yield from picoweb.start_response(resp)
yield from resp.awrite("Done.")

@app.route('/status')
def get_status(req, resp):
status = port_io.get_ports_status()
yield from picoweb.start_response(resp, content_type = "application/json")
yield from resp.awrite(ujson.dumps(status))

@app.route('/switch')
def switch(req, resp):
queryString = req.qs
parameters = qs_parse(queryString)
for key, val in parameters.items():
if int(key) in cfg.outputs.keys():
print("INFO: switching port {} to {}".format(key, val))
if val == 'on':
port_io.set_output(int(key), 1)
elif val == 'off':
port_io.set_output(int(key), 0)
status = port_io.get_ports_status()
yield from picoweb.start_response(resp, content_type = "application/json")
yield from resp.awrite(ujson.dumps(status))

@app.route('/toggle')
def toggle(req, resp):
queryString = req.qs
parameters = qs_parse(queryString)
for key, val in parameters.items():
if int(key) in cfg.outputs.keys():
print("INFO: toggling port {} for {} seconds".format(key, val))
port_io.toggle_output(int(key))
time.sleep(float(val))
port_io.toggle_output(int(key))
status = port_io.get_ports_status()
yield from picoweb.start_response(resp, content_type = "application/json")
yield from resp.awrite(ujson.dumps(status))

@app.route("/")
def index(req, resp):
method = req.method
print("Method was:" + method)
if method == "POST":
yield from picoweb.start_response(resp)
yield from resp.awrite("POST method incoming")
else:
yield from picoweb.start_response(resp)
yield from resp.awrite("Hi, this is {} <br />".format(os.uname()[0]))
yield from resp.awrite("Version: {} <br />".format(os.uname()[3]))
yield from resp.awrite("{} bytes free".format(gc.mem_free()))

port_io.blink_led()
app.run(debug=True, port = 80, host = '0.0.0.0')
74 changes: 74 additions & 0 deletions port_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from machine import Pin
import config as cfg
import time

def toggle_output(port_id):
set_output(port_id, not get_output(port_id))

def set_output(port_id, value):
v = translate_port_level(port_id, value)
cfg.outputs[port_id]['obj'].value(v)

def get_output(port_id):
value = cfg.outputs[port_id]['obj'].value()
v = translate_port_level(port_id, value)
return v

# translates a logical high = "do something" to physical logic level
def translate_port_level(port_id, value):
if value == 0:
if cfg.outputs[port_id]['active'] == 'high':
v = 0 # high active 0
else:
v = 1 # low active 0
else:
if cfg.outputs[port_id]['active'] == 'high':
v = 1 # high active 1
else:
v = 0 # low active 1
return v

def get_ports_status():
status = dict()
for port in range (1, len(cfg.outputs) + 1):
status[port] = get_output(port)
return status

def board_init():
# set output ports
for port, pcfg in cfg.outputs.items():
print("INFO: Setting up GPIO {} on pin {}".format(port, pcfg['pin']))
if pcfg['active'] == 'high':
# init high active ports with 0
pcfg['obj'] = Pin(pcfg['pin'], Pin.OUT, value=0)
else:
# init low active ports with 1
pcfg['obj'] = Pin(pcfg['pin'], Pin.OUT, value=1)
# setup input port
on_off = Pin(cfg.inputs[cfg.ON_OFF]['pin'], Pin.IN, Pin.PULL_UP)
on_off.irq(trigger = Pin.IRQ_FALLING, handler = toggle_on_off)

def toggle_on_off(_):
# hack to disable interrupts
on_off = Pin(cfg.inputs[cfg.ON_OFF]['pin'], Pin.IN, Pin.PULL_UP)
on_off.irq(trigger = 0, handler = toggle_on_off)
print("INFO: Button pressed.")
toggle_output(cfg.LED_R)
toggle_output(cfg.RELAY)
# debounce time
time.sleep(0.5)
on_off.irq(trigger = Pin.IRQ_FALLING, handler = toggle_on_off)


def blink_led():
for i in range(0,20):
toggle_output(cfg.LED_R)
time.sleep(0.03)

def toggle_each_port():
for port in range (1, len(cfg.outputs) + 1):
cfg.outputs[port]['obj'].value(0)
time.sleep(0.2)
for port in range (1, len(cfg.outputs) + 1):
cfg.outputs[port]['obj'].value(1)
time.sleep(0.2)
14 changes: 14 additions & 0 deletions wifi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import config as cfg
import port_io

def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(cfg.ssid, cfg.pw)
while not wlan.isconnected():
pass
port_io.set_output(cfg.LED_G, 1)
print('network config:', wlan.ifconfig())

0 comments on commit 5cdeafd

Please sign in to comment.