Skip to content

Wireless With Display

Wireless Display

In this lesson, we will add a 128x64 OLED display to our Pico "W" to display network information.

The display we are using is a 2.42" Diymore OLED display as describe in the Display Graphics.

 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
78
79
80
81
82
83
84
85
86
# make a number scroll to the right and increment the number and move down one pixel
# this test patten shows the device is working and will not burn any single pixel if it runs a long time
from machine import Pin
import network
import ssd1306
import secrets
from utime import sleep, ticks_ms, ticks_diff

WIDTH = 128
HEIGHT = 64
SCK=machine.Pin(2)
SDL=machine.Pin(3)
spi=machine.SPI(0,baudrate=100000,sck=SCK, mosi=SDL)
CS = machine.Pin(0)
DC = machine.Pin(1)
RES = machine.Pin(4)
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
oled.poweron()

def mac_address_fmt():
    mac_addess = wlan.config('mac')
    s=""
    for digit in range(0,5):
        s+=str(hex(mac_addess[digit]))[2:4] + ':'
    s+= hex(mac_addess[5])[2:4]
    return s

def display_startup(counter):
    oled.fill(0)
    oled.text('Running startup', 0, 10, 1)
    oled.text('Connecting to', 0, 20, 1)
    oled.text(secrets.SSID, 0, 30, 1)
    oled.text(str(counter), 0, 40, 1)
    oled.show()

def display_status(counter):
    oled.fill(0)
    # display the network name
    oled.text('n:' + secrets.SSID, 0, 0, 1)

    # display the connection time
    oled.text('t:', 0, 10, 1)
    oled.text(str(connection_time)+ ' ms', 15, 10, 1)

    # display the MAC address
    oled.text(mac_address_fmt(), 0, 20, 1)

    # display the IP address
    oled.text('ip:' + wlan.ifconfig()[0], 0, 30, 1)
    oled.text('c:' + str(counter), 0, 40, 1)
    oled.show()
# startup
led = Pin("LED", Pin.OUT)
led.on()

start = ticks_ms() # start a millisecond counter

print('Connecting to WiFi Network Name:', secrets.SSID)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

if not wlan.isconnected():
    # this should normally take 3-5 seconds...
    wlan.connect(secrets.SSID, secrets.PASSWORD)
    print("Waiting for connection...")
    counter = 0
    display_startup(counter)
    while not wlan.isconnected():
        sleep(1)
        counter += 1
        led.toggle()
        display_startup(counter)
        print(counter, '.', sep='', end='')

connection_time = ticks_diff(ticks_ms(), start)
mac_addess = wlan.config('mac')
print('Connected to', secrets.SSID)
print('Total connect milliseconds:', connection_time)

counter = 0
while True:
    led.toggle()
    counter += 1
    display_status(counter)
    sleep(1)
    print(counter, 'listening on', wlan.ifconfig()[0])