Skip to content

Commit c782ea5

Browse files
authored
Merge branch 'master' into master
2 parents 909d483 + b59f50e commit c782ea5

File tree

15 files changed

+894
-5
lines changed

15 files changed

+894
-5
lines changed

IoT_Environment_Sensor/secrets.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# If you put them in the code you risk committing that info or sharing it
33

44
secrets = {
5-
'ssid' : b'Shibari',
6-
'password' : b'$k7cGE$7=jY67kg#',
7-
'timezone' : 'America/Toronto',
8-
'aio_username' : 'dastels',
9-
'aio_key' : '796252b4b3484ed3b00665fab73de47c7fb8ea9e',
5+
"ssid": b"My_SSID",
6+
"password": b"My_WIFI_Password",
7+
"timezone": "Area/City",
8+
"aio_username": "my_username",
9+
"aio_key": "my_key",
1010
}

MagTag_Dishwasher_Status/clean.bmp

18.6 KB
Binary file not shown.

MagTag_Dishwasher_Status/dirty.bmp

38.1 KB
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
import board
3+
import alarm
4+
import displayio
5+
6+
# get the display
7+
epd = board.DISPLAY
8+
epd.rotation = 270
9+
10+
# set up pin alarms
11+
buttons = (board.BUTTON_A, board.BUTTON_B) # pick any two
12+
pin_alarms = [alarm.pin.PinAlarm(pin=pin, value=False, pull=True) for pin in buttons]
13+
14+
# toggle saved state
15+
alarm.sleep_memory[0] = not alarm.sleep_memory[0]
16+
17+
# set bitmap
18+
bmp_file = "clean.bmp" if alarm.sleep_memory[0] else "dirty.bmp"
19+
20+
# show bitmap
21+
with open(bmp_file, "rb") as fp:
22+
bitmap = displayio.OnDiskBitmap(fp)
23+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
24+
group = displayio.Group(max_size=1)
25+
group.append(tile_grid)
26+
epd.show(group)
27+
time.sleep(epd.time_to_refresh + 0.01)
28+
epd.refresh()
29+
while epd.busy:
30+
pass
31+
32+
# go to sleep
33+
alarm.exit_and_deep_sleep_until_alarms(*pin_alarms)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import time
2+
import board
3+
import alarm
4+
import displayio
5+
import adafruit_lis3dh
6+
7+
# get the display
8+
epd = board.DISPLAY
9+
10+
# set up accelerometer
11+
lis = adafruit_lis3dh.LIS3DH_I2C(board.I2C(), address=0x19)
12+
13+
# See: ST Design Tip DT0008 - Simple screen rotation using
14+
# the accelerometer built-in 4D detection interrupt
15+
# pylint: disable=protected-access
16+
lis._write_register_byte(0x20, 0x3F) # low power mode with ODR = 25Hz
17+
lis._write_register_byte(0x22, 0x40) # AOI1 interrupt generation is routed to INT1 pin
18+
lis._write_register_byte(0x23, 0x80) # FS = ±2g low power mode with BDU bit enabled
19+
lis._write_register_byte(
20+
0x24, 0x0C
21+
) # Interrupt signal on INT1 pin is latched with D4D_INT1 bit enabled
22+
lis._write_register_byte(
23+
0x32, 0x20
24+
) # Threshold = 32LSBs * 15.625mg/LSB = 500mg. (~30 deg of tilt)
25+
lis._write_register_byte(0x33, 0x01) # Duration = 1LSBs * (1/25Hz) = 0.04s
26+
27+
# read to clear
28+
_ = lis._read_register_byte(0x31)
29+
30+
# get current accel values
31+
_, y, _ = lis.acceleration
32+
33+
# update based on orientation
34+
if y > 0:
35+
# upside up
36+
bmp_file = "clean.bmp"
37+
rotation = 270
38+
irq_config = 0b01000100
39+
else:
40+
# upside down
41+
bmp_file = "dirty.bmp"
42+
rotation = 90
43+
irq_config = 0b01001000
44+
45+
# show bitmap
46+
epd.rotation = rotation
47+
with open(bmp_file, "rb") as fp:
48+
bitmap = displayio.OnDiskBitmap(fp)
49+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
50+
group = displayio.Group(max_size=1)
51+
group.append(tile_grid)
52+
epd.show(group)
53+
time.sleep(epd.time_to_refresh + 0.01)
54+
epd.refresh()
55+
while epd.busy:
56+
pass
57+
58+
# config accelo irq
59+
lis._write_register_byte(0x30, irq_config)
60+
61+
# go to sleep
62+
pin_alarm = alarm.pin.PinAlarm(pin=board.ACCELEROMETER_INTERRUPT, value=True)
63+
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# SPDX-FileCopyrightText: 2021 Brent Rubell, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
import ssl
5+
import board
6+
import wifi
7+
import socketpool
8+
import adafruit_requests as requests
9+
from adafruit_oauth2 import OAuth2
10+
from adafruit_display_text.label import Label
11+
from adafruit_bitmap_font import bitmap_font
12+
from adafruit_magtag.magtag import Graphics
13+
from adafruit_display_shapes.rect import Rect
14+
15+
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
16+
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
17+
# source control.
18+
# pylint: disable=no-name-in-module,wrong-import-order
19+
try:
20+
from secrets import secrets
21+
except ImportError:
22+
print("Credentials and tokens are kept in secrets.py, please add them there!")
23+
raise
24+
25+
print("Connecting to %s" % secrets["ssid"])
26+
wifi.radio.connect(secrets["ssid"], secrets["password"])
27+
print("Connected to %s!" % secrets["ssid"])
28+
29+
pool = socketpool.SocketPool(wifi.radio)
30+
requests = requests.Session(pool, ssl.create_default_context())
31+
32+
# DisplayIO setup
33+
font_small = bitmap_font.load_font("/fonts/Arial-12.pcf")
34+
font_large = bitmap_font.load_font("/fonts/Arial-14.pcf")
35+
36+
graphics = Graphics(auto_refresh=False)
37+
display = graphics.display
38+
39+
background = Rect(0, 0, 296, 128, fill=0xFFFFFF)
40+
graphics.splash.append(background)
41+
42+
label_overview_text = Label(
43+
font_large,
44+
x=0,
45+
y=10,
46+
line_spacing=0.75,
47+
color=0x000000,
48+
text="Authorize this device with Google:",
49+
)
50+
graphics.splash.append(label_overview_text)
51+
52+
label_verification_url = Label(
53+
font_small, x=0, y=40, line_spacing=0.75, color=0x000000, max_glyphs=90
54+
)
55+
graphics.splash.append(label_verification_url)
56+
57+
label_user_code = Label(
58+
font_small, x=0, y=80, color=0x000000, line_spacing=0.75, max_glyphs=50
59+
)
60+
graphics.splash.append(label_user_code)
61+
62+
label_qr_code = Label(
63+
font_small, x=0, y=100, color=0x000000, text="Or scan the QR code:"
64+
)
65+
graphics.splash.append(label_qr_code)
66+
67+
# Set scope(s) of access required by the API you're using
68+
scopes = ["https://www.googleapis.com/auth/calendar.readonly"]
69+
70+
# Initialize an OAuth2 object
71+
google_auth = OAuth2(
72+
requests, secrets["google_client_id"], secrets["google_client_secret"], scopes
73+
)
74+
75+
# Request device and user codes
76+
# https://developers.google.com/identity/protocols/oauth2/limited-input-device#step-1:-request-device-and-user-codes
77+
google_auth.request_codes()
78+
79+
# Display user code and verification url
80+
# NOTE: If you are displaying this on a screen, ensure the text label fields are
81+
# long enough to handle the user_code and verification_url.
82+
# Details in link below:
83+
# https://developers.google.com/identity/protocols/oauth2/limited-input-device#displayingthecode
84+
print(
85+
"1) Navigate to the following URL in a web browser:", google_auth.verification_url
86+
)
87+
print("2) Enter the following code:", google_auth.user_code)
88+
label_verification_url.text = (
89+
"1. On your computer or mobile device,\ngo to %s" % google_auth.verification_url
90+
)
91+
label_user_code.text = "2. Enter code: %s" % google_auth.user_code
92+
93+
graphics.qrcode(google_auth.verification_url.encode(), qr_size=2, x=240, y=70)
94+
board.DISPLAY.show(graphics.splash)
95+
display.refresh()
96+
97+
# Poll Google's authorization server
98+
print("Waiting for browser authorization...")
99+
if not google_auth.wait_for_authorization():
100+
raise RuntimeError("Timed out waiting for browser response!")
101+
102+
print("Successfully Authenticated with Google!")
103+
print("Add the following lines to your secrets.py file:")
104+
print("\t'google_access_token' " + ":" + " '%s'," % google_auth.access_token)
105+
print("\t'google_refresh_token' " + ":" + " '%s'" % google_auth.refresh_token)
106+
107+
graphics.splash.pop()
108+
graphics.splash.pop()
109+
graphics.splash.pop()
110+
111+
label_overview_text.text = "Successfully Authenticated!"
112+
label_verification_url.text = (
113+
"Check the REPL for tokens to add\n\tto your secrets.py file"
114+
)
115+
display.refresh()

0 commit comments

Comments
 (0)