Coding style enforced

This commit is contained in:
2022-05-28 12:46:31 +02:00
parent 4a5c844510
commit 7620db8abb
8 changed files with 197 additions and 125 deletions

View File

@@ -4,83 +4,98 @@ from machine import reset
from time import sleep from time import sleep
import json import json
settings=json.loads(''.join(open('settings.json').readlines())) settings = json.loads("".join(open("settings.json").readlines()))
# Create web server application # Create web server application
app = tinyweb.webserver() app = tinyweb.webserver()
# Hardware Setup, e.g. Garage Door Remote # Hardware Setup, e.g. Garage Door Remote
Button={} Button = {}
for name in settings["buttons"].keys(): for name in settings["buttons"].keys():
Button[name]=Pin(settings["buttons"][name]["pin"],Pin.OUT) Button[name] = Pin(settings["buttons"][name]["pin"], Pin.OUT)
Button[name].off() Button[name].off()
# Index page # Index page
@app.route('/') @app.route("/")
async def index(request, response): async def index(request, response):
# Start HTTP response with content-type text/html # Start HTTP response with content-type text/html
await response.start_html() await response.start_html()
# Send actual HTML page, in this example with three buttons # Send actual HTML page, in this example with three buttons
await response.send('''<html><head> 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> <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>Garage Door Remote</h1> </head><body><h1>Garage Door Remote</h1>
<a href="button/1"><button class="button">1</button></a> <a href="button/1"><button class="button">1</button></a>
<a href="button/2"><button class="button">2</button></a> <a href="button/2"><button class="button">2</button></a>
<a href="button/3"><button class="button">3</button></a> <a href="button/3"><button class="button">3</button></a>
</html>\n''') </html>\n"""
)
@app.route('/reset')
@app.route("/reset")
async def index(request, response): async def index(request, response):
await response.start_html() await response.start_html()
await response.send('''<html><head> 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> <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> </head><body><h1>RESETTING...</h1>
</html>\n''') </html>\n"""
)
reset() reset()
@app.route('/button/<nr>')
async def button(request, response,nr): @app.route("/button/<nr>")
async def button(request, response, nr):
# Start HTTP response with content-type text/html # Start HTTP response with content-type text/html
# Machine control # Machine control
Button[nr].on() Button[nr].on()
sleep(settings['buttons'][nr]['delay']) sleep(settings["buttons"][nr]["delay"])
Button[str(nr)].off() Button[str(nr)].off()
# Debug string # Debug string
print ('button %s (Pin %s) pressed for %s seconds'%(nr,Button[nr],settings['buttons'][nr]['delay'])) print(
#Send response "button %s (Pin %s) pressed for %s seconds"
% (nr, Button[nr], settings["buttons"][nr]["delay"])
)
# Send response
await response.start_html() 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'])) 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(): def Write_Settings():
File=open('settings.json','w') File = open("settings.json", "w")
File.write(json.dumps(settings)) File.write(json.dumps(settings))
File.close() File.close()
#@app.route('/config',save_headers=[])
class config(): # @app.route('/config',save_headers=[])
def get(self,data): class config:
SanitizedSettings=settings.copy() def get(self, data):
if 'password' in SanitizedSettings: SanitizedSettings = settings.copy()
SanitizedSettings['password']='*not displayed*' if "password" in SanitizedSettings:
if 'AP-password' in SanitizedSettings: SanitizedSettings["password"] = "*not displayed*"
SanitizedSettings['AP-password']='*not displayed*' if "AP-password" in SanitizedSettings:
SanitizedSettings["AP-password"] = "*not displayed*"
return SanitizedSettings return SanitizedSettings
def post(self, data): def post(self, data):
print (data) print(data)
for Setting in data.keys(): for Setting in data.keys():
settings[Setting]=data[Setting] settings[Setting] = data[Setting]
print("Setting %s to %s"%(Setting,data[Setting])) print("Setting %s to %s" % (Setting, data[Setting]))
Write_Settings() Write_Settings()
return data return data
def delete(self,data): def delete(self, data):
for Setting in data.keys(): for Setting in data.keys():
settings.pop(Setting) settings.pop(Setting)
Write_Settings() Write_Settings()
return data return data
def run(): def run():
print ("running app") print("running app")
app.add_resource(config,"/config") app.add_resource(config, "/config")
app.run(host='0.0.0.0', port=80) app.run(host="0.0.0.0", port=80)

View File

@@ -1,29 +1,28 @@
from time import sleep from time import sleep
import os import os
def DoubleReset(Time=5,File="doublereset.txt"):
"""Use this at the beginning of the file. def DoubleReset(Time=5, File="doublereset.txt"):
DoubleReset(Time=5,File="doublereset.txt") """Use this at the beginning of the file.
Determines, if the board had two successive resets in the last <n> seconds. DoubleReset(Time=5,File="doublereset.txt")
This is done by writing a file in <File> and deleting it after <n> seconds. Determines, if the board had two successive resets in the last <n> seconds.
If the file is already there, the reset was doubled.""" This is done by writing a file in <File> and deleting it after <n> seconds.
try: If the file is already there, the reset was doubled."""
a=open(File) try:
Check=a.read().strip() a = open(File)
a.close() Check = a.read().strip()
a=open(File,"w") a.close()
a.write(str(int(Check)+1)) a = open(File, "w")
a.close() a.write(str(int(Check) + 1))
sleep(Time) a.close()
os.remove(File) sleep(Time)
return int(Check)+1 os.remove(File)
except OSError: return int(Check) + 1
pass except OSError:
a=open(File,"w") pass
a.write("1") a = open(File, "w")
a.close() a.write("1")
sleep(Time) a.close()
os.remove(File) sleep(Time)
return False os.remove(File)
return False

