From 0ddee7e933a6c2918cb8dcbd091f8390110fccae Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Fri, 26 Mar 2021 16:37:34 +0100 Subject: [PATCH 01/13] makefile release process --- Makefile | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7746018 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ + +release: pysense pysense2 + + +pysense: + mkdir pysense + mkdir pysense/lib + # sensors + cp shields/lib/LIS2HH12.py pysense/lib/ + cp shields/lib/LTR329ALS01.py pysense/lib/ + cp shields/lib/MPL3115A2.py pysense/lib/ + cp shields/lib/SI7006A20.py pysense/lib/ + # pycoproc + cp shields/lib/pycoproc_1.py pysense/lib/pycoproc.py + # example + cp shields/pysense_1.py pysense/main.py + zip -r pysense.zip pysense + +pysense2: + mkdir pysense2 + mkdir pysense2/lib + # sensors + cp shields/lib/LIS2HH12.py pysense2/lib/ + cp shields/lib/LTR329ALS01.py pysense2/lib/ + cp shields/lib/MPL3115A2.py pysense2/lib/ + cp shields/lib/SI7006A20.py pysense2/lib/ + # pycoproc + cp shields/lib/pycoproc.py pysense/lib/pycoproc.py + # example + cp shields/pysense_2.py pysense/main.py + zip -r pysense2.zip pysense2 + +clean: + @echo "Cleaning up files" + rm -rf pysense + rm -rf pytrack + rm -rf pyscan + rm -rf pysense2 + rm -rf pytrack2 + From 4de614d927daabce50ea9ecd2041b31e2c657a48 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:51:21 +0200 Subject: [PATCH 02/13] Update Makefile --- Makefile | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 7746018..8fc154a 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ - -release: pysense pysense2 +release: clean pysense pysense2 pytrack pytrack2 pyscan +PYBYTES ?= 0 pysense: + @echo "Making Pysense" mkdir pysense mkdir pysense/lib # sensors @@ -11,12 +12,18 @@ pysense: cp shields/lib/MPL3115A2.py pysense/lib/ cp shields/lib/SI7006A20.py pysense/lib/ # pycoproc - cp shields/lib/pycoproc_1.py pysense/lib/pycoproc.py + cp shields/lib/pycoproc_1.py pysense/lib/ # example +ifeq ($(PYBYTES), 1) + $(info PYBYTES enabled) + cp shields/pysense_1_pybytes.py pysense/main.py +else cp shields/pysense_1.py pysense/main.py +endif zip -r pysense.zip pysense pysense2: + @echo "Making Pysense 2" mkdir pysense2 mkdir pysense2/lib # sensors @@ -25,11 +32,60 @@ pysense2: cp shields/lib/MPL3115A2.py pysense2/lib/ cp shields/lib/SI7006A20.py pysense2/lib/ # pycoproc - cp shields/lib/pycoproc.py pysense/lib/pycoproc.py + cp shields/lib/pycoproc_2.py pysense2/lib/ # example - cp shields/pysense_2.py pysense/main.py +ifeq ($(PYBYTES),1) + cp shields/pysense_2_pybytes.py pysense2/main.py +else + cp shields/pysense_2.py pysense2/main.py +endif zip -r pysense2.zip pysense2 +pytrack: + @echo "Making Pytrack" + mkdir pytrack + mkdir pytrack/lib + #sensors + cp shields/lib/L76GNSS.py pytrack/lib/ + cp shields/lib/LIS2HH12.py pytrack/lib/ + #pycoproc + cp shields/lib/pycoproc_1.py pytrack/lib/ + #example +ifeq ($(PYBYTES),1) + cp shields/pytrack_1_pybytes.py pytrack/main.py +else + cp shields/pytrack_1.py pytrack/main.py +endif +pytrack2: + @echo "Making Pytrack2" + mkdir pytrack2 + mkdir pytrack2/lib + #sensors + cp shields/lib/L76GNSS.py pytrack2/lib/ + cp shields/lib/LIS2HH12.py pytrack2/lib/ + #pycoproc + cp shields/lib/pycoproc_2.py pytrack2/lib/ + #example +ifeq ($(PYBYTES),1) + cp shields/pytrack_2_pybytes.py pytrack2/main.py +else + cp shields/pytrack_2.py pytrack2/main.py +endif + +pyscan: + @echo "Making Pyscan" + mkdir pyscan + mkdir pyscan/lib + #sensors + cp shields/lib/LIS2HH12.py pyscan/lib/ + cp shields/lib/MFRC630.py pyscan/lib/ + cp shields/lib/SI7006A20.py pyscan/lib/ + cp shields/lib/LTR329ALS01.py pyscan/lib/ + #pycoproc + cp shields/lib/pycoproc_1.py pyscan/lib/ + #example + cp shields/pyscan_1.py pyscan/main.py + clean: @echo "Cleaning up files" rm -rf pysense @@ -37,4 +93,10 @@ clean: rm -rf pyscan rm -rf pysense2 rm -rf pytrack2 - + rm -f pysense.zip + rm -f pysense2.zip + rm -f pytrack.zip + rm -f pytrack2.zip + rm -f pyscan.zip + + From 0a9103e8ae2978c3de0eef6dc46a1ae601b4b2c9 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:54:49 +0200 Subject: [PATCH 03/13] added missing zips --- Makefile | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 8fc154a..f932e89 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ ifeq ($(PYBYTES),1) else cp shields/pytrack_1.py pytrack/main.py endif + zip -r pytrack.zip pytrack pytrack2: @echo "Making Pytrack2" mkdir pytrack2 @@ -71,7 +72,7 @@ ifeq ($(PYBYTES),1) else cp shields/pytrack_2.py pytrack2/main.py endif - + zip -r pytrack2.zip pytrack2 pyscan: @echo "Making Pyscan" mkdir pyscan @@ -84,8 +85,12 @@ pyscan: #pycoproc cp shields/lib/pycoproc_1.py pyscan/lib/ #example +ifeq ($(PYBYTES), 1) + cp shields/pyscan_1_pybytes.py pyscan/main.py +else cp shields/pyscan_1.py pyscan/main.py - +endif + zip -r pyscan.zip pyscan clean: @echo "Cleaning up files" rm -rf pysense From 364c09d41c0a239e20b64c992770d09270e4f0c5 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:55:07 +0200 Subject: [PATCH 04/13] added pybytes example for v2 --- shields/pysense_2_pybytes.py | 52 ++++++++++++++++++++++++++++++++++++ shields/pytrack_2_pybytes.py | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 shields/pysense_2_pybytes.py create mode 100644 shields/pytrack_2_pybytes.py diff --git a/shields/pysense_2_pybytes.py b/shields/pysense_2_pybytes.py new file mode 100644 index 0000000..9c31de0 --- /dev/null +++ b/shields/pysense_2_pybytes.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# +# Copyright (c) 2019, Pycom Limited. +# +# This software is licensed under the GNU GPL version 3 or any +# later version, with permitted additional terms. For more information +# see the Pycom Licence v1.0 document supplied with this file, or +# available at https://www.pycom.io/opensource/licensing +# + +# See https://docs.pycom.io for more information regarding library specifics + +from pycoproc_2 import Pycoproc +from LIS2HH12 import LIS2HH12 +from SI7006A20 import SI7006A20 +from LTR329ALS01 import LTR329ALS01 +from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE + +py = Pycoproc() +mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals +si = SI7006A20(py) +lt = LTR329ALS01(py) +li = LIS2HH12(py) + +import _thread +from time import sleep +import uos + +def send_sensor_data(): + h2 = None + t2 = None + l2 = None + while (pybytes): + h = round(si.humidity(),1) + t = round(si.temperature(),1) + l = lt.light() + if h != h2: + print('humidity', h) + pybytes.send_signal(1, h) + h2=h + if t != t2: + print('temperature', t) + pybytes.send_signal(2, t) + t2=t + if l != l2: + print('luminocity', l) + pybytes.send_signal(3, l) + l2=l + sleep(10) + +# _thread.start_new_thread(send_sensor_data, ()) +send_sensor_data() diff --git a/shields/pytrack_2_pybytes.py b/shields/pytrack_2_pybytes.py new file mode 100644 index 0000000..c70c270 --- /dev/null +++ b/shields/pytrack_2_pybytes.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# +# Copyright (c) 2019, Pycom Limited. +# +# This software is licensed under the GNU GPL version 3 or any +# later version, with permitted additional terms. For more information +# see the Pycom Licence v1.0 document supplied with this file, or +# available at https://www.pycom.io/opensource/licensing +# + +import machine +import math +import network +import os +import time +import utime +from machine import RTC +from machine import SD +from machine import Timer +from L76GNSS import L76GNSS +from pycoproc_2 import Pycoproc +from LIS2HH12 import LIS2HH12 +# setup as a station + +import gc + +time.sleep(2) +gc.enable() + +# setup rtc +rtc = machine.RTC() +rtc.ntp_sync("pool.ntp.org") +utime.sleep_ms(750) +print('\nRTC Set from NTP to UTC:', rtc.now()) +utime.timezone(7200) +print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') +py = Pycoproc() +l76 = L76GNSS(py, timeout=30) +chrono = Timer.Chrono() +chrono.start() +li = LIS2HH12(py) +#sd = SD() +#os.mount(sd, '/sd') +#f = open('/sd/gps-record.txt', 'w') +while (pybytes): + coord = l76.coordinates() + #f.write("{} - {}\n".format(coord, rtc.now())) + print('Sending data', coord) + pybytes.send_signal(1, coord) + pybytes.send_signal(2, li.acceleration()) + time.sleep(10) From a55be226f7c1d4c046721345daa67b6be4db660c Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:55:53 +0200 Subject: [PATCH 05/13] ignore release files --- .gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index beced4e..81ff9e7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,14 @@ *.conf .DS_Store +pyscan/* +pysense/* +pytrack/* +pysense2/* +pytrack2/* + +pyscan.zip +pysense.zip +pytrack.zip +pytrack2.zip +pysense2.zip From 02c81efc5b5610a49272d9b26a3e6ba4ac696c8f Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 29 Mar 2021 14:12:54 +0200 Subject: [PATCH 06/13] make each target clean up before itself --- Makefile | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f932e89..02b8808 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ -release: clean pysense pysense2 pytrack pytrack2 pyscan +release: pysense pysense2 pytrack pytrack2 pyscan PYBYTES ?= 0 pysense: + rm -rf pysense + rm -f pysense.zip @echo "Making Pysense" mkdir pysense mkdir pysense/lib @@ -23,6 +25,8 @@ endif zip -r pysense.zip pysense pysense2: + rm -rf pysense2 + rm -f pysense2.zip @echo "Making Pysense 2" mkdir pysense2 mkdir pysense2/lib @@ -42,6 +46,8 @@ endif zip -r pysense2.zip pysense2 pytrack: + rm -rf pytrack + rm -f pytrack.zip @echo "Making Pytrack" mkdir pytrack mkdir pytrack/lib @@ -58,6 +64,8 @@ else endif zip -r pytrack.zip pytrack pytrack2: + rm -rf pytrack2 + rm -f pytrack2.zip @echo "Making Pytrack2" mkdir pytrack2 mkdir pytrack2/lib @@ -74,6 +82,8 @@ else endif zip -r pytrack2.zip pytrack2 pyscan: + rm -rf pyscan + rm -f pyscan.zip @echo "Making Pyscan" mkdir pyscan mkdir pyscan/lib From 75dbc0ca1721b760b70558af3f2a354171ee8051 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Fri, 9 Apr 2021 16:48:42 +0200 Subject: [PATCH 07/13] added pybytes in all examples, if available --- shields/pyscan_1.py | 87 ++++++++++++++++++++++--------------- shields/pyscan_1_pybytes.py | 12 +++-- shields/pysense_1.py | 22 ++++++++-- shields/pysense_2.py | 19 ++++++++ shields/pytrack_1.py | 15 +++++++ shields/pytrack_2.py | 11 ++++- 6 files changed, 124 insertions(+), 42 deletions(-) diff --git a/shields/pyscan_1.py b/shields/pyscan_1.py index 66ab94e..444cf22 100644 --- a/shields/pyscan_1.py +++ b/shields/pyscan_1.py @@ -1,7 +1,15 @@ +#!/usr/bin/env python +# +# Copyright (c) 2021, Pycom Limited. +# +# This software is licensed under the GNU GPL version 3 or any +# later version, with permitted additional terms. For more information +# see the Pycom Licence v1.0 document supplied with this file, or +# available at https://www.pycom.io/opensource/licensing +# + ''' Simple Pyscan NFC / MiFare Classic Example -Copyright (c) 2020, Pycom Limited. - This example runs the NFC discovery loop in a thread. If a card is detected it will read the UID and compare it to VALID_CARDS RGB LED is BLUE while waiting for card, @@ -28,6 +36,11 @@ lt = LTR329ALS01(py) li = LIS2HH12(py) +pybytes_enabled = False +if 'pybytes' in globals().keys(): + if(pybytes.isconnected()): + pybytes_enabled = True + RGB_BRIGHTNESS = 0x8 RGB_RED = (RGB_BRIGHTNESS << 16) @@ -48,39 +61,45 @@ def print_debug(msg): print(msg) def send_sensor_data(name, timeout): - while(True): - print(lt.light()) - print(li.acceleration()) - time.sleep(timeout) + if(pybytes_enabled): + while(pybytes.isconnected()): + pybytes.send_signal(2, lt.light()) + pybytes.send_signal(3, li.acceleration()) + time.sleep(timeout) -def discovery_loop(nfc, id): - while True: - # Send REQA for ISO14443A card type - print_debug('Sending REQA for ISO14443A card type...') - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) - print_debug('Response: {}'.format(atqa)) - if (atqa != 0): - # A card has been detected, read UID - print_debug('A card has been detected, read UID...') - uid = bytearray(10) - uid_len = nfc.mfrc630_iso14443a_select(uid) - print_debug('UID has length: {}'.format(uid_len)) - if (uid_len > 0): - print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) - if (check_uid(list(uid), uid_len)) > 0: - print_debug('Card is listed, turn LED green') - pycom.rgbled(RGB_GREEN) - else: - print_debug('Card is not listed, turn LED red') - pycom.rgbled(RGB_RED) - else: - # No card detected - print_debug('Did not detect any card...') - pycom.rgbled(RGB_BLUE) - nfc.mfrc630_cmd_reset() - time.sleep(.5) - nfc.mfrc630_cmd_init() +def discovery_loop(arg1, arg2): + # Send REQA for ISO14443A card type + print_debug('Sending REQA for ISO14443A card type...') + atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) + print_debug('Response: {}'.format(atqa)) + if (atqa != 0): + # A card has been detected, read UID + print_debug('A card has been detected, read UID...') + uid = bytearray(10) + uid_len = nfc.mfrc630_iso14443a_select(uid) + print_debug('UID has length: {}'.format(uid_len)) + if (uid_len > 0): + print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) + if (check_uid(list(uid), uid_len)) > 0: + print_debug('Card is listed, turn LED green') + pycom.rgbled(RGB_GREEN) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is listed, Access granted', uid)) + else: + print_debug('Card is not listed, turn LED red') + pycom.rgbled(RGB_RED) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) + else: + # No card detected + print_debug('Did not detect any card...') + pycom.rgbled(RGB_BLUE) + if(pybytes_enabled): + pybytes.send_signal(1, ('Did not detect any card', 0)) + nfc.mfrc630_cmd_reset() + time.sleep(.5) + nfc.mfrc630_cmd_init() # This is the start of our main execution... start the thread _thread.start_new_thread(discovery_loop, (nfc, 0)) -_thread.start_new_thread(send_sensor_data, ('Thread 2', 10)) +_thread.start_new_thread(send_sensor_data, ('Thread 2',10)) diff --git a/shields/pyscan_1_pybytes.py b/shields/pyscan_1_pybytes.py index 458b743..4c2ac0e 100644 --- a/shields/pyscan_1_pybytes.py +++ b/shields/pyscan_1_pybytes.py @@ -27,6 +27,9 @@ import time import pycom import _thread +pybytes_enabled = False +if 'pybytes' not in globals().keys(): + pybytes_enabled = True; VALID_CARDS = [[0x43, 0x95, 0xDD, 0xF8], [0x43, 0x95, 0xDD, 0xF9]] @@ -78,16 +81,19 @@ def discovery_loop(arg1, arg2): if (check_uid(list(uid), uid_len)) > 0: print_debug('Card is listed, turn LED green') pycom.rgbled(RGB_GREEN) - pybytes.send_signal(1, ('Card is listed, Access granted', uid)) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is listed, Access granted', uid)) else: print_debug('Card is not listed, turn LED red') pycom.rgbled(RGB_RED) - pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) else: # No card detected print_debug('Did not detect any card...') pycom.rgbled(RGB_BLUE) - pybytes.send_signal(1, ('Did not detect any card', 0)) + if(pybytes_enabled): + pybytes.send_signal(1, ('Did not detect any card', 0)) nfc.mfrc630_cmd_reset() time.sleep(5) nfc.mfrc630_cmd_init() diff --git a/shields/pysense_1.py b/shields/pysense_1.py index 0a0d4b1..d0992e8 100644 --- a/shields/pysense_1.py +++ b/shields/pysense_1.py @@ -25,13 +25,17 @@ py = Pycoproc(Pycoproc.PYSENSE) +pybytes_enabled = False +if 'pybytes' in globals().keys(): + if(pybytes.isconnected()): + pybytes_enabled = True + mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals print("MPL3115A2 temperature: " + str(mp.temperature())) print("Altitude: " + str(mp.altitude())) mpp = MPL3115A2(py,mode=PRESSURE) # Returns pressure in Pa. Mode may also be set to ALTITUDE, returning a value in meters print("Pressure: " + str(mpp.pressure())) - si = SI7006A20(py) print("Temperature: " + str(si.temperature())+ " deg C and Relative Humidity: " + str(si.humidity()) + " %RH") print("Dew point: "+ str(si.dew_point()) + " deg C") @@ -47,8 +51,20 @@ print("Roll: " + str(li.roll())) print("Pitch: " + str(li.pitch())) -print("Battery voltage: " + str(py.read_battery_voltage())) +# set your battery voltage limits here +vmax = 4.2 +vmin = 3.3 +battery_voltage = py.read_battery_voltage() +battery_percentage = (battery_voltage - vmin / (vmax - vmin))*100 +print("Battery voltage: " + str(py.read_battery_voltage()), " percentage: ", battery_percentage) +if(pybytes_enabled): + pybytes.send_signal(1, mpp.pressure()) + pybytes.send_signal(2, si.temperature()) + pybytes.send_signal(3, lt.light()) + pybytes.send_signal(4, li.acceleration()) + pybytes.send_battery_level(int(battery_percentage)) + print("Sent data to pybytes") -time.sleep(3) +time.sleep(5) py.setup_sleep(10) py.go_to_sleep() diff --git a/shields/pysense_2.py b/shields/pysense_2.py index cfa6280..14f676a 100644 --- a/shields/pysense_2.py +++ b/shields/pysense_2.py @@ -25,6 +25,11 @@ py = Pycoproc() +pybytes_enabled = False +if 'pybytes' in globals().keys(): + if(pybytes.isconnected()): + pybytes_enabled = True + mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals print("MPL3115A2 temperature: " + str(mp.temperature())) print("Altitude: " + str(mp.altitude())) @@ -48,3 +53,17 @@ print("Pitch: " + str(li.pitch())) print("Battery voltage: " + str(py.read_battery_voltage())) + +# set your battery voltage limits here +vmax = 4.2 +vmin = 3.3 +battery_voltage = py.read_battery_voltage() +battery_percentage = (battery_voltage - vmin / (vmax - vmin))*100 +print("Battery voltage: " + str(py.read_battery_voltage()), " percentage: ", battery_percentage) +if(pybytes_enabled): + pybytes.send_signal(1, mpp.pressure()) + pybytes.send_signal(2, si.temperature()) + pybytes.send_signal(3, lt.light()) + pybytes.send_signal(4, li.acceleration()) + pybytes.send_battery_level(int(battery_percentage)) + print("Sent data to pybytes") \ No newline at end of file diff --git a/shields/pytrack_1.py b/shields/pytrack_1.py index 4751617..8aef5ad 100644 --- a/shields/pytrack_1.py +++ b/shields/pytrack_1.py @@ -34,6 +34,11 @@ py = Pycoproc(Pycoproc.PYTRACK) l76 = L76GNSS(py, timeout=30) +pybytes_enabled = False +if 'pybytes' in globals().keys(): + if(pybytes.isconnected()): + pybytes_enabled = True + # sd = SD() # os.mount(sd, '/sd') # f = open('/sd/gps-record.txt', 'w') @@ -42,3 +47,13 @@ coord = l76.coordinates() #f.write("{} - {}\n".format(coord, rtc.now())) print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free())) + if(pybytes_enabled): + pybytes.send_signal(1, coord) + time.sleep(10) + +""" +# sleep procedure +time.sleep(3) +py.setup_sleep(10) +py.go_to_sleep() +""" diff --git a/shields/pytrack_2.py b/shields/pytrack_2.py index 3f33ff5..1980477 100644 --- a/shields/pytrack_2.py +++ b/shields/pytrack_2.py @@ -40,15 +40,22 @@ time.sleep(1) l76 = L76GNSS(py, timeout=30, buffer=512) +pybytes_enabled = False +if 'pybytes' in globals().keys(): + if(pybytes.isconnected()): + pybytes_enabled = True + # sd = SD() # os.mount(sd, '/sd') # f = open('/sd/gps-record.txt', 'w') -# while (True): -for _ in range(5): +while (True): coord = l76.coordinates() #f.write("{} - {}\n".format(coord, rtc.now())) print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free())) + if(pybytes_enabled): + pybytes.send_signal(1, coord) + time.sleep(10) """ # sleep procedure From 2be495cec541ed6532b57928cd836d56d9baebbd Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Fri, 9 Apr 2021 16:49:47 +0200 Subject: [PATCH 08/13] updated order, removed pybytes flag --- .gitignore | 5 ++-- Makefile | 75 ++++++++++++++++++++++-------------------------------- 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 81ff9e7..e943765 100644 --- a/.gitignore +++ b/.gitignore @@ -7,12 +7,13 @@ pyscan/* pysense/* -pytrack/* pysense2/* +pytrack/* pytrack2/* pyscan.zip pysense.zip +pysense2.zip pytrack.zip pytrack2.zip -pysense2.zip + diff --git a/Makefile b/Makefile index 02b8808..17034ef 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,24 @@ -release: pysense pysense2 pytrack pytrack2 pyscan -PYBYTES ?= 0 +release: pyscan pysense pysense2 pytrack pytrack2 +pyscan: + rm -rf pyscan + rm -f pyscan.zip + @echo "Making Pyscan" + mkdir pyscan + mkdir pyscan/lib + #sensors + cp shields/lib/LIS2HH12.py pyscan/lib/ + cp shields/lib/MFRC630.py pyscan/lib/ + cp shields/lib/SI7006A20.py pyscan/lib/ + cp shields/lib/LTR329ALS01.py pyscan/lib/ + #pycoproc + cp shields/lib/pycoproc_1.py pyscan/lib/ + #example + cp shields/pyscan_1.py pyscan/main.py + + zip -r pyscan.zip pyscan + pysense: rm -rf pysense rm -f pysense.zip @@ -16,12 +33,8 @@ pysense: # pycoproc cp shields/lib/pycoproc_1.py pysense/lib/ # example -ifeq ($(PYBYTES), 1) - $(info PYBYTES enabled) - cp shields/pysense_1_pybytes.py pysense/main.py -else cp shields/pysense_1.py pysense/main.py -endif + zip -r pysense.zip pysense pysense2: @@ -38,11 +51,8 @@ pysense2: # pycoproc cp shields/lib/pycoproc_2.py pysense2/lib/ # example -ifeq ($(PYBYTES),1) - cp shields/pysense_2_pybytes.py pysense2/main.py -else cp shields/pysense_2.py pysense2/main.py -endif + zip -r pysense2.zip pysense2 pytrack: @@ -57,12 +67,10 @@ pytrack: #pycoproc cp shields/lib/pycoproc_1.py pytrack/lib/ #example -ifeq ($(PYBYTES),1) - cp shields/pytrack_1_pybytes.py pytrack/main.py -else cp shields/pytrack_1.py pytrack/main.py -endif + zip -r pytrack.zip pytrack + pytrack2: rm -rf pytrack2 rm -f pytrack2.zip @@ -75,43 +83,20 @@ pytrack2: #pycoproc cp shields/lib/pycoproc_2.py pytrack2/lib/ #example -ifeq ($(PYBYTES),1) - cp shields/pytrack_2_pybytes.py pytrack2/main.py -else cp shields/pytrack_2.py pytrack2/main.py -endif + zip -r pytrack2.zip pytrack2 -pyscan: - rm -rf pyscan - rm -f pyscan.zip - @echo "Making Pyscan" - mkdir pyscan - mkdir pyscan/lib - #sensors - cp shields/lib/LIS2HH12.py pyscan/lib/ - cp shields/lib/MFRC630.py pyscan/lib/ - cp shields/lib/SI7006A20.py pyscan/lib/ - cp shields/lib/LTR329ALS01.py pyscan/lib/ - #pycoproc - cp shields/lib/pycoproc_1.py pyscan/lib/ - #example -ifeq ($(PYBYTES), 1) - cp shields/pyscan_1_pybytes.py pyscan/main.py -else - cp shields/pyscan_1.py pyscan/main.py -endif - zip -r pyscan.zip pyscan + clean: @echo "Cleaning up files" - rm -rf pysense - rm -rf pytrack rm -rf pyscan + rm -rf pysense rm -rf pysense2 + rm -rf pytrack rm -rf pytrack2 + + rm -f pyscan.zip rm -f pysense.zip rm -f pysense2.zip rm -f pytrack.zip - rm -f pytrack2.zip - rm -f pyscan.zip - - + rm -f pytrack2.zip \ No newline at end of file From a95e80023842b312608e742e8f3da0560500e4d2 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 12 Apr 2021 14:14:34 +0200 Subject: [PATCH 09/13] rm pybytes examples --- shields/pysense_1_pybytes.py | 52 ------------------------------------ shields/pysense_2_pybytes.py | 52 ------------------------------------ shields/pytrack_1_pybytes.py | 51 ----------------------------------- shields/pytrack_2_pybytes.py | 51 ----------------------------------- 4 files changed, 206 deletions(-) delete mode 100644 shields/pysense_1_pybytes.py delete mode 100644 shields/pysense_2_pybytes.py delete mode 100644 shields/pytrack_1_pybytes.py delete mode 100644 shields/pytrack_2_pybytes.py diff --git a/shields/pysense_1_pybytes.py b/shields/pysense_1_pybytes.py deleted file mode 100644 index 2d3326d..0000000 --- a/shields/pysense_1_pybytes.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2019, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - -# See https://docs.pycom.io for more information regarding library specifics - -from pycoproc_1 import Pycoproc -from LIS2HH12 import LIS2HH12 -from SI7006A20 import SI7006A20 -from LTR329ALS01 import LTR329ALS01 -from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE - -py = Pycoproc(Pycoproc.PYSENSE) -mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals -si = SI7006A20(py) -lt = LTR329ALS01(py) -li = LIS2HH12(py) - -import _thread -from time import sleep -import uos - -def send_sensor_data(): - h2 = None - t2 = None - l2 = None - while (pybytes): - h = round(si.humidity(),1) - t = round(si.temperature(),1) - l = lt.light() - if h != h2: - print('humidity', h) - pybytes.send_signal(1, h) - h2=h - if t != t2: - print('temperature', t) - pybytes.send_signal(2, t) - t2=t - if l != l2: - print('luminocity', l) - pybytes.send_signal(3, l) - l2=l - sleep(10) - -# _thread.start_new_thread(send_sensor_data, ()) -send_sensor_data() diff --git a/shields/pysense_2_pybytes.py b/shields/pysense_2_pybytes.py deleted file mode 100644 index 9c31de0..0000000 --- a/shields/pysense_2_pybytes.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2019, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - -# See https://docs.pycom.io for more information regarding library specifics - -from pycoproc_2 import Pycoproc -from LIS2HH12 import LIS2HH12 -from SI7006A20 import SI7006A20 -from LTR329ALS01 import LTR329ALS01 -from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE - -py = Pycoproc() -mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals -si = SI7006A20(py) -lt = LTR329ALS01(py) -li = LIS2HH12(py) - -import _thread -from time import sleep -import uos - -def send_sensor_data(): - h2 = None - t2 = None - l2 = None - while (pybytes): - h = round(si.humidity(),1) - t = round(si.temperature(),1) - l = lt.light() - if h != h2: - print('humidity', h) - pybytes.send_signal(1, h) - h2=h - if t != t2: - print('temperature', t) - pybytes.send_signal(2, t) - t2=t - if l != l2: - print('luminocity', l) - pybytes.send_signal(3, l) - l2=l - sleep(10) - -# _thread.start_new_thread(send_sensor_data, ()) -send_sensor_data() diff --git a/shields/pytrack_1_pybytes.py b/shields/pytrack_1_pybytes.py deleted file mode 100644 index 51e03ab..0000000 --- a/shields/pytrack_1_pybytes.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2019, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - -import machine -import math -import network -import os -import time -import utime -from machine import RTC -from machine import SD -from machine import Timer -from L76GNSS import L76GNSS -from pycoproc_1 import Pycoproc -from LIS2HH12 import LIS2HH12 -# setup as a station - -import gc - -time.sleep(2) -gc.enable() - -# setup rtc -rtc = machine.RTC() -rtc.ntp_sync("pool.ntp.org") -utime.sleep_ms(750) -print('\nRTC Set from NTP to UTC:', rtc.now()) -utime.timezone(7200) -print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') -py = Pycoproc(Pycoproc.PYTRACK) -l76 = L76GNSS(py, timeout=30) -chrono = Timer.Chrono() -chrono.start() -li = LIS2HH12(py) -#sd = SD() -#os.mount(sd, '/sd') -#f = open('/sd/gps-record.txt', 'w') -while (pybytes): - coord = l76.coordinates() - #f.write("{} - {}\n".format(coord, rtc.now())) - print('Sending data', coord) - pybytes.send_signal(1, coord) - pybytes.send_signal(2, li.acceleration()) - time.sleep(10) diff --git a/shields/pytrack_2_pybytes.py b/shields/pytrack_2_pybytes.py deleted file mode 100644 index c70c270..0000000 --- a/shields/pytrack_2_pybytes.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2019, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - -import machine -import math -import network -import os -import time -import utime -from machine import RTC -from machine import SD -from machine import Timer -from L76GNSS import L76GNSS -from pycoproc_2 import Pycoproc -from LIS2HH12 import LIS2HH12 -# setup as a station - -import gc - -time.sleep(2) -gc.enable() - -# setup rtc -rtc = machine.RTC() -rtc.ntp_sync("pool.ntp.org") -utime.sleep_ms(750) -print('\nRTC Set from NTP to UTC:', rtc.now()) -utime.timezone(7200) -print('Adjusted from UTC to EST timezone', utime.localtime(), '\n') -py = Pycoproc() -l76 = L76GNSS(py, timeout=30) -chrono = Timer.Chrono() -chrono.start() -li = LIS2HH12(py) -#sd = SD() -#os.mount(sd, '/sd') -#f = open('/sd/gps-record.txt', 'w') -while (pybytes): - coord = l76.coordinates() - #f.write("{} - {}\n".format(coord, rtc.now())) - print('Sending data', coord) - pybytes.send_signal(1, coord) - pybytes.send_signal(2, li.acceleration()) - time.sleep(10) From aa3969242b7f94792641d95accaf913d11b1700b Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 12 Apr 2021 14:14:48 +0200 Subject: [PATCH 10/13] added Pybytes info message --- shields/pyscan_1.py | 3 ++- shields/pysense_1.py | 1 + shields/pysense_2.py | 1 + shields/pytrack_1.py | 1 + shields/pytrack_2.py | 1 + 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/shields/pyscan_1.py b/shields/pyscan_1.py index 444cf22..28f2023 100644 --- a/shields/pyscan_1.py +++ b/shields/pyscan_1.py @@ -39,6 +39,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True RGB_BRIGHTNESS = 0x8 @@ -62,7 +63,7 @@ def print_debug(msg): def send_sensor_data(name, timeout): if(pybytes_enabled): - while(pybytes.isconnected()): + while(True): pybytes.send_signal(2, lt.light()) pybytes.send_signal(3, li.acceleration()) time.sleep(timeout) diff --git a/shields/pysense_1.py b/shields/pysense_1.py index d0992e8..f996e2f 100644 --- a/shields/pysense_1.py +++ b/shields/pysense_1.py @@ -28,6 +28,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals diff --git a/shields/pysense_2.py b/shields/pysense_2.py index 14f676a..011b269 100644 --- a/shields/pysense_2.py +++ b/shields/pysense_2.py @@ -28,6 +28,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals diff --git a/shields/pytrack_1.py b/shields/pytrack_1.py index 8aef5ad..7066ccc 100644 --- a/shields/pytrack_1.py +++ b/shields/pytrack_1.py @@ -37,6 +37,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True # sd = SD() diff --git a/shields/pytrack_2.py b/shields/pytrack_2.py index 1980477..ca21b7b 100644 --- a/shields/pytrack_2.py +++ b/shields/pytrack_2.py @@ -43,6 +43,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True # sd = SD() From 3f5d6fa6da94a816ee8dd328c2eb823102ffe9ab Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 12 Apr 2021 14:30:47 +0200 Subject: [PATCH 11/13] enabled debug by default --- shields/pyscan_1.py | 68 ++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/shields/pyscan_1.py b/shields/pyscan_1.py index 28f2023..b99d99e 100644 --- a/shields/pyscan_1.py +++ b/shields/pyscan_1.py @@ -16,7 +16,7 @@ GREEN if card is valid, RED if card is invalid ''' -DEBUG = False # change to True to see debug messages +DEBUG = True # change to True to see debug messages from pycoproc_1 import Pycoproc from MFRC630 import MFRC630 @@ -39,7 +39,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): - print('Pybytes is connected, sending signals to Pybytes') + print_debug('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True RGB_BRIGHTNESS = 0x8 @@ -68,39 +68,39 @@ def send_sensor_data(name, timeout): pybytes.send_signal(3, li.acceleration()) time.sleep(timeout) -def discovery_loop(arg1, arg2): - # Send REQA for ISO14443A card type - print_debug('Sending REQA for ISO14443A card type...') - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) - print_debug('Response: {}'.format(atqa)) - if (atqa != 0): - # A card has been detected, read UID - print_debug('A card has been detected, read UID...') - uid = bytearray(10) - uid_len = nfc.mfrc630_iso14443a_select(uid) - print_debug('UID has length: {}'.format(uid_len)) - if (uid_len > 0): - print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) - if (check_uid(list(uid), uid_len)) > 0: - print_debug('Card is listed, turn LED green') - pycom.rgbled(RGB_GREEN) - if(pybytes_enabled): - pybytes.send_signal(1, ('Card is listed, Access granted', uid)) - else: - print_debug('Card is not listed, turn LED red') - pycom.rgbled(RGB_RED) - if(pybytes_enabled): - pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) - else: - # No card detected - print_debug('Did not detect any card...') - pycom.rgbled(RGB_BLUE) - if(pybytes_enabled): - pybytes.send_signal(1, ('Did not detect any card', 0)) - nfc.mfrc630_cmd_reset() - time.sleep(.5) - nfc.mfrc630_cmd_init() +def discovery_loop(nfc, id): + while(True): + # Send REQA for ISO14443A card type + # print_debug('Sending REQA for ISO14443A card type...') + atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) + # print_debug('Response: {}'.format(atqa)) + if (atqa != 0): + # A card has been detected, read UID + print_debug('A card has been detected, read UID...') + uid = bytearray(10) + uid_len = nfc.mfrc630_iso14443a_select(uid) + print_debug('UID has length: {}'.format(uid_len)) + if (uid_len > 0): + print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) + if (check_uid(list(uid), uid_len)) > 0: + print_debug('Card is listed, turn LED green') + pycom.rgbled(RGB_GREEN) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is listed, Access granted', uid)) + else: + print_debug('Card is not listed, turn LED red') + pycom.rgbled(RGB_RED) + if(pybytes_enabled): + pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) + else: + # No card detected + # print_debug('Did not detect any card...') + pycom.rgbled(RGB_BLUE) + nfc.mfrc630_cmd_reset() + time.sleep(.5) + nfc.mfrc630_cmd_init() # This is the start of our main execution... start the thread _thread.start_new_thread(discovery_loop, (nfc, 0)) _thread.start_new_thread(send_sensor_data, ('Thread 2',10)) +print_debug("Scanning for cards") From af937023cc124739c062bcfd82a959f518c2a501 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 12 Apr 2021 15:03:21 +0200 Subject: [PATCH 12/13] combined pyscan examples --- shields/pyscan_1.py | 124 +++++++++++++++++++----------------- shields/pyscan_1_nfc.py | 75 ---------------------- shields/pyscan_1_pybytes.py | 104 ------------------------------ 3 files changed, 66 insertions(+), 237 deletions(-) delete mode 100644 shields/pyscan_1_nfc.py delete mode 100644 shields/pyscan_1_pybytes.py diff --git a/shields/pyscan_1.py b/shields/pyscan_1.py index b99d99e..2d2dcbb 100644 --- a/shields/pyscan_1.py +++ b/shields/pyscan_1.py @@ -1,36 +1,30 @@ -#!/usr/bin/env python -# -# Copyright (c) 2021, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - ''' Simple Pyscan NFC / MiFare Classic Example -This example runs the NFC discovery loop in a thread. -If a card is detected it will read the UID and compare it to VALID_CARDS -RGB LED is BLUE while waiting for card, -GREEN if card is valid, RED if card is invalid -''' +Copyright (c) 2019, Pycom Limited. -DEBUG = True # change to True to see debug messages +This example continuously sends a REQA for ISO14443A card type +If a card is discovered, it will read the UID +If DECODE_CARD = True, will attempt to authenticate with CARDkey +If authentication succeeds will attempt to read sectors from the card +''' from pycoproc_1 import Pycoproc from MFRC630 import MFRC630 from LIS2HH12 import LIS2HH12 from LTR329ALS01 import LTR329ALS01 -import binascii import time import pycom -import _thread +#add your card UID here VALID_CARDS = [[0x43, 0x95, 0xDD, 0xF8], [0x43, 0x95, 0xDD, 0xF9], [0x46, 0x5A, 0xEB, 0x7D, 0x8A, 0x08, 0x04]] + +# This is the default key for an unencrypted MiFare card +CARDkey = [ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ] +DECODE_CARD = False + py = Pycoproc(Pycoproc.PYSCAN) nfc = MFRC630(py) lt = LTR329ALS01(py) @@ -39,7 +33,7 @@ pybytes_enabled = False if 'pybytes' in globals().keys(): if(pybytes.isconnected()): - print_debug('Pybytes is connected, sending signals to Pybytes') + print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True RGB_BRIGHTNESS = 0x8 @@ -48,19 +42,11 @@ RGB_GREEN = (RGB_BRIGHTNESS << 8) RGB_BLUE = (RGB_BRIGHTNESS) -# Make sure heartbeat is disabled before setting RGB LED -pycom.heartbeat(False) - -# Initialise the MFRC630 with some settings -nfc.mfrc630_cmd_init() +counter = 0 def check_uid(uid, len): return VALID_CARDS.count(uid[:len]) -def print_debug(msg): - if DEBUG: - print(msg) - def send_sensor_data(name, timeout): if(pybytes_enabled): while(True): @@ -68,39 +54,61 @@ def send_sensor_data(name, timeout): pybytes.send_signal(3, li.acceleration()) time.sleep(timeout) -def discovery_loop(nfc, id): - while(True): - # Send REQA for ISO14443A card type - # print_debug('Sending REQA for ISO14443A card type...') - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) - # print_debug('Response: {}'.format(atqa)) - if (atqa != 0): - # A card has been detected, read UID - print_debug('A card has been detected, read UID...') - uid = bytearray(10) - uid_len = nfc.mfrc630_iso14443a_select(uid) - print_debug('UID has length: {}'.format(uid_len)) - if (uid_len > 0): - print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) +# Make sure heartbeat is disabled before setting RGB LED +pycom.heartbeat(False) + +# Initialise the MFRC630 with some settings +nfc.mfrc630_cmd_init() + +print('Scanning for cards') +while True: + # Send REQA for ISO14443A card type + atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) + if (atqa != 0): + # A card has been detected, read UID + print('A card has been detected, reading its UID ...') + uid = bytearray(10) + uid_len = nfc.mfrc630_iso14443a_select(uid) + print('UID has length {}'.format(uid_len)) + if (uid_len > 0): + # A valid UID has been detected, print details + counter += 1 + print("%d\tUID [%d]: %s" % (counter, uid_len, nfc.format_block(uid, uid_len))) + if DECODE_CARD: + # Try to authenticate with CARD key + nfc.mfrc630_cmd_load_key(CARDkey) + for sector in range(0, 16): + if (nfc.mfrc630_MF_auth(uid, nfc.MFRC630_MF_AUTH_KEY_A, sector * 4)): + pycom.rgbled(RGB_GREEN) + # Authentication was sucessful, read card data + readbuf = bytearray(16) + for b in range(0, 4): + f_sect = sector * 4 + b + len = nfc.mfrc630_MF_read_block(f_sect, readbuf) + print("\t\tSector %s: Block: %s: %s" % (nfc.format_block([sector], 1), nfc.format_block([b], 1), nfc.format_block(readbuf, len))) + else: + print("Authentication denied for sector %s!" % nfc.format_block([sector], 1)) + pycom.rgbled(RGB_RED) + # It is necessary to call mfrc630_MF_deauth after authentication + # Although this is also handled by the reset / init cycle + nfc.mfrc630_MF_deauth() + else: + #check if card uid is listed in VALID_CARDS if (check_uid(list(uid), uid_len)) > 0: - print_debug('Card is listed, turn LED green') + print('Card is listed, turn LED green') pycom.rgbled(RGB_GREEN) if(pybytes_enabled): - pybytes.send_signal(1, ('Card is listed, Access granted', uid)) + pybytes.send_signal(1, ('Card is listed', uid)) else: - print_debug('Card is not listed, turn LED red') + print('Card is not listed, turn LED red') pycom.rgbled(RGB_RED) if(pybytes_enabled): - pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) - else: - # No card detected - # print_debug('Did not detect any card...') - pycom.rgbled(RGB_BLUE) - nfc.mfrc630_cmd_reset() - time.sleep(.5) - nfc.mfrc630_cmd_init() - -# This is the start of our main execution... start the thread -_thread.start_new_thread(discovery_loop, (nfc, 0)) -_thread.start_new_thread(send_sensor_data, ('Thread 2',10)) -print_debug("Scanning for cards") + pybytes.send_signal(1, ('Unauthorized card detected', uid)) + + else: + pycom.rgbled(RGB_BLUE) + # We could go into power saving mode here... to be investigated + nfc.mfrc630_cmd_reset() + time.sleep(.5) + # Re-Initialise the MFRC630 with settings as these got wiped during reset + nfc.mfrc630_cmd_init() diff --git a/shields/pyscan_1_nfc.py b/shields/pyscan_1_nfc.py deleted file mode 100644 index 6b2ea36..0000000 --- a/shields/pyscan_1_nfc.py +++ /dev/null @@ -1,75 +0,0 @@ -''' -Simple Pyscan NFC / MiFare Classic Example -Copyright (c) 2019, Pycom Limited. - -This example continuously sends a REQA for ISO14443A card type -If a card is discovered, it will read the UID -If DECODE_CARD = True, will attempt to authenticate with CARDkey -If authentication succeeds will attempt to read sectors from the card -''' - -from pycoproc_1 import Pycoproc -from MFRC630 import MFRC630 -import time -import pycom - -# This is the default key for an unencrypted MiFare card -CARDkey = [ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ] -DECODE_CARD = False - -py = Pycoproc(Pycoproc.PYSCAN) -nfc = MFRC630(py) - -RGB_BRIGHTNESS = 0x8 - -RGB_RED = (RGB_BRIGHTNESS << 16) -RGB_GREEN = (RGB_BRIGHTNESS << 8) -RGB_BLUE = (RGB_BRIGHTNESS) - -counter = 0 - -# Make sure heartbeat is disabled before setting RGB LED -pycom.heartbeat(False) - -# Initialise the MFRC630 with some settings -nfc.mfrc630_cmd_init() - -while True: - # Send REQA for ISO14443A card type - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) - if (atqa != 0): - # A card has been detected, read UID - uid = bytearray(10) - uid_len = nfc.mfrc630_iso14443a_select(uid) - if (uid_len > 0): - # A valid UID has been detected, print details - counter += 1 - print("%d\tUID [%d]: %s" % (counter, uid_len, nfc.format_block(uid, uid_len))) - if DECODE_CARD: - # Try to authenticate with CARD key - nfc.mfrc630_cmd_load_key(CARDkey) - for sector in range(0, 16): - if (nfc.mfrc630_MF_auth(uid, nfc.MFRC630_MF_AUTH_KEY_A, sector * 4)): - pycom.rgbled(RGB_GREEN) - # Authentication was sucessful, read card data - readbuf = bytearray(16) - for b in range(0, 4): - f_sect = sector * 4 + b - len = nfc.mfrc630_MF_read_block(f_sect, readbuf) - print("\t\tSector %s: Block: %s: %s" % (nfc.format_block([sector], 1), nfc.format_block([b], 1), nfc.format_block(readbuf, len))) - else: - print("Authentication denied for sector %s!" % nfc.format_block([sector], 1)) - pycom.rgbled(RGB_RED) - # It is necessary to call mfrc630_MF_deauth after authentication - # Although this is also handled by the reset / init cycle - nfc.mfrc630_MF_deauth() - else: - # If we're not trying to authenticate, show green when a UID > 0 has been detected - pycom.rgbled(RGB_GREEN) - else: - pycom.rgbled(RGB_BLUE) - # We could go into power saving mode here... to be investigated - nfc.mfrc630_cmd_reset() - time.sleep(.5) - # Re-Initialise the MFRC630 with settings as these got wiped during reset - nfc.mfrc630_cmd_init() diff --git a/shields/pyscan_1_pybytes.py b/shields/pyscan_1_pybytes.py deleted file mode 100644 index 4c2ac0e..0000000 --- a/shields/pyscan_1_pybytes.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2019, Pycom Limited. -# -# This software is licensed under the GNU GPL version 3 or any -# later version, with permitted additional terms. For more information -# see the Pycom Licence v1.0 document supplied with this file, or -# available at https://www.pycom.io/opensource/licensing -# - -''' -Simple Pyscan NFC / MiFare Classic Example -Copyright (c) 2019, Pycom Limited. -This example runs the NFC discovery loop in a thread. -If a card is detected it will read the UID and compare it to VALID_CARDS -RGB LED is BLUE while waiting for card, -GREEN if card is valid, RED if card is invalid -''' - -DEBUG = False # change to True to see debug messages - -from pycoproc_1 import Pycoproc -from MFRC630 import MFRC630 -from LIS2HH12 import LIS2HH12 -from LTR329ALS01 import LTR329ALS01 -import binascii -import time -import pycom -import _thread -pybytes_enabled = False -if 'pybytes' not in globals().keys(): - pybytes_enabled = True; - -VALID_CARDS = [[0x43, 0x95, 0xDD, 0xF8], - [0x43, 0x95, 0xDD, 0xF9]] - -py = Pycoproc(Pycoproc.PYSENSE) -nfc = MFRC630(py) -lt = LTR329ALS01(py) -li = LIS2HH12(py) - -RGB_BRIGHTNESS = 0x8 - -RGB_RED = (RGB_BRIGHTNESS << 16) -RGB_GREEN = (RGB_BRIGHTNESS << 8) -RGB_BLUE = (RGB_BRIGHTNESS) - -# Make sure heartbeat is disabled before setting RGB LED -pycom.heartbeat(False) - -# Initialise the MFRC630 with some settings -nfc.mfrc630_cmd_init() - -def check_uid(uid, len): - return VALID_CARDS.count(uid[:len]) - -def print_debug(msg): - if DEBUG: - print(msg) - -def send_sensor_data(name, timeout): - while(pybytes): - pybytes.send_signal(2, lt.light()) - pybytes.send_signal(3, li.acceleration()) - time.sleep(timeout) - -def discovery_loop(arg1, arg2): - while(pybytes): - # Send REQA for ISO14443A card type - print_debug('Sending REQA for ISO14443A card type...') - atqa = nfc.mfrc630_iso14443a_WUPA_REQA(nfc.MFRC630_ISO14443_CMD_REQA) - print_debug('Response: {}'.format(atqa)) - if (atqa != 0): - # A card has been detected, read UID - print_debug('A card has been detected, read UID...') - uid = bytearray(10) - uid_len = nfc.mfrc630_iso14443a_select(uid) - print_debug('UID has length: {}'.format(uid_len)) - if (uid_len > 0): - print_debug('Checking if card with UID: [{:s}] is listed in VALID_CARDS...'.format(binascii.hexlify(uid[:uid_len],' ').upper())) - if (check_uid(list(uid), uid_len)) > 0: - print_debug('Card is listed, turn LED green') - pycom.rgbled(RGB_GREEN) - if(pybytes_enabled): - pybytes.send_signal(1, ('Card is listed, Access granted', uid)) - else: - print_debug('Card is not listed, turn LED red') - pycom.rgbled(RGB_RED) - if(pybytes_enabled): - pybytes.send_signal(1, ('Card is not listed, Access denied', uid)) - else: - # No card detected - print_debug('Did not detect any card...') - pycom.rgbled(RGB_BLUE) - if(pybytes_enabled): - pybytes.send_signal(1, ('Did not detect any card', 0)) - nfc.mfrc630_cmd_reset() - time.sleep(5) - nfc.mfrc630_cmd_init() - - -# This is the start of our main execution... start the thread -_thread.start_new_thread(discovery_loop, (nfc, 0)) -_thread.start_new_thread(send_sensor_data, ('Thread 2',10)) From 63e3071b7c5dfa9333277731a6d01bf2fd990491 Mon Sep 17 00:00:00 2001 From: gijsio <67470426+gijsio@users.noreply.github.com> Date: Mon, 12 Apr 2021 15:04:59 +0200 Subject: [PATCH 13/13] rm keys() --- shields/pyscan_1.py | 2 +- shields/pysense_1.py | 2 +- shields/pysense_2.py | 2 +- shields/pytrack_1.py | 2 +- shields/pytrack_2.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/shields/pyscan_1.py b/shields/pyscan_1.py index 2d2dcbb..9e9c263 100644 --- a/shields/pyscan_1.py +++ b/shields/pyscan_1.py @@ -31,7 +31,7 @@ li = LIS2HH12(py) pybytes_enabled = False -if 'pybytes' in globals().keys(): +if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True diff --git a/shields/pysense_1.py b/shields/pysense_1.py index f996e2f..2a4e126 100644 --- a/shields/pysense_1.py +++ b/shields/pysense_1.py @@ -26,7 +26,7 @@ py = Pycoproc(Pycoproc.PYSENSE) pybytes_enabled = False -if 'pybytes' in globals().keys(): +if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True diff --git a/shields/pysense_2.py b/shields/pysense_2.py index 011b269..a353076 100644 --- a/shields/pysense_2.py +++ b/shields/pysense_2.py @@ -26,7 +26,7 @@ py = Pycoproc() pybytes_enabled = False -if 'pybytes' in globals().keys(): +if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True diff --git a/shields/pytrack_1.py b/shields/pytrack_1.py index 7066ccc..cab1cb9 100644 --- a/shields/pytrack_1.py +++ b/shields/pytrack_1.py @@ -35,7 +35,7 @@ l76 = L76GNSS(py, timeout=30) pybytes_enabled = False -if 'pybytes' in globals().keys(): +if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True diff --git a/shields/pytrack_2.py b/shields/pytrack_2.py index ca21b7b..aa0af95 100644 --- a/shields/pytrack_2.py +++ b/shields/pytrack_2.py @@ -41,7 +41,7 @@ l76 = L76GNSS(py, timeout=30, buffer=512) pybytes_enabled = False -if 'pybytes' in globals().keys(): +if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True