from time import time
from uasyncio import sleep_ms, sleep
from machine import WDT
from rfid_utils import RFID, BUZZ, GLED, CardFilter, OUT
from webserver import eprint
from config import settings
from uasyncio import AsyncUDP
async def door():
OUT.off()
await sleep(5)
OUT.on()
async def run():
await eprint("User code started")
rfid = RFID()
GLED.off()
OUT.on()
c_filter = CardFilter(2)
wdt_enabled = settings['sys-i-wdt']
str_format = settings['rfid-format']
udp_en = (settings['rfid-protocol'] == 'UDP')
if wdt_enabled:
wdt = WDT(timeout=10000)
await BUZZ.beep(100)
while 1:
if wdt_enabled:
wdt.feed()
await sleep_ms(100)
c_filter.clean(time())
uid = rfid.read(str_format)
if uid and uid not in c_filter:
c_filter.append(uid)
GLED.on()
await BUZZ.beep()
GLED.off()
if (udp_en):
await AsyncUDP.udp_send(uid, (settings['rfid-dest'], settings['rfid-i-dport']) )
await door()
from time import time
from uasyncio import sleep, TCPClient
from rfid_utils import BUZZ
from webserver import eprint
from config import settings
async def run():
await eprint("User code started")
client = TCPClient(settings['rfid-dest'], settings['rfid-i-dport'])
client.connect()
while 1:
await BUZZ.beep()
await client.send('hello')
await sleep(1)
from time import time
from uasyncio import sleep, AsyncUDP
from rfid_utils import BUZZ
from webserver import eprint
async def run():
await eprint("User code started")
while 1:
await BUZZ.beep()
await AsyncUDP.udp_send('hello', ('192.168.0.51', 7000))
await sleep(1)
async def try_send_post(url, data):
n = 0
try:
response = crequests.request(url, crequests.POST, data=dumps(data), headers={'Content-Type': 'application/json'})
while not response:
await uasyncio.sleep_ms(20)
n += 1
if n == 100:
raise
return response.text()
except Exception as e:
return None
def http_command(data):
print(data)
if 'led' in data:
if data['led'] == 'on':
GLED.on()
elif data['led'] =='off':
GLED.off()
else:
return {'status': 'invalid'}
return {'status': 'ok'}
else:
return {'status': 'unsupported'}