View File

@@ -1,49 +1,85 @@
from machine import Pin from machine import Pin
from time import sleep from time import sleep
class LED: class LED:
"""Define a LED on a defined pin""" """Define a LED on a defined pin"""
def __init__(self,LEDPin=2):
self.LEDPin=Pin(LEDPin,Pin.OUT) def __init__(self, LEDPin=2):
print ("New LED defined on Pin %s"%LEDPin) self.LEDPin = Pin(LEDPin, Pin.OUT)
def Blink(self,number,timeon=0.2,timeoff=0.2): print("New LED defined on Pin %s" % LEDPin)
def Blink(self, number, timeon=0.2, timeoff=0.2):
"""Blinks the LED (number) times, (timeon) and (timeoff) are self explanatory""" """Blinks the LED (number) times, (timeon) and (timeoff) are self explanatory"""
for x in range (number): for x in range(number):
self.LEDPin.off() self.LEDPin.off()
sleep(timeon) sleep(timeon)
self.LEDPin.on() self.LEDPin.on()
sleep(timeoff) sleep(timeoff)
def Pulse(self,seconds,Speed=1): def Pulse(self, seconds, Speed=1):
"""Helper function for Morse Code""" """Helper function for Morse Code"""
self.LEDPin.off() self.LEDPin.off()
sleep(seconds) sleep(seconds)
self.LEDPin.on() self.LEDPin.on()
sleep (0.1*Speed) sleep(0.1 * Speed)
def Morse(self,text,Speed=1): def Morse(self, text, Speed=1):
"""Blinks (text) in morse code. Speed is around 60 cpm at 1 and proportional""" """Blinks (text) in morse code. Speed is around 60 cpm at 1 and proportional"""
Speed=1/Speed Speed = 1 / Speed
Dot=0.1*Speed Dot = 0.1 * Speed
Dash=0.3*Speed Dash = 0.3 * Speed
SpaceInLetter=0.1*Speed SpaceInLetter = 0.1 * Speed
SpaceBetweenLetters=0.3*Speed SpaceBetweenLetters = 0.3 * Speed
Space=0.7*Speed Space = 0.7 * Speed
alphabet={' ':' ','a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.','f':'..-.','g':'--.', alphabet = {
'h':'....','i':'..','j':'.---','k':'-.-','l':'.-..','m':'--','n':'-.','o':'---', " ": " ",
'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'-','u':'..-','v':'...-','w':'.--', "a": ".-",
'x':'-..-','y':'-.--','z':'--..','1':'.----','2':'..---','3':'...--','4':'....-', "b": "-...",
'5':'.....','6':'-....','7':'--...','8':'---..','9':'----.','0':'-----', "c": "-.-.",
'.':'.-.-.-',} "d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
".": ".-.-.-",
}
print ("morsing %s"%text) print("morsing %s" % text)
for c in text: for c in text:
for m in alphabet[c.lower()]: for m in alphabet[c.lower()]:
if m == '.': if m == ".":
self.Pulse(Dot,Speed) self.Pulse(Dot, Speed)
if m == '-': if m == "-":
self.Pulse(Dash,Speed) self.Pulse(Dash, Speed)
if m == ' ': if m == " ":
sleep(Space) sleep(Space)
sleep(SpaceBetweenLetters) sleep(SpaceBetweenLetters)

View File

@@ -2,5 +2,3 @@ from aabiot.LED import LED
from aabiot.DoubleReset import DoubleReset from aabiot.DoubleReset import DoubleReset
print("AABIOT v0.1 imported") print("AABIOT v0.1 imported")

View File

