Skip to content

Commit b021901

Browse files
author
brentru
committed
add code workign with pyotp
1 parent 253c54a commit b021901

1 file changed

Lines changed: 62 additions & 5 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
import adafruit_hashlib as hashlib
88
from adafruit_binascii import hexlify, unhexlify
99

10+
# Get wifi details and more from a secrets.py file
11+
try:
12+
from secrets import secrets
13+
except ImportError:
14+
print("WiFi secrets are kept in secrets.py, please add them there!")
15+
raise
16+
1017
# https://github.com/pyotp/pyotp example
11-
totp = [("Discord ", 'JBSWY3DPEHPK3PXP'),
12-
("Gmail ", 'abcdefghijklmnopqrstuvwxyz234567'),
13-
("Accounts", 'asfdkwefoaiwejfa323nfjkl')]
14-
ssid = 'my_wifi_ssid'
15-
password = 'my_wifi_password'
18+
totp = [("Gmail ", 'JBSWY3DPEHPK3PXP')]
19+
1620

1721
TEST = True # if you want to print out the tests the hashers
1822
ALWAYS_ON = False # Set to true if you never want to go to sleep!
@@ -24,6 +28,16 @@
2428
# Create a SHA1 Object
2529
SHA1 = hashlib.sha1
2630

31+
# PyPortal ESP32 AirLift Pins
32+
esp32_cs = DigitalInOut(board.ESP_CS)
33+
esp32_ready = DigitalInOut(board.ESP_BUSY)
34+
esp32_reset = DigitalInOut(board.ESP_RESET)
35+
36+
# Initialize PyPortal ESP32 AirLift
37+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
38+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
39+
40+
2741
if TEST:
2842
print("===========================================")
2943
sha1_output = hexlify(SHA1(b'hello world').digest())
@@ -118,3 +132,46 @@ def generate_otp(int_input, secret_key, digits=6):
118132
return str_code
119133

120134
print("===========================================")
135+
136+
137+
print("Connecting to AP...")
138+
while not esp.is_connected:
139+
try:
140+
esp.connect_AP(secrets['ssid'], secrets['password'])
141+
except RuntimeError as e:
142+
print("could not connect to AP, retrying: ", e)
143+
continue
144+
145+
print("Connected to SSID: ", secrets['ssid'])
146+
147+
# Initialize the NTP object
148+
ntp = NTP(esp)
149+
150+
# Fetch and set the microcontroller's current UTC time
151+
# keep retrying until a valid time is returned
152+
while not ntp.valid_time:
153+
ntp.set_time()
154+
print("Failed to obtain time, retrying in 15 seconds...")
155+
time.sleep(15)
156+
157+
# Get the current time in seconds since Jan 1, 1970
158+
t = time.time()
159+
print("Seconds since Jan 1, 1970: {} seconds".format(t))
160+
161+
# Instead of using RTC which means converting back and forth
162+
# we'll just keep track of seconds-elapsed-since-NTP-call
163+
mono_time = int(time.monotonic())
164+
print("Monotonic time", mono_time)
165+
166+
countdown = ON_SECONDS # how long to stay on if not in always_on mode
167+
while ALWAYS_ON or (countdown > 0):
168+
# Calculate current time based on NTP + monotonic
169+
unix_time = t - mono_time + int(time.monotonic())
170+
print("Unix time: ", unix_time)
171+
# We can do up to 3 per line on the Feather OLED
172+
for name, secret in totp:
173+
otp = generate_otp(unix_time // 30, secret)
174+
print(name + " OTP output: ", otp) # serial debugging output
175+
# We'll update every 1/4 second, we can hash very fast so its no biggie!
176+
countdown -= 0.25
177+
time.sleep(0.25)

0 commit comments

Comments
 (0)