Skip to main content

6.1. Connecting and getting data

Under construction
Short example demonstration video

One of the key features of the NULA Mini board is its built-in WiFi 6 support, making this board perfect for wireless projects. This example covers how we can use HTTP GET request to request data from a specified URL. Returned data depends on what the website or API provides. In this example we will request data from Webhook.site, which is a free online tool that let's us test and debug HTTP requests.

At the end of this example you will learn:

  • How to connect NULA Mini to existing WiFi network
  • How to make a HTTP GET request
ℹ️

Parts required:

  • Soldered NULA Mini Board
  • USB-C cable

Code Example

To connect to a WiFi network import network library, urequests will be used for HTTP GET request.

import network
import urequests

Set the ssid and password variables to hold the SSID and password of the existing WiFi network you want to connect to.

ssid = ""
password = ""

Create a WLAN network object and set the board to act as a WiFi station, in this mode the board acts as a client, connecting to an existing WiFi network.

wlan = network.WLAN(network.STA_IF)

Activate the network interface and connect to the network using ssid and password variables.

wlan.active(True)
wlan.connect(ssid, password)

To make a GET request we need internet connection, so we ensure that we do not proceed further without WiFi connection. After successful connection print out IP config parameters.

while wlan.isconnected() == False:
pass

# Print IP parameters: IP address, subnet mask, gateway and DNS server
print('Connection successful')
print(f"IP config: {wlan.ifconfig()}")

Define your Webhook URL by putting in your unique ID which can be found on their website.

request_url = "https://webhook.site/YOUR_UNIQUE_ID"

urequests.get() function sends an HTTP GET request to the specified URL and stores the result in response variable.

print(f"Making GET request to: {request_url}")
response = urequests.get(request_url)

Print out the HTTP status code returned by the server. Some common examples:

  • 200OK (request was successful)
  • 404Not found (URL doesn't exist)
  • 500Server Error
print("Response status:", response.status_code)

This prints the body (main content) of the server's response, most common responses are webpage (HTML format) and JSON data.

print("Response body:\n", response.text)

Full Example

6.1_Connection_Getting_Data.py