Raspberry Pi Remote Data Logging
Combine a sensor reading loop with writing to a local file, database, or remote API so your Raspberry Pi can log data unattended over time.
What Is Data Logging?
Data logging means automatically taking a measurement — temperature, humidity, light level, anything your sensor reports — again and again over time, and saving each reading somewhere durable along with a timestamp. Instead of watching a live reading scroll past in a terminal, you end up with a record you can graph, search, or hand off to another program later, even if nobody was at the keyboard when it was collected.
- A plain CSV (comma-separated values) text file — the simplest option, easy to open in a spreadsheet
- A local SQLite database — a single-file database that supports proper queries without any separate server
- A remote API or cloud IoT platform — send each reading over HTTP so it's stored somewhere off the Pi
- An MQTT broker — a lightweight publish/subscribe protocol popular for near-real-time sensor streams
Logging Sensor Readings to a Local File
Start simple: read the sensor, append a timestamped row to a file, then sleep for a fixed interval before repeating. Python's built-in csv module keeps the formatting consistent, and opening the file in append mode ("a") means every run of the script adds to the same log instead of overwriting it. The example below uses a placeholder read_sensor() function — swap in your real reading using smbus2 or spidev, as covered on the I2C and SPI pages.
Example
import csv
import time
from datetime import datetime
LOG_FILE = "temperature_log.csv"
INTERVAL_SECONDS = 60
def read_sensor():
# Replace this with a real reading, e.g. from smbus2 or spidev
return 21.4
with open(LOG_FILE, "a", newline="") as f:
writer = csv.writer(f)
while True:
timestamp = datetime.now().isoformat(timespec="seconds")
temperature = read_sensor()
writer.writerow([timestamp, temperature])
f.flush()
print(f"Logged {temperature}C at {timestamp}")
time.sleep(INTERVAL_SECONDS)Sending Readings to a Remote API
To make readings available somewhere other than the Pi's own storage, send each one to a remote API with the requests library (pip install requests). A POST request with a small JSON payload is enough for most simple logging endpoints or IoT platforms. Because networks are unreliable, wrap the request in a try/except so a dropped Wi-Fi connection doesn't stop the whole logger — and consider still writing to a local file as a backup, so no readings are lost if the upload fails.
Example
import csv
import time
import requests
from datetime import datetime
API_URL = "https://example.com/api/readings"
BACKUP_FILE = "backup_log.csv"
INTERVAL_SECONDS = 60
def read_sensor():
return 21.4 # Replace with a real sensor reading
while True:
timestamp = datetime.now().isoformat(timespec="seconds")
temperature = read_sensor()
payload = {"timestamp": timestamp, "temperature": temperature}
try:
response = requests.post(API_URL, json=payload, timeout=5)
response.raise_for_status()
print(f"Uploaded {temperature}C at {timestamp}")
except requests.RequestException as error:
print(f"Upload failed ({error}), saving locally instead")
with open(BACKUP_FILE, "a", newline="") as f:
csv.writer(f).writerow([timestamp, temperature])
time.sleep(INTERVAL_SECONDS)Keeping the Logger Running Reliably
A logging script is only useful if it keeps running. For short-term projects, a screen or tmux session lets it survive after you close your SSH connection. For a permanent setup, create a systemd service so the Pi restarts the logger automatically on boot or after a crash, or trigger a script periodically with cron if you'd rather run-and-exit each time instead of looping forever. Whichever approach you pick, choose a logging interval that matches what you actually need — every second generates a lot of storage and bandwidth for a slow-changing value like room temperature, while every hour might hide a spike you cared about.
Exercise: Raspberry Pi Networking
Which service allows you to remotely access a Raspberry Pi's command line over the network?