Skip to main content

Wi-Fi network

What this example shows​

Connect to your Wi-Fi network, fetch a small test page from micropython.org with a plain HTTP GET request, and print the whole response on the screen. Before you run it, put your network name and password in ssid and password.

This example also uses MicroPython's own network and socket modules (network basics), which are not part of the Inkplate modules.

Example code​

"""Connect to WiFi and fetch data from the internet."""

# Include needed libraries
from inkplate7spectra import Inkplate

# Enter your WiFi credentials here
ssid = "YOUR_SSID_HERE"
password = "YOUR_PASSWORD_HERE"


# Function which connects to WiFi
# More info here: https://docs.micropython.org/en/latest/esp8266/tutorial/network_basics.html
def do_connect():
import network

sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print("connecting to network...")
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print("network config:", sta_if.ifconfig())


# This function does a HTTP GET request
# More info here: https://docs.micropython.org/en/latest/esp8266/tutorial/network_tcp.html
def http_get(url):
import socket

res = ""
_, _, host, path = url.split("/", 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host), "utf8"))
while True:
data = s.recv(100)
if data:
res += str(data, "utf8")
else:
break
s.close()
return res


# First, connect
do_connect()

# Do a GET request to the micropython test page
# If you were to do a GET request to a different page/resource, change the URL here
response = http_get("http://micropython.org/ks/test.html")

# Create and initialize our Inkplate object
inkplate = Inkplate()
inkplate.begin()

# Set text size to double from the original size, so we can see the text better
inkplate.set_text_size(1)

# Print response line by line
inkplate.print(response)

# Display image from buffer in full refresh
inkplate.display()

Functions used​

network.WLAN(network.STA_IF)returns WLAN

MicroPython: the Wi-Fi station interface. The example calls isconnected(), active(True), connect(ssid, password) and ifconfig() on it.

ReturnsThe interface object.

socket.socket()returns socket

MicroPython: a TCP socket. The example resolves the host with socket.getaddrinfo(), then calls connect(), send(), recv(100) until the server closes, and close().

ReturnsThe socket object.

Inkplate()returns Inkplate

Creates the display object from the inkplate7spectra module.

ReturnsThe display object.

inkplate.begin()returns bool

Sets up the board and a white frame buffer. Call it once.

ReturnsTrue.

inkplate.set_text_size(s)returns None

Sets the text scale factor, here 1 (the comment above it says double, the value is 1).

ReturnsNothing.

inkplate.print(text)returns None

Draws the text at the cursor, starting at the top-left corner. A newline in the text starts a new line.

ReturnsNothing.

inkplate.display(leave_on=False)returns None

Sends the frame buffer to the panel and refreshes it.

ReturnsNothing.

What to expect​

The shell prints connecting to network... (only if the board was not connected yet), then network config: followed by the board's network settings. After the refresh, the screen shows the HTTP response from the test page: the headers first, then the page's HTML, as text.

🔗Open the complete example

examples/inkplate7spectra/example_network.py

→