Project 2.1.23: Remote Farm Monitoring Station
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a Wi-Fi monitoring station that serves live farm data to a browser on the same network.
Students will combine soil moisture, temperature, humidity, Wi-Fi connection setup, and a simple browser dashboard into one monitoring system.
The final system should connect to Wi-Fi, show an IP address in the Thonny Shell, and serve an auto-refreshing web page with live readings and a rule-based condition summary.
Required Components
![]() Soil moisture sensor (analog output) |
![]() Raspberry Pi Pico 2 W |
![]() BME280 sensor module |
![]() Built-in Pico status LED |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | Physical pin 36 | Physical pin 36 | Sensor power |
| Soil sensor GND | Any GND pin | Any GND pin | Common ground |
| Soil sensor AOUT | GPIO 26 / physical pin 31 | GPIO 26 / physical pin 31 | Analog moisture input |
| BME280 VCC | 3.3V | Physical pin 36 | Sensor power |
| BME280 GND | GND | Any GND pin | Common ground |
| BME280 SDA | GPIO 2 | GPIO 2 / physical pin 4 | I2C data |
| BME280 SCL | GPIO 3 | GPIO 3 / physical pin 5 | I2C clock |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
│ GPIO 2 ───────────┤──── BME280 SDA
│ GPIO 3 ───────────┤──── BME280 SCL
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ 3.3V ───────────┤──── BME280 VCC
│ │
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── BME280 GND
│ │
│ Built-in LED (used in code for Wi-Fi status, no wiring needed)
└─────────────────────┘
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 2: Position the Soil Moisture Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Identify VCC, GND, and AOUT / AO / Signal before wiring.
Step 3: Connect the Soil Sensor
Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26 (ADC0).
Step 4: Place the BME280 Sensor Module
Place the BME280 module where it can sense the farm-area air. Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3. If your BME280 is bare, use the module pins labeled VCC, GND, SDA, and SCL.
Step 5: Prepare for Wi-Fi Testing
The built-in Pico LED is used in the code, so no external status LED wiring is needed. Prepare your Wi-Fi name and password in your local code copy before running the web page.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor VCC connects to 3.3V
- [x] Soil sensor GND connects to GND
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
- [x] Pico built-in LED is used for Wi-Fi status
- [x] Pico 2W is connected to Wi-Fi for the web page
- [x] No loose jumper wires
Intermediate Note When running the networking part, the Pico 2W must be connected to Wi-Fi. Keep real Wi-Fi credentials private and enter them only in your local copy of the code. The built-in LED should show the Wi-Fi connection state. Confirm local sensor readings before debugging the web page.
Safety Note Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Keep the soil sample away from the USB connection.
Testing Individual Components
Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.
Hardware Setup
- Build the soil sensor and BME280 circuit before attempting Wi-Fi setup
- Keep the wiring neat because network problems are easier to solve when hardware is already stable
Test the Input Sensor
- Read soil values in dry and wet conditions so you can tune SOIL_DRY and SOIL_WET
- Read the BME280 values in the Shell and confirm the temperature and humidity look realistic
Test the Output Device
- Confirm that the built-in Pico LED blinks during connection and stays on after Wi-Fi connects
- Check that the browser page refreshes and updates the displayed values
Test Communication
- Enter your Wi-Fi credentials, run the code, and wait for the IP address to appear in the Thonny Shell
- Open the IP address in a browser on the same network and confirm that the dashboard loads
Run the Full System
- Change one condition at a time, such as soil moisture or temperature, and observe how the page values update
- Confirm that the condition summary changes appropriately when the system becomes dry or hot and dry
Save the Project
- Save the final code locally without exposing real credentials in shared class materials
- Write down the IP address behaviour and threshold values that worked on your network
Additional Testing and Calibration Checks
- Dry/wet reading test: record soil values in dry and wet conditions so the soil percentage is realistic
- Normal condition test: confirm the dashboard shows stable room values when the environment is unchanged
- Threshold condition test: dry the soil or warm the BME280 slightly and confirm the condition summary changes
- Wi-Fi setup test: make sure the Pico and browser device are on the same 2.4 GHz network
- Response test: refresh the page manually once and then confirm the automatic refresh continues to update the readings
Full Project Code
After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
import bme280
import network
import socket
import time
from machine import ADC, I2C, Pin
WIFI_SSID = 'YOUR_WIFI_NAME'
WIFI_PASSWORD = 'YOUR_WIFI_PASSWORD'
soil_sensor = ADC(26)
i2c_bme = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
bme = bme280.BME280(i2c=i2c_bme)
def number_from_bme(text_value):
cleaned = "".join(ch for ch in str(text_value) if ch.isdigit() or ch in ".-")
return float(cleaned)
def read_bme280():
temp_text, pressure_text, humidity_text = bme.values
return number_from_bme(temp_text), number_from_bme(humidity_text), number_from_bme(pressure_text)
status_led = Pin('LED', Pin.OUT)
SOIL_DRY = 52000
SOIL_WET = 22000
REFRESH_SECONDS = 5
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def soil_percent():
raw = soil_sensor.read_u16()
span = SOIL_DRY - SOIL_WET
if span <= 0:
return 0
percent = int(((SOIL_DRY - raw) * 100) / span)
return clamp(percent, 0, 100)
def read_environment():
try:
temp_c, humidity, pressure_hpa = read_bme280()
return temp_c, humidity
except Exception as error:
print('BME28022 read error:', error)
return None, None
def plant_state(moisture, temp_c, humidity):
if temp_c is not None and moisture < 35 and temp_c > 30:
return 'HOT AND DRY'
if moisture < 35:
return 'DRY'
if humidity is not None and humidity > 85:
return 'TOO HUMID'
return 'NORMAL'
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('Connecting to Wi-Fi...')
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
start_time = time.time()
while not wlan.isconnected():
status_led.value(1 - status_led.value())
if time.time() - start_time > 20:
raise RuntimeError('Wi-Fi connection timeout')
time.sleep(0.5)
status_led.value(1)
print('Wi-Fi connected:', wlan.ifconfig()[0])
return wlan
def html_page(ip_address, moisture, temp_c, humidity, state):
temp_text = '--' if temp_c is None else '{} C'.format(temp_c)
humidity_text = '--' if humidity is None else '{} %'.format(humidity)
return """<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta http-equiv='refresh' content='{}'>
<title>Remote Farm Monitoring Station</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; background: #f5fbf4; }}
h1 {{ color: #214d2f; }}
.card {{ background: white; border-radius: 10px; padding: 14px; margin-bottom: 12px; box-shadow: 0 0 4px rgba(0,0,0,0.1); }}
.state {{ font-weight: bold; color: #0d5c3f; }}
</style>
</head>
<body>
<h1>Remote Farm Monitoring Station</h1>
<div class='card'>IP Address: {}</div>
<div class='card'>Soil Moisture: {}%</div>
<div class='card'>Temperature: {}</div>
<div class='card'>Humidity: {}</div>
<div class='card state'>Condition: {}</div>
</body>
</html>""".format(REFRESH_SECONDS, ip_address, moisture, temp_text, humidity_text, state)
wlan = connect_wifi()
ip_address = wlan.ifconfig()[0]
address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)
print('Open http://{}/ in a browser on the same Wi-Fi network.'.format(ip_address))
while True:
client, remote_address = server.accept()
print('Browser connected from:', remote_address)
request = client.recv(1024)
moisture = soil_percent()
temp_c, humidity = read_environment()
state = plant_state(moisture, temp_c, humidity)
response = html_page(ip_address, moisture, temp_c, humidity, state)
client.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n')
client.sendall(response.encode())
client.close()
print('Served page | Soil: {}% | Temp: {} | Humidity: {} | State: {}'.format(
moisture, temp_c, humidity, state
))
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| connect_wifi() | Connects the Pico to the local Wi-Fi network | The network must be working before the browser dashboard can be tested |
| soil_percent() and read_environment() | Read moisture, temperature, and humidity sensors | These readings are the live data shown on the dashboard |
| plant_state() | Applies rules to classify the overall condition | A simple rule-based summary is easier to interpret than raw numbers alone |
| html_page() and socket server | Builds and serves the browser dashboard page | This creates the remote monitoring function of the project |
Expected Result
After a successful Wi-Fi connection, the Thonny Shell should show an IP address and the built-in Pico LED should stay on.
When that IP address is opened in a browser on the same network, the page should show soil moisture, temperature, humidity, and a condition summary.
As sensor conditions change, the browser page should refresh and update the displayed values automatically.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The page never loads | Network connectivity issue | Check the Shell for the IP address and ensure both devices are on the same 2.4 GHz Wi-Fi network |
| The IP address does not appear | Wi-Fi credentials are incorrect | Re-enter the Wi-Fi placeholders carefully in your local copy of the code |
| The dashboard shows 0 or missing climate values | BME280 wiring or read timing issue | Check the GPIO 2/3 wiring and make sure the sensor is not being read too quickly |
| The soil percentage looks wrong | Calibration values do not match sensor | Measure real dry and wet values and update SOIL_DRY and SOIL_WET |




