Project 3.4.1: Climate Logger
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico reads temperature and humidity, stores local log entries, and can optionally send the same data to a local Wi-Fi endpoint.
Environmental monitoring is more useful when readings can be saved, reviewed, and shared rather than only printed once.
A Pico 2 W prototype with a BME280 environmental sensor, local CSV logging, and optional HTTP upload over Wi-Fi.
Data logging, communication fallback, and honest explanation of what a local prototype can and cannot claim.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Breadboard |
![]() Jumper wires |
![]() BME280 |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| BME280 VCC | 3.3V output | Physical pin 36 | Sensor power |
| BME280 GND | GND | Physical pin 38 | Common ground |
| BME280 SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
Wiring Diagram

3.3V -> BME280 VCC
GND -> BME280 GND
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
Wi-Fi uses the Pico 2 W radio and needs no extra wiring
Step-by-Step Assembly
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Power the Pico from a stable USB source so the logger can run for repeated tests.
- Enter your Wi-Fi credentials and local server details in the code only after the local logging step works.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Read temperature and humidity in the Shell and confirm the values make sense before adding file logging.
- Test the output device: Write a small test file to the Pico so you know the local storage path works before enabling network upload.
- Test the decision logic: Check that one log interval creates one CSV line and that the comfort label matches the measured climate.
- Run the full system: Enable Wi-Fi and confirm that the local log and the server record match for the same reading.
- Validate the prototype: Run the logger with Wi-Fi disabled and confirm that local-only logging still works correctly.
- Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.
Full Project Code
After completing and checking the circuit connections, open Thonny IDE, copy and paste this code 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
import ujson as json
from machine import I2C, Pin
BME_SDA_PIN = 20
BME_SCL_PIN = 21
LOG_FILE = "climate_log.csv"
LOG_INTERVAL_S = 300
UPLOAD_ENABLED = False
WIFI_SSID = "YOUR_WIFI"
WIFI_PASSWORD = "YOUR_PASSWORD"
SERVER_HOST = "192.168.1.50"
SERVER_PORT = 8000
SERVER_PATH = "/climate"
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
sensor = bme280.BME280(i2c=i2c)
def comfort_label(temp_c, humidity):
if temp_c >= 30 or humidity >= 80:
return "HOT_OR_HUMID"
if temp_c <= 18:
return "COOL"
return "NORMAL"
def append_log(record):
try:
with open(LOG_FILE, "a") as handle:
handle.write("{},{},{},{}\n".format(record["uptime_s"], record["temperature_c"], record["humidity_pct"], record["label"]))
except OSError:
print("Could not write local log")
def connect_wifi(timeout_s=15):
wlan = network.WLAN(network.STA_IF)
if wlan.isconnected():
return wlan
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
for _ in range(timeout_s):
if wlan.isconnected():
return wlan
time.sleep(1)
return None
def upload_record(record):
if not UPLOAD_ENABLED:
return False
wlan = connect_wifi()
if wlan is None:
return False
address = socket.getaddrinfo(SERVER_HOST, SERVER_PORT)[0][-1]
body = json.dumps(record)
request = "POST {} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}".format(SERVER_PATH, SERVER_HOST, len(body), body)
sock = socket.socket()
try:
sock.connect(address)
sock.send(request.encode())
sock.recv(128)
return True
finally:
sock.close()
print("Climate logger ready")
while True:
try:
values = sensor.values
temperature = float(values[0].replace('C', ''))
pressure = float(values[1].replace('hPa', ''))
humidity = float(values[2].replace('%', ''))
record = {
"uptime_s": time.ticks_ms() // 1000,
"temperature_c": temperature,
"humidity_pct": humidity,
"label": comfort_label(temperature, humidity),
}
append_log(record)
uploaded = upload_record(record)
print(record, "uploaded" if uploaded else "local_only")
except Exception:
print("BME280 read error")
time.sleep(LOG_INTERVAL_S)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Sensor and label logic | Reads the BME280 and maps the values to a simple comfort label | Labels help turn raw climate numbers into something a user can interpret quickly | Adjust the label thresholds after studying your actual environment |
| append_log | Writes each record to a local CSV file on the Pico | Local storage provides a fallback when the network is not available | Change LOG_FILE if you want a different file name or storage strategy |
| Wi-Fi upload | Sends the same record to a local HTTP endpoint when upload is enabled | This shows how a prototype can grow from local logging to network reporting without overclaiming cloud features | Enable UPLOAD_ENABLED only after the local logger works first |
| Main loop | Creates one record per interval and reports whether it stayed local or uploaded | Students can validate both data and communication behavior from a single loop | Change LOG_INTERVAL_S after considering how much data you really need |
Expected Result
The serial monitor reports the current reading or state clearly.
The hardware output responds when the decision logic changes state.
Subsystem behavior matches the thresholds, timing, or rules described in the document.
Validation Checks
- Normal condition test: log one record in a stable room and confirm the values and label are believable
- Local logging validation: open the CSV file after several intervals and confirm each loop creates exactly one new line
- Upload validation: enable Wi-Fi and compare the local CSV line with the record received by the test server
- Fallback test: turn off the router and confirm the logger continues storing data locally
- Limitation test: discuss how sensor drift and long sampling intervals affect the usefulness of the record set
Deployment and Limitations
- This prototype is suitable for local pilots where sensor data must be logged or transmitted over short periods
- Before deployment, network reliability, power backup, and endpoint security need attention
- The system should keep useful local behavior even when communication fails
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No CSV file appears | Local write failed or the board reset before writing | Test a simple file write first and confirm the Pico file system is writable |
| BME280 keeps failing | Sensor wiring is unstable | Shorten the I2C wires and recheck power and ground |
| Upload always fails | UPLOAD_ENABLED is false or the endpoint is wrong | Confirm the server host, port, path, and Wi-Fi credentials |
| Data uploads but looks wrong | The sensor is drifting or the label thresholds are unrealistic | Compare local readings with a known thermometer and humidity source before trusting the data |



