From Raspberry Pi Sensor Data to AI Analysis with Python

In our last example, Python and Pandas turned messy temperature readings into clean CSV and JSON files. This time we'll take the idea one step further.

 

From Raspberry Pi Sensor Data to AI Analysis with Python

In our last example, Python and Pandas turned messy temperature readings into clean CSV and JSON files.

This time we'll take the idea one step further.

We'll start with another dirty temperature dataset, clean it with Pandas, save the results for both people and software, and then send the cleaned observations to an AI model for an evaluation of the temperature pattern.

The pipeline becomes:

dirty CSV → Pandas → clean data → CSV + JSON → AI API → analysis

The AI isn't being asked to fix our broken sensor data.

That's deliberate.

Python handles the predictable data-cleaning work first. Only then do we give the clean observations to a model and ask it to interpret the pattern.

Another Messy Dataset

Create room_temperature.csv:

timestamp,temp
2026-11-02 12:00:00,71.8
2026-11-02 12:10:00,72.1
2026-11-02 12:20:00,72.9
2026-11-02 12:30:00,
2026-11-02 12:40:00,74.2
2026-11-02 12:50:00,74.9
wrong_time,75.3
2026-11-02 13:10:00,75.8

Again, the problem is obvious once we look at it. There's a missing temperature and an invalid timestamp.

Our program shouldn't simply hope that everything is okay.

It should clean the measurements first.

Cleaning the Sensor Data

We'll use Pandas again:

import pandas as pd

df = pd.read_csv("room_temperature.csv")

df["timestamp"] = pd.to_datetime(
    df["timestamp"],
    errors="coerce"
)

df["temp"] = pd.to_numeric(
    df["temp"],
    errors="coerce"
)

df = df.dropna(subset=["timestamp", "temp"])
df = df.drop_duplicates()
df = df[df["temp"].between(40, 120)]
df = df.sort_values("timestamp")

df.to_csv(
    "room_temperature_clean.csv",
    index=False
)

df.to_json(
    "room_temperature_clean.json",
    orient="records",
    date_format="iso",
    indent=2
)

print(df)

Once again we have the same basic pattern:

input → processing → output

Pandas reads the file, validates its contents according to our rules, removes unusable records and writes clean versions.

But now we have another potential output.

An AI model.

Giving the Data to an AI Model

For this experiment we'll use OpenRouter's Free Models Router. Its documentation says openrouter/free routes requests among currently available free models, making it suitable for experimentation and low-volume learning projects.

That distinction matters: openrouter/free isn't one fixed model. The router chooses an available free model for a request.

You'll need an API key. Rather than putting that key directly inside the Python program, set it as an environment variable:

export OPENROUTER_API_KEY="your-key-here"

We'll also need the Requests library:

python3 -m pip install requests

Now create analyze_temperature.py:

import json
import os
import requests

API_KEY = os.environ["OPENROUTER_API_KEY"]

with open("room_temperature_clean.json", "r") as file:
    sensor_data = json.load(file)

prompt = f"""
You are evaluating temperature sensor readings.

Describe the overall temperature pattern in plain English.
Identify whether temperatures are generally rising, falling,
or remaining stable.

Do not invent measurements or causes that are not supported
by the supplied data.

Sensor readings:
{json.dumps(sensor_data, indent=2)}
"""

response = requests.post(
    "https://openrouter.ai/api/v1/responses",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    },
    json={
        "model": "openrouter/free",
        "input": prompt,
    },
    timeout=60,
)

response.raise_for_status()

result = response.json()

message = next(
    item
    for item in reversed(result["output"])
    if item["type"] == "message"
)

analysis = next(
    part["text"]
    for part in message["content"]
    if part["type"] == "output_text"
)

print(analysis)
print("\nModel used:", result["model"])

Run it:

python3 analyze_temperature.py

Python Cleans. AI Interprets.

There's an important architectural distinction hiding inside this little Raspberry Pi project.

We didn't throw dirty data at an AI model and ask it to figure everything out.

We gave deterministic work to normal software.

Pandas parses the timestamps. Pandas converts the numbers. Pandas removes invalid records. Pandas writes the CSV and JSON files.

Only after that pipeline has produced trustworthy input do we invoke a model for the fuzzy part of the problem:

What pattern do these measurements appear to show?

Our cleaned example rises from 71.8 degrees to 75.8 degrees across the retained observations. A model can turn that series of measurements into a short natural-language assessment, while our prompt explicitly tells it not to invent causes that the data cannot establish.

That division of labor is worth noticing.

Sensors measure. Pandas cleans. Code validates. AI interprets.

Suddenly our little Raspberry Pi isn't merely collecting numbers.

It's running a complete pipeline that moves information from a physical sensor, through traditional data processing, and finally into an AI system capable of explaining what the measurements appear to mean.

That is a remarkably powerful pipeline to have sitting on a desk.



Copyright © 2026 Daily Tech-Reader.blog
All Rights Reserved

Popular posts from this blog

Insight: The Great Minimal OS Showdown—DietPi vs Raspberry Pi OS Lite

Troubleshooting Raspberry Pi Connect Issues: A Comprehensive Guide