Version 0.9, mostly works, REST API for changing settings is clunky
This commit is contained in:
76
LED.py
Normal file
76
LED.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
from machine import Pin
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LEDPin=Pin(2,Pin.OUT)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def Blink(number,timeon=0.2,timeoff=0.2):
|
||||||
|
|
||||||
|
n=0
|
||||||
|
|
||||||
|
while n<number:
|
||||||
|
|
||||||
|
LEDPin.off()
|
||||||
|
|
||||||
|
sleep(timeon)
|
||||||
|
|
||||||
|
LEDPin.on()
|
||||||
|
|
||||||
|
sleep(timeoff)
|
||||||
|
|
||||||
|
n +=1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def Pulse(seconds,Speed):
|
||||||
|
|
||||||
|
LEDPin.off()
|
||||||
|
|
||||||
|
sleep(seconds)
|
||||||
|
|
||||||
|
LEDPin.on()
|
||||||
|
|
||||||
|
sleep (0.1*Speed)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def Morse(text,Speed=1):
|
||||||
|
|
||||||
|
Speed=1/Speed
|
||||||
|
|
||||||
|
Dot=0.1*Speed
|
||||||
|
|
||||||
|
Dash=0.3*Speed
|
||||||
|
|
||||||
|
SpaceInLetter=0.1*Speed
|
||||||
|
|
||||||
|
SpaceBetweenLetters=0.3*Speed
|
||||||
|
|
||||||
|
Space=0.7*Speed
|
||||||
|
|
||||||
|
alphabet={' ':' ',
|
||||||
|
|
||||||
|
'a':'.-',
|
||||||
|
|
||||||
|
'b':'-...',
|
||||||
|
|
||||||
|
'c':'-.-.',
|
||||||
|
|
||||||
|
'd':'-..',
|
||||||
|
|
||||||
|
'e':'.',
|
||||||
|
|
||||||
|
'f':'..-.',
|
||||||
|
|
||||||
|
'g':'--.',
|
||||||
|
|
||||||
|
'h':'....',
|
||||||
|
|
||||||
|
'i':'..',
|
||||||
|
|
||||||
|
'j':'.---',
|
||||||
|
|
||||||
18
boot.py
18
boot.py
@@ -1,9 +1,9 @@
|
|||||||
# This file is executed on every boot (including wake-boot from deepsleep)
|
# This file is executed on every boot (including wake-boot from deepsleep)
|
||||||
#import esp
|
import esp
|
||||||
#esp.osdebug(None)
|
#esp.osdebug(None)
|
||||||
import uos, machine
|
import uos, machine
|
||||||
#uos.dupterm(None, 1) # disable REPL on UART(0)
|
#uos.dupterm(None, 1) # disable REPL on UART(0)
|
||||||
import gc
|
import gc
|
||||||
#import webrepl
|
#import webrepl
|
||||||
#webrepl.start()
|
#webrepl.start()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
128
garage.py
128
garage.py
@@ -1,32 +1,96 @@
|
|||||||
import tinyweb
|
import tinyweb
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from time import sleep
|
from machine import reset
|
||||||
|
from time import sleep
|
||||||
# Create web server application
|
import json
|
||||||
app = tinyweb.webserver()
|
|
||||||
Button=Pin(2,Pin.OUT)
|
settings=json.loads(''.join(open('settings.json').readlines()))
|
||||||
Button.on()
|
|
||||||
|
# Create web server application
|
||||||
# Index page
|
app = tinyweb.webserver()
|
||||||
@app.route('/')
|
Button={}
|
||||||
async def index(request, response):
|
for name in settings["buttons"].keys():
|
||||||
# Start HTTP response with content-type text/html
|
Button[name]=Pin(settings["buttons"][name]["pin"],Pin.OUT)
|
||||||
await response.start_html()
|
Button[name].off()
|
||||||
# Send actual HTML page
|
|
||||||
await response.send('<html><body><h1>Hi George!</h1><a href="button/1">Push the garage door button</a></html>\n')
|
# Index page
|
||||||
|
@app.route('/')
|
||||||
|
async def index(request, response):
|
||||||
# Another one, more complicated page
|
# Start HTTP response with content-type text/html
|
||||||
@app.route('/button/<nr>')
|
await response.start_html()
|
||||||
async def table(request, response,nr):
|
# Send actual HTML page
|
||||||
# Start HTTP response with content-type text/html
|
await response.send('''<html><head>
|
||||||
Button.off()
|
<style>.button { display: inline-block; border-radius: 4px; background-color: #00001e; border: none; color: #FFFFFF; text-align: center; font-size: 28px; padding: 20px; width: 200px; transition: all 0.5s; cursor: pointer; margin: 5px;}</style>
|
||||||
sleep(1)
|
</head><body><h1>George\'s Garage</h1>
|
||||||
Button.on()
|
<a href="button/1"><button class="button">1</button></a>
|
||||||
await response.start_html()
|
<a href="button/2"><button class="button">2</button></a>
|
||||||
await response.send('<html><body><h1>Hi George!</h1>Button number %s pushed. <a href="/">Go back.</a> <a href="button">Or push it again.</a></html>\n'%nr)
|
<a href="button/3"><button class="button">3</button></a>
|
||||||
|
</html>\n''')
|
||||||
|
|
||||||
def run():
|
@app.route('/reset')
|
||||||
print ("running app")
|
async def index(request, response):
|
||||||
app.run(host='0.0.0.0', port=80)
|
# Start HTTP response with content-type text/html
|
||||||
|
await response.start_html()
|
||||||
|
# Send actual HTML page
|
||||||
|
await response.send('''<html><head>
|
||||||
|
<style>.button { display: inline-block; border-radius: 4px; background-color: #00001e; border: none; color: #FFFFFF; text-align: center; font-size: 28px; padding: 20px; width: 200px; transition: all 0.5s; cursor: pointer; margin: 5px;}</style>
|
||||||
|
</head><body><h1>|RESETTING...</h1>
|
||||||
|
</html>\n''')
|
||||||
|
reset()
|
||||||
|
|
||||||
|
@app.route('/button/<nr>')
|
||||||
|
async def button(request, response,nr):
|
||||||
|
# Start HTTP response with content-type text/html
|
||||||
|
Button[nr].on()
|
||||||
|
sleep(settings['buttons'][nr]['delay'])
|
||||||
|
Button[str(nr)].off()
|
||||||
|
print ('button %s (Pin %s) pressed for %s seconds'%(nr,Button[nr],settings['buttons'][nr]['delay']))
|
||||||
|
await response.start_html()
|
||||||
|
await response.send('<html><head><meta http-equiv="refresh" content="0;url=/" /></head><body><h1>Hi George!</h1>Button number %s pushed for %s seconds. <a href="/">Go back.</a></html>\n'%(nr,settings['buttons'][str(nr)]['delay']))
|
||||||
|
|
||||||
|
def Write_Settings():
|
||||||
|
File=open('settings.json','w')
|
||||||
|
File.write(json.dumps(settings))
|
||||||
|
File.close()
|
||||||
|
|
||||||
|
#@app.route('/config',save_headers=[])
|
||||||
|
class config():
|
||||||
|
def get(self,data):
|
||||||
|
SanitizedSettings=settings.copy()
|
||||||
|
if 'password' in SanitizedSettings:
|
||||||
|
SanitizedSettings['password']='*not displayed*'
|
||||||
|
if 'AP-password' in SanitizedSettings:
|
||||||
|
SanitizedSettings['AP-password']='*not displayed*'
|
||||||
|
return SanitizedSettings
|
||||||
|
def post(self, data):
|
||||||
|
print (data)
|
||||||
|
for Setting in data.keys():
|
||||||
|
settings[Setting]=data[Setting]
|
||||||
|
print("Setting %s to %s"%(Setting,data[Setting]))
|
||||||
|
Write_Settings()
|
||||||
|
return data
|
||||||
|
|
||||||
|
def delete(self,data):
|
||||||
|
for Setting in data.keys():
|
||||||
|
settings.pop(Setting)
|
||||||
|
Write_Settings()
|
||||||
|
return data
|
||||||
|
|
||||||
|
#await response.start_html()
|
||||||
|
# print (request.query_string)
|
||||||
|
# a=tinyweb.server.parse_query_string(request.query_string)
|
||||||
|
# print (a)
|
||||||
|
# configs=str(a)
|
||||||
|
# await response.send(configs)
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
print ("running app")
|
||||||
|
app.add_resource(config,"/config")
|
||||||
|
app.run(host='0.0.0.0', port=80)
|
||||||
|
|
||||||
|
#
|
||||||
|
# form='''<html><head></head><body><h1>Settings</h1><form method='post'><table>'''
|
||||||
|
# form += "<tr><td>AP-ssid</td><td><input type='text' name='AP-ssid'>%s</input></td></tr>"%settings['AP-ssid']
|
||||||
|
# form +="</table><input type='submit'></body></html>"
|
||||||
|
# return form
|
||||||
95
main.py
95
main.py
@@ -1,42 +1,53 @@
|
|||||||
import network
|
import network
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
from machine import Pin
|
||||||
settings=json.loads(''.join(open('settings.json').readlines()))
|
import LED
|
||||||
|
import garage
|
||||||
def netstop():
|
|
||||||
n=network.WLAN(network.STA_IF)
|
#import settings
|
||||||
n.active(False)
|
settings=json.loads(''.join(open('settings.json').readlines()))
|
||||||
n=network.WLAN(network.AP_IF)
|
|
||||||
n.active(False)
|
def netstop():
|
||||||
|
n=network.WLAN(network.STA_IF)
|
||||||
def STA():
|
n.active(False)
|
||||||
sta=network.WLAN(network.STA_IF)
|
n=network.WLAN(network.AP_IF)
|
||||||
if sta.isconnected():
|
n.active(False)
|
||||||
return True
|
|
||||||
sta.active(True)
|
def STA():
|
||||||
if ('NetMask') in settings and 'DNS' in settings and 'Gateway' in settings and 'IP' in settings:
|
"""Connects Board as Client to WiFi-Network. LED.Blinks 5 times short if successful"""
|
||||||
print("Static configuration active")
|
sta=network.WLAN(network.STA_IF)
|
||||||
sta.ifconfig((settings["IP"],settings["NetMask"],settings["Gateway"],settings["DNS"]))
|
if sta.isconnected():
|
||||||
else:
|
LED.Blink(5)
|
||||||
print("DHCP active")
|
return True
|
||||||
sta.connect(settings['ssid'],settings['password'])
|
sta.active(True)
|
||||||
timeout=0
|
if ('NetMask') in settings and 'DNS' in settings and 'Gateway' in settings and 'IP' in settings:
|
||||||
while timeout < 60:
|
print("Static configuration active")
|
||||||
if sta.isconnected():
|
sta.ifconfig((settings["IP"],settings["NetMask"],settings["Gateway"],settings["DNS"]))
|
||||||
return True
|
else:
|
||||||
else:
|
print("DHCP active")
|
||||||
time.sleep(1)
|
sta.connect(settings['ssid'],settings['password'])
|
||||||
timeout += 1
|
timeout=0
|
||||||
print("Time waiting: %s sec"%timeout)
|
while timeout < 30:
|
||||||
sta.active(False)
|
if sta.isconnected():
|
||||||
return False
|
LED.Morse('ip %s'%sta.ifconfig()[0],1)
|
||||||
|
return True
|
||||||
def AP():
|
else:
|
||||||
ap=network.WLAN(network.AP_IF)
|
LED.Blink(1,.05,.95)
|
||||||
ap.active(True)
|
timeout += 1
|
||||||
ap.config(essid=settings['AP-ssid'],password=settings['AP-password'])
|
print("Time waiting: %s sec"%timeout)
|
||||||
print("AP configured")
|
sta.active(False)
|
||||||
|
return False
|
||||||
import garage
|
|
||||||
#garage.run()
|
def AP():
|
||||||
|
"""Provides Access point with settings in settings.json. LED.Blinks 3 long, 3 short on success"""
|
||||||
|
ap=network.WLAN(network.AP_IF)
|
||||||
|
ap.active(True)
|
||||||
|
ap.config(essid=settings['AP-ssid'],password=settings['AP-password'])
|
||||||
|
print("AP configured")
|
||||||
|
LED.Morse ('AP')
|
||||||
|
|
||||||
|
netstop()
|
||||||
|
if STA() != True:
|
||||||
|
AP()
|
||||||
|
garage.run()
|
||||||
|
|||||||
@@ -1,14 +1,4 @@
|
|||||||
{"password": "blueground856",
|
{"password": "blueground856",
|
||||||
|
"DNS": "8.8.8.8",
|
||||||
"AP-password": "OpenSesame2020",
|
"AP-password": "OpenSesame2020",
|
||||||
|
"buttons": {"1": {"pin": 5, "delay": 2}, "3": {"pin": 13, "delay": 2}, "2": {"pin": 4, "delay": 2}}, "AP-ssid": "Georges Garage Door Prototype", "NetMask": "255.255.255.0", "ssid": "BLUEGROUND 856", "Gateway": "192.168.1.1"}
|
||||||
"DNS": "8.8.8.8", "IP":
|
|
||||||
|
|
||||||
"192.168.1.234",
|
|
||||||
|
|
||||||
"AP-ssid": "Georges Garage Door Prototype",
|
|
||||||
|
|
||||||
"NetMask": "255.255.255.0",
|
|
||||||
|
|
||||||
"buttons": {
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user