Skip to content

STEMAIDE AFRICA

Project 1.8.15: Wi-Fi Temperature Alert

Beginner Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython

Overview

Build a temperature alert monitor with a BME280 sensor, an LED warning, and a browser page where the alert limits can be changed.

This project demonstrates threshold-based monitoring and simple browser input handling.

The final result should light the LED and show an alert on the web page when the temperature goes above the high limit or below the low limit.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
BME280
BME280 module
LED
LED
220Ω resistor
220Ω resistor
Breadboard
Breadboard
Jumper Wires
Jumper wires
2.4 GHz Wi-Fi network Phone or browser

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
BME280 VCC 3.3V Physical pin 36
BME280 GND GND Physical pin 38
BME280 SDA GPIO 8 GPIO 8 / physical pin 11 I2C0 SDA
BME280 SCL GPIO 9 GPIO 9 / physical pin 12 I2C0 SCL
LED anode (+) 220Ω resistor then GPIO 0 GPIO 0 / physical pin 1 Alert LED
LED cathode (-) GND Physical pin 38

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Step 1


Step 2: Place the BME280 Module

Place the BME280 module on the breadboard.

Identify VCC, GND, SDA, and SCL before wiring.

Check the printed pin labels on your module.

Step 2


Step 3: Connect BME280 Power

Connect BME280 VCC to 3.3V.

Connect BME280 GND to GND.

Step 3


Step 4: Connect BME280 I2C Pins

Connect BME280 SDA to GPIO 8.

Connect BME280 SCL to GPIO 9.

Step 4


Step 5: Place the Alert LED

Place the LED on the breadboard with its legs in different rows.

The long leg is the anode (+). The short leg is the cathode (-).

Step 5

Step 6: Connect the Alert LED

Connect the LED long leg to one end of a 220Ω resistor.

Connect the other end of the resistor to GPIO 0.

Connect the LED short leg to GND

Step 6


Wiring Check

  • Pico 2W is placed correctly across the breadboard center gap
  • BME280 VCC connects to 3.3V
  • BME280 GND connects to GND
  • BME280 SDA connects to GPIO 8
  • BME280 SCL connects to GPIO 9
  • LED long leg connects through a 220Ω resistor to GPIO 0
  • LED short leg connects to GND
  • No loose jumper wires

Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

I2C scanner test

Check that the BME280 appears on the I2C bus.

from machine import I2C, Pin
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
print([hex(addr) for addr in i2c.scan()])

Expected test result: You should usually see the BME280 address 0x76 or 0x77.


BME280 temperature test

Check that the BME280 returns a temperature value.

from machine import I2C, Pin
import BME280

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
try:
    bme = BME280.BME280(i2c=i2c, address=0x76)
except OSError:
    bme = BME280.BME280(i2c=i2c, address=0x77)
print('Temperature:', bme.temperature)

Expected test result: The Shell should print a temperature value.


LED test

Check that the alert LED works before using the full project code.

from machine import Pin
import time
led = Pin(0, Pin.OUT)
led.on()
time.sleep(1)
led.off()

Expected test result: The LED should turn on briefly and then turn off.


Wi-Fi connection test

Check that the Pico connects to Wi-Fi and prints its IP address.

import network
import time
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(15):
    if wlan.isconnected():
        break
    print('Connecting...')
    time.sleep(1)
print('Connected:', wlan.isconnected())
if wlan.isconnected():
    print('IP address:', wlan.ifconfig()[0])

Expected test result: The Shell should show Connected: True and print an IP address.


Full Project Code

Upload and run this code after the individual tests work correctly.

import network
import socket
import time
from machine import I2C, Pin
import BME280

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

led = Pin(0, Pin.OUT)
high_temp = 30.0
low_temp = 18.0

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
try:
    bme = BME280.BME280(i2c=i2c, address=0x76)
except OSError:
    bme = BME280.BME280(i2c=i2c, address=0x77)


def current_temp_c():
    temp_text = str(bme.temperature).replace('C', '')
    return float(temp_text)


def parse_param(request, name, default):
    if name + '=' not in request:
        return default
    start = request.find(name + '=') + len(name) + 1
    end = request.find(' ', start)
    value_text = request[start:end].split('&')[0]
    try:
        return float(value_text)
    except ValueError:
        return default


def web_page(temp_value, high_value, low_value, alert_active):
    status = 'ALERT!' if alert_active else 'Normal'
    return '''<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <meta http-equiv='refresh' content='3'>
    <title>Temperature Alert</title>
</head>
<body style='font-family:Arial;text-align:center;padding:30px'>
    <h1>Wi-Fi Temperature Alert</h1>
    <h2>{:.1f} C</h2>
    <p>Status: {}</p>
    <form>
        High: <input type='number' name='high' value='{}' step='0.5'>
        Low: <input type='number' name='low' value='{}' step='0.5'>
        <button type='submit'>Update</button>
    </form>
</body>
</html>'''.format(temp_value, status, high_value, low_value)


wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))

address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)

while True:
    client, client_address = server.accept()
    request = client.recv(1024).decode()

    high_temp = parse_param(request, 'high', high_temp)
    low_temp = parse_param(request, 'low', low_temp)

    temp_value = current_temp_c()
    alert = temp_value > high_temp or temp_value < low_temp
    led.value(1 if alert else 0)

    response = web_page(temp_value, high_temp, low_temp, alert)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
current_temp_c() Reads the BME280 temperature and converts it to a float The code needs a number to compare with thresholds
parse_param() Reads the high and low values from the browser request This lets the user change the limits without editing the code
Threshold check Decides whether temperature is outside the safe range This is the alert logic for the project
LED alert Turns the LED on when the temperature is outside limits The project gives a quick local warning without opening the browser

Expected Result

After entering your Wi-Fi details and running the code, the browser page should show the current temperature and the current high and low limits. When the temperature goes above the high limit or below the low limit, the LED should turn on and the page should show an alert.


Troubleshooting

Problem Possible Cause Solution
Temperature never changes Sensor is not responding or the text is not converted correctly Run the BME280 temperature test first and recheck current_temp_c()
Threshold update does not work The browser values are not being parsed properly Watch the request handling and try simple values like 25 and 20
LED never lights Alert condition is not reached or LED wiring is wrong Use easy test values like a very low high limit and recheck the LED wiring