-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_oled.py
77 lines (62 loc) · 1.93 KB
/
wifi_oled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import machine
import ssd1306
import time
import network
def wifi_connection(file, max):
"""Connect to Wifi"""
ip = "Not Connected"
f = open(file, "r")
wifi_details = f.readline()
f.close()
ssid = wifi_details.split(" ")[0]
password = wifi_details.split(" ")[1]
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
print("Connection to", ssid)
max_wait = max
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print("waiting for connection...")
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError("network connection failed")
else:
print("connected")
ip = wlan.ifconfig()[0]
print("ip = " + ip)
return ip
def setup_display(sda, scl, x=128, y=64, freq=400000):
"""Setup I2C Display"""
i2c = machine.I2C(0, sda=sda, scl=scl, freq=freq)
return ssd1306.SSD1306_I2C(x, y, i2c)
def update_display(display, lines, y_offset=2):
"""Update the Display"""
clear_display(display)
frames = [[0,0,128,16,1],[0,17,127,46,1]]
draw_frames(frames)
y = y_offset
for line in lines:
display.text(line, 1, y)
y += 16
display.show()
def clear_display(display):
"""Clear the display"""
display.fill(0)
display.show()
def draw_frames(display, frames):
"""Draw frames on display"""
for frame in frames:
display.rect(frame[0], frame[1], frame[2], frame[3], frame[4])
if __name__ == "__main__":
sda = machine.Pin(0)
scl = machine.Pin(1)
display = setup_display(sda, scl)
update_display(display, ["Connecting..."])
ip_address = wifi_connection("wifi.txt", 10)
lines = [ip_address, "Hello", "World"]
update_display(display, lines)