瀏覽代碼

wifi switcher

seamlessly choose between saved wifi options on cardputer boot. write ssid and password combinations in settings.toml then just add them to the (list of) list "wifi_options". the menu will adjust accordingly so add as many or as few as you like. menu will in theory go off the screen if it is a long list
cube 1 月之前
父節點
當前提交
0094d9502c
共有 1 個檔案被更改,包括 146 行新增0 行删除
  1. 146
    0
      CARDPUTER CircuitPython/wifi_switcher/code.py

+ 146
- 0
CARDPUTER CircuitPython/wifi_switcher/code.py 查看文件

@@ -0,0 +1,146 @@
1
+from keyb import Keyboard # type: ignore
2
+import time, os, rtc, json, wifi, supervisor # type: ignore
3
+import board, busio, sdcardio, storage, terminalio, displayio # type: ignore
4
+from adafruit_display_text import label # type: ignore
5
+from adafruit_display_shapes.rect import Rect # type: ignore
6
+#import pytumblr # type: ignore
7
+import tumblrapi # type: ignore
8
+
9
+### DEFAULT HOME PAGE # # # # # # # # # #
10
+def home():
11
+    title_label.text = "Home"
12
+    title_label.x = 10
13
+    title_label.y = 14
14
+
15
+    ip_label.text = my_ip
16
+    ip_label.x = 160
17
+    ip_label.y = 20
18
+
19
+    page_label.text = """
20
+1 - User Info
21
+2 - Make Post 
22
+    """
23
+    page_label.x = 5
24
+    page_label.y = 30
25
+
26
+# # # # # # # # # # # # # # # # # # 
27
+
28
+keyb = Keyboard()
29
+
30
+
31
+# SET UP DISPLAY & WIFI
32
+display = board.DISPLAY
33
+display_group = displayio.Group()
34
+display.root_group = display_group
35
+
36
+wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
37
+                [os.getenv("WIFI2"),os.getenv("WIFIPW2")],
38
+                [os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
39
+
40
+
41
+h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
42
+h1.x = 10
43
+h1.y = 15
44
+
45
+count = 0
46
+menu_labs = []
47
+for option in wifi_options:
48
+    if count == 0:
49
+        menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
50
+    else:
51
+        menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
52
+    menu_lab.x = 10
53
+    menu_lab.y = 35 + count * 10
54
+    display.root_group.append(menu_lab)
55
+    menu_labs.append(menu_lab)
56
+    count+=1
57
+
58
+display.root_group.append(h1)
59
+
60
+pos = 0
61
+while True:
62
+    key = keyb.scan()
63
+    if key == "UP":
64
+        if pos == 0:
65
+            pass
66
+        else:
67
+            menu_labs[pos].text = wifi_options[pos][0]
68
+            pos -= 1
69
+        menu_labs[pos].text = "> " + wifi_options[pos][0]
70
+    elif key == "DOWN":
71
+        if pos == len(wifi_options)-1:
72
+            pass
73
+        else:
74
+            menu_labs[pos].text = wifi_options[pos][0]
75
+            pos += 1
76
+        menu_labs[pos].text = "> " + wifi_options[pos][0]
77
+    elif key == "\n":
78
+        WIFI_SSID = wifi_options[pos][0]
79
+        WIFI_PASS = wifi_options[pos][1]
80
+        break
81
+
82
+
83
+# SET UP WIFI
84
+try:
85
+    wifi.radio.connect(ssid=WIFI_SSID,
86
+                    password=WIFI_PASS)
87
+    my_ip = str(wifi.radio.ipv4_address).strip()
88
+    display.root_group.remove(h1)
89
+    for lab in menu_labs:
90
+        display.root_group.remove(lab)
91
+except ConnectionError as e:
92
+    txt = label.Label(terminalio.FONT, text = "", color=0xff0000, scale=1)
93
+    txt.text = "Could not connect to " + WIFI_SSID
94
+    txt.x = 10
95
+    txt.y = 100
96
+    display.root_group.append(txt)
97
+    time.sleep(10)
98
+    supervisor.reload()
99
+
100
+# # # # # # # # # # # # # # # # # #
101
+
102
+
103
+# SET UP SD CARD
104
+spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
105
+cs = board.SD_CS
106
+
107
+try:
108
+    sdcard = sdcardio.SDCard(spi, cs)
109
+    vfs = storage.VfsFat(sdcard)
110
+
111
+    storage.mount(vfs, "/sd") # access files on sd card here
112
+except OSError:
113
+    pass # SD card not inserted/found
114
+
115
+
116
+
117
+
118
+# Header Bar
119
+rect = Rect(0, 0, 238, 30, fill=0x1f4476)
120
+display.root_group.append(rect)
121
+
122
+# Page Title
123
+title_font = terminalio.FONT
124
+title_color = 0xffffff
125
+title_label = label.Label(title_font, text="Home", color=title_color, scale=2)
126
+display.root_group.append(title_label)
127
+
128
+
129
+# Page Text
130
+page_font = terminalio.FONT
131
+page_color = 0xffffff
132
+page_label = label.Label(page_font, text="page_text", color=page_color, scale=1)
133
+display.root_group.append(page_label)
134
+
135
+# IP Label
136
+ip_font = terminalio.FONT
137
+ip_color = 0x89a0a8
138
+ip_label = label.Label(ip_font, text="0.0.0.0", color=ip_color, scale=1)
139
+display.root_group.append(ip_label)
140
+
141
+home()
142
+
143
+
144
+## PROGRAM MAIN LOOP (WORKS FROM HOME PAGE IF OTHER PAGES HAVE THEIR OWN LOOPS)
145
+while True:
146
+    key = keyb.scan()