Skip to main content

6.3. Sending data

Under construction
Short example demonstration video

Sometimes when measuring sensor data we want to store it for later analysis or monitor devices remotely. IoT (Internet of Things) refers to a network of physical devices that talk to each other over internet. These devices often use an HTTP POST method to send data to a web server. In this example we will be using NULA Mini's built-in WiFi to send data in JSON format to Webhook.site.

At the end of this example you will learn:

  • How to save measured data in JSON format
  • How to send a POST request to specific URL and handle the response
ℹ️

Parts required:

  • Soldered NULA Mini board
  • Breadboard
  • SHTC3 sensor
  • 1 x Qwiic cable
  • WiFi connection
Under construction
Required components

Putting the components together

  • Use a Qwiic cable to connect the SHTC3 sensor to NULA Mini
Under construction
Components connected together

Code Example

Start by including necessary libraries: network used to handle WiFi connection, urequests for handling requests to web server, random to generate random number.

import network
import urequests
import time
import random

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

ssid = ""
password = ""

Insert your unique ID in the URL which can be found on their website.

WEBHOOK_URL = "https://webhook.site/YOUR-UNIQUE-ID"

Create a WLAN network object and set the board to act as a WiFi station. Activate the network interface, initialize connection to the network using ssid and password variables and wait until the board is connected before proceeding.

wlan = network.WLAN(network.STA_IF)

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

while wlan.isconnected() == False:
pass

After successful connection print the IP network parameters (IP address, subnet mask, gateway and DNS server).

print('Connection successful')
print(f"IP config: {wlan.ifconfig()}")

Declare timer variable to send data every 10 seconds, setting it this way instead of last_send_time = 0 sends data immediately.

last_send_time = time.ticks_ms() - 10000

If 10 seconds passed, generate random number and get the timestamp and store them in dictionaty variable. Then send a POST request to specified URL with data in JSON format. You should then get 200 - OK successful status code by server.

# Main loop
while True:
# If ten seconds has passed, send data via POST request
if time.ticks_diff(time.ticks_ms(), last_send_time) > 10000:
# Store data to send in dictionary format: random value and timestamp
random_value = random.randint(0, 100)
data = {
"value" : random_value,
"timestamp" : time.ticks_ms()
}
try:
# Send a POST request to the specified URL with data in JSON format
response = urequests.post(WEBHOOK_URL, json=data)

# Print HTTP status code returned by the server
print("POST status: ", response.status_code)
# Print the response body returned by the server
print("POST body: ", response.text)
# Close the response to free up memory
response.close()

# Catch any exception (network issue, timeout, ...) and print it out
except Exception as e:
print("POST failed: ", e)

# Update the timestamp of last time data was sent
last_send_time = time.ticks_ms()

# Add small delay to program
time.sleep_ms(10)

Full Example

6.2_Send_Measured_Data_WiFi.py