No idea what I'm committing here, but it goes on github
This commit is contained in:
21
DoubleReset.py
Normal file
21
DoubleReset.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from time import sleep
|
||||||
|
import os
|
||||||
|
|
||||||
|
def DoubleReset(Time=5,File="doublereset.txt"):
|
||||||
|
try:
|
||||||
|
a=open(File)
|
||||||
|
Check=a.read().strip()
|
||||||
|
a.close()
|
||||||
|
if Check == '1':
|
||||||
|
os.remove(File)
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
a=open(File,"w")
|
||||||
|
a.write("1")
|
||||||
|
a.close()
|
||||||
|
sleep(Time)
|
||||||
|
os.remove(File)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
26
aabiot/DoubleReset.py
Normal file
26
aabiot/DoubleReset.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from time import sleep
|
||||||
|
import os
|
||||||
|
|
||||||
|
def DoubleReset(Time=5,File="doublereset.txt"):
|
||||||
|
"""Use this at the beginning of the file.
|
||||||
|
DoubleReset(Time=5,File="doublereset.txt")
|
||||||
|
Determines, if the board had two successive resets in the last <n> seconds.
|
||||||
|
This is done by writing a file in <File> and deleting it after <n> seconds.
|
||||||
|
If the file is already there, the reset was doubled."""
|
||||||
|
try:
|
||||||
|
a=open(File)
|
||||||
|
Check=a.read().strip()
|
||||||
|
a.close()
|
||||||
|
if Check == '1':
|
||||||
|
os.remove(File)
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
a=open(File,"w")
|
||||||
|
a.write("1")
|
||||||
|
a.close()
|
||||||
|
sleep(Time)
|
||||||
|
os.remove(File)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -1,49 +1,49 @@
|
|||||||
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):
|
def __init__(self,LEDPin=2):
|
||||||
self.LEDPin=Pin(LEDPin,Pin.OUT)
|
self.LEDPin=Pin(LEDPin,Pin.OUT)
|
||||||
print ("New LED defined on Pin %s"%LEDPin)
|
print ("New LED defined on Pin %s"%LEDPin)
|
||||||
def Blink(self,number,timeon=0.2,timeoff=0.2):
|
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={' ':' ','a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.','f':'..-.','g':'--.',
|
||||||
'h':'....','i':'..','j':'.---','k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
|
'h':'....','i':'..','j':'.---','k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
|
||||||
'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'-','u':'..-','v':'...-','w':'.--',
|
'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'-','u':'..-','v':'...-','w':'.--',
|
||||||
'x':'-..-','y':'-.--','z':'--..','1':'.----','2':'..---','3':'...--','4':'....-',
|
'x':'-..-','y':'-.--','z':'--..','1':'.----','2':'..---','3':'...--','4':'....-',
|
||||||
'5':'.....','6':'-....','7':'--...','8':'---..','9':'----.','0':'-----',
|
'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)
|
||||||
|
|
||||||
6
aabiot/__init__.py
Normal file
6
aabiot/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from aabiot.LED import LED
|
||||||
|
from aabiot.DoubleReset import DoubleReset
|
||||||
|
|
||||||
|
print("AABIOT v0.1 imported")
|
||||||
|
|
||||||
|
|
||||||
73
aabiot/wiegand.py
Executable file
73
aabiot/wiegand.py
Executable file
@@ -0,0 +1,73 @@
|
|||||||
|
"""
|
||||||
|
weigand.py - read card IDs from a wiegand card reader
|
||||||
|
|
||||||
|
(C) 2017 Paul Jimenez - released under LGPLv3+
|
||||||
|
"""
|
||||||
|
|
||||||
|
from machine import Pin, Timer
|
||||||
|
import utime
|
||||||
|
|
||||||
|
CARD_MASK = 0b11111111111111110 # 16 ones
|
||||||
|
FACILITY_MASK = 0b1111111100000000000000000 # 8 ones
|
||||||
|
|
||||||
|
# Max pulse interval: 2ms
|
||||||
|
# pulse width: 50us
|
||||||
|
|
||||||
|
class Wiegand:
|
||||||
|
def __init__(self, pin0, pin1, callback):
|
||||||
|
"""
|
||||||
|
pin0 - the GPIO that goes high when a zero is sent by the reader
|
||||||
|
pin1 - the GPIO that goes high when a one is sent by the reader
|
||||||
|
callback - the function called (with two args: card ID and cardcount)
|
||||||
|
when a card is detected. Note that micropython interrupt
|
||||||
|
implementation limitations apply to the callback!
|
||||||
|
"""
|
||||||
|
self.pin0 = Pin(pin0, Pin.IN)
|
||||||
|
self.pin1 = Pin(pin1, Pin.IN)
|
||||||
|
self.callback = callback
|
||||||
|
self.last_card = None
|
||||||
|
self.next_card = 0
|
||||||
|
self._bits = 0
|
||||||
|
self.pin0.irq(trigger=Pin.IRQ_FALLING, handler=self._on_pin0)
|
||||||
|
self.pin1.irq(trigger=Pin.IRQ_FALLING, handler=self._on_pin1)
|
||||||
|
self.last_bit_read = None
|
||||||
|
self.timer = Timer(-1)
|
||||||
|
self.timer.init(period=50, mode=Timer.PERIODIC, callback=self._cardcheck)
|
||||||
|
self.cards_read = 0
|
||||||
|
|
||||||
|
def _on_pin0(self, newstate): self._on_pin(0, newstate)
|
||||||
|
def _on_pin1(self, newstate): self._on_pin(1, newstate)
|
||||||
|
|
||||||
|
def _on_pin(self, is_one, newstate):
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if self.last_bit_read is not None and now - self.last_bit_read < 2:
|
||||||
|
# too fast
|
||||||
|
return
|
||||||
|
|
||||||
|
self.last_bit_read = now
|
||||||
|
self.next_card <<= 1
|
||||||
|
if is_one: self.next_card |= 1
|
||||||
|
self._bits += 1
|
||||||
|
|
||||||
|
def get_card(self):
|
||||||
|
if self.last_card is None:
|
||||||
|
return None
|
||||||
|
return ( self.last_card & CARD_MASK ) >> 1
|
||||||
|
|
||||||
|
def get_facility_code(self):
|
||||||
|
if self.last_card is None:
|
||||||
|
return None
|
||||||
|
# Specific to standard 26bit wiegand
|
||||||
|
return ( self.last_card & FACILITY_MASK ) >> 17
|
||||||
|
|
||||||
|
def _cardcheck(self, t):
|
||||||
|
if self.last_bit_read is None: return
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if now - self.last_bit_read > 50:
|
||||||
|
# too slow - new start!
|
||||||
|
self.last_bit_read = None
|
||||||
|
self.last_card = self.next_card
|
||||||
|
self.next_card = 0
|
||||||
|
self._bits = 0
|
||||||
|
self.cards_read += 1
|
||||||
|
self.callback(self.get_card(), self.get_facility_code(), self.cards_read)
|
||||||
12
main.py
12
main.py
@@ -2,11 +2,17 @@ import network
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
import LED
|
import aabiot.LED
|
||||||
import Device
|
import Device
|
||||||
|
import aabiot
|
||||||
|
|
||||||
|
if aabiot.DoubleReset():
|
||||||
|
print("Double Reset - Starting 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()
|
||||||
|
|
||||||
def netstop():
|
def netstop():
|
||||||
n=network.WLAN(network.STA_IF)
|
n=network.WLAN(network.STA_IF)
|
||||||
@@ -15,7 +21,7 @@ def netstop():
|
|||||||
n.active(False)
|
n.active(False)
|
||||||
|
|
||||||
def STA():
|
def STA():
|
||||||
"""Connects Board as Client to WiFi-Network. LED.Blinks 5 times short if successful"""
|
"""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)
|
||||||
@@ -40,7 +46,7 @@ def STA():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def AP():
|
def AP():
|
||||||
"""Provides Access point with settings in settings.json. LED.Blinks 3 long, 3 short 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'])
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
{"password": "blueground856",
|
{"password": "PASSWORD",
|
||||||
"DNS": "8.8.8.8",
|
"DNS": "8.8.8.8",
|
||||||
"AP-password": "OpenSesame2020",
|
"AP-password": "PASSWORD",
|
||||||
"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"}
|
"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"}
|
||||||
|
|||||||
Reference in New Issue
Block a user