@@ -7,12 +7,13 @@ weigand.py - read card IDs from a wiegand card reader
from machine import Pin, Timer from machine import Pin, Timer
import utime import utime
CARD_MASK = 0b11111111111111110 # 16 ones CARD_MASK = 0b11111111111111110 # 16 ones
FACILITY_MASK = 0b1111111100000000000000000 # 8 ones FACILITY_MASK = 0b1111111100000000000000000 # 8 ones
# Max pulse interval: 2ms # Max pulse interval: 2ms
# pulse width: 50us # pulse width: 50us
class Wiegand: class Wiegand:
def __init__(self, pin0, pin1, callback): def __init__(self, pin0, pin1, callback):
""" """
@@ -35,8 +36,11 @@ class Wiegand:
self.timer.init(period=50, mode=Timer.PERIODIC, callback=self._cardcheck) self.timer.init(period=50, mode=Timer.PERIODIC, callback=self._cardcheck)
self.cards_read = 0 self.cards_read = 0
def _on_pin0(self, newstate): self._on_pin(0, newstate) def _on_pin0(self, newstate):
def _on_pin1(self, newstate): self._on_pin(1, newstate) self._on_pin(0, newstate)
def _on_pin1(self, newstate):
self._on_pin(1, newstate)
def _on_pin(self, is_one, newstate): def _on_pin(self, is_one, newstate):
now = utime.ticks_ms() now = utime.ticks_ms()
@@ -46,22 +50,24 @@ class Wiegand:
self.last_bit_read = now self.last_bit_read = now
self.next_card <<= 1 self.next_card <<= 1
if is_one: self.next_card |= 1 if is_one:
self.next_card |= 1
self._bits += 1 self._bits += 1
def get_card(self): def get_card(self):
if self.last_card is None: if self.last_card is None:
return None return None
return ( self.last_card & CARD_MASK ) >> 1 return (self.last_card & CARD_MASK) >> 1
def get_facility_code(self): def get_facility_code(self):
if self.last_card is None: if self.last_card is None:
return None return None
# Specific to standard 26bit wiegand # Specific to standard 26bit wiegand
return ( self.last_card & FACILITY_MASK ) >> 17 return (self.last_card & FACILITY_MASK) >> 17
def _cardcheck(self, t): def _cardcheck(self, t):
if self.last_bit_read is None: return if self.last_bit_read is None:
return
now = utime.ticks_ms() now = utime.ticks_ms()
if now - self.last_bit_read > 50: if now - self.last_bit_read > 50:
# too slow - new start! # too slow - new start!

11
boot.py
View File

@@ -1,9 +1,12 @@
# 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
#webrepl.start() # import webrepl
# webrepl.start()
gc.collect() gc.collect()

43
main.py
View File

@@ -10,48 +10,59 @@ if aabiot.DoubleReset():
print("Double Reset - Starting AP") print("Double Reset - Starting AP")
AP() AP()
#import settings # import settings
settings=json.loads(''.join(open('settings.json').readlines())) settings = json.loads("".join(open("settings.json").readlines()))
LED=aabiot.LED.LED() LED = aabiot.LED.LED()
def netstop(): def netstop():
n=network.WLAN(network.STA_IF) n = network.WLAN(network.STA_IF)
n.active(False) n.active(False)
n=network.WLAN(network.AP_IF) n = network.WLAN(network.AP_IF)
n.active(False) n.active(False)
def STA(): def STA():
"""Connects Board as Client to WiFi-Network. LED.Blinks 5 times short if successful and then displays the IP address in morse code""" """Connects Board as Client to WiFi-Network. LED.Blinks 5 times short if successful and then displays the IP address in morse code"""
sta=network.WLAN(network.STA_IF) sta = network.WLAN(network.STA_IF)
if sta.isconnected(): if sta.isconnected():
LED.Blink(5) LED.Blink(5)
return True return True
sta.active(True) sta.active(True)
if ('NetMask') in settings and 'DNS' in settings and 'Gateway' in settings and 'IP' in settings: if (
("NetMask") in settings
and "DNS" in settings
and "Gateway" in settings
and "IP" in settings
):
print("Static configuration active") print("Static configuration active")
sta.ifconfig((settings["IP"],settings["NetMask"],settings["Gateway"],settings["DNS"])) sta.ifconfig(
(settings["IP"], settings["NetMask"], settings["Gateway"], settings["DNS"])
)
else: else:
print("DHCP active") print("DHCP active")
sta.connect(settings['ssid'],settings['password']) sta.connect(settings["ssid"], settings["password"])
timeout=0 timeout = 0
while timeout < 30: while timeout < 30:
if sta.isconnected(): if sta.isconnected():
LED.Morse('ip %s'%sta.ifconfig()[0],1) LED.Morse("ip %s" % sta.ifconfig()[0], 1)
return True return True
else: else:
LED.Blink(1,.05,.95) LED.Blink(1, 0.05, 0.95)
timeout += 1 timeout += 1
print("Time waiting: %s sec"%timeout) print("Time waiting: %s sec" % timeout)
sta.active(False) sta.active(False)
return False return False
def AP(): def AP():
"""Provides Access point with settings in settings.json. LED blinks 'AP' in morse code on success""" """Provides Access point with settings in settings.json. LED blinks 'AP' in morse code on success"""
ap=network.WLAN(network.AP_IF) ap = network.WLAN(network.AP_IF)
ap.active(True) ap.active(True)
ap.config(essid=settings['AP-ssid'],password=settings['AP-password']) ap.config(essid=settings["AP-ssid"], password=settings["AP-password"])
print("AP configured") print("AP configured")
LED.Morse ('AP') LED.Morse("AP")
netstop() netstop()
if STA() != True: if STA() != True:

View File

@@ -1,5 +1,9 @@
import network import network
n=network.WLAN(network.STA_IF) n = network.WLAN(network.STA_IF)
n.active(False) n.active(False)
n = network.WLAN(network.AP_IF)
n.active(False)