Implement Bodenfeuchte app: MQTT listener, SQLite storage, chart UI, Docker
- Custom Next.js server starts MQTT listener on boot - Subscribes to zigbee2mqtt/Bodenfeuchte_1, stores soil_moisture in SQLite - API route /api/data returns last 6 hours of measurements - Frontend shows current value + Recharts line chart, auto-refresh every 60s - Dockerfile + docker-compose with persistent volume for SQLite DB Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { getLast6Hours } from '@/lib/db';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET() {
|
||||
const rows = getLast6Hours();
|
||||
return Response.json(rows);
|
||||
}
|
||||
+99
-58
@@ -1,65 +1,106 @@
|
||||
import Image from "next/image";
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts';
|
||||
|
||||
type Measurement = {
|
||||
timestamp: number;
|
||||
soil_moisture: number;
|
||||
};
|
||||
|
||||
function formatTime(ts: number): string {
|
||||
return new Date(ts).toLocaleTimeString('de-DE', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
const [data, setData] = useState<Measurement[]>([]);
|
||||
const [lastUpdate, setLastUpdate] = useState<string>('');
|
||||
|
||||
const fetchData = async () => {
|
||||
const res = await fetch('/api/data');
|
||||
const json: Measurement[] = await res.json();
|
||||
setData(json);
|
||||
setLastUpdate(new Date().toLocaleTimeString('de-DE'));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
const interval = setInterval(fetchData, 60_000);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
const latest = data.at(-1);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col flex-1 items-center justify-center bg-zinc-50 font-sans dark:bg-black">
|
||||
<main className="flex flex-1 w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/next.svg"
|
||||
alt="Next.js logo"
|
||||
width={100}
|
||||
height={20}
|
||||
priority
|
||||
/>
|
||||
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
|
||||
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
|
||||
To get started, edit the page.tsx file.
|
||||
</h1>
|
||||
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
|
||||
Looking for a starting point or more instructions? Head over to{" "}
|
||||
<a
|
||||
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Templates
|
||||
</a>{" "}
|
||||
or the{" "}
|
||||
<a
|
||||
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Learning
|
||||
</a>{" "}
|
||||
center.
|
||||
<main className="min-h-screen bg-gray-950 text-gray-100 p-8">
|
||||
<h1 className="text-2xl font-semibold mb-1">Bodenfeuchte</h1>
|
||||
<p className="text-sm text-gray-400 mb-6">
|
||||
Letzte 6 Stunden · Aktualisierung: {lastUpdate || '…'}
|
||||
</p>
|
||||
|
||||
{latest && (
|
||||
<div className="mb-8 inline-block bg-gray-800 rounded-xl px-6 py-4">
|
||||
<span className="text-5xl font-bold text-green-400">
|
||||
{latest.soil_moisture.toFixed(1)}
|
||||
</span>
|
||||
<span className="text-2xl text-gray-400 ml-2">%</span>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
{formatTime(latest.timestamp)} Uhr
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
|
||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/vercel.svg"
|
||||
alt="Vercel logomark"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Deploy Now
|
||||
</a>
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
|
||||
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Documentation
|
||||
</a>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="bg-gray-900 rounded-2xl p-4" style={{ height: 360 }}>
|
||||
{data.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-full text-gray-500">
|
||||
Noch keine Daten vorhanden
|
||||
</div>
|
||||
) : (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<LineChart data={data} margin={{ top: 8, right: 16, bottom: 8, left: 0 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="#374151" />
|
||||
<XAxis
|
||||
dataKey="timestamp"
|
||||
tickFormatter={formatTime}
|
||||
stroke="#6b7280"
|
||||
tick={{ fontSize: 12 }}
|
||||
minTickGap={40}
|
||||
/>
|
||||
<YAxis
|
||||
domain={[0, 100]}
|
||||
unit="%"
|
||||
stroke="#6b7280"
|
||||
tick={{ fontSize: 12 }}
|
||||
width={45}
|
||||
/>
|
||||
<Tooltip
|
||||
formatter={(v) => [`${Number(v).toFixed(1)} %`, 'Bodenfeuchte']}
|
||||
labelFormatter={(ts) => formatTime(Number(ts))}
|
||||
contentStyle={{ background: '#1f2937', border: 'none', borderRadius: 8 }}
|
||||
/>
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="soil_moisture"
|
||||
stroke="#4ade80"
|
||||
strokeWidth={2}
|
||||
dot={false}
|
||||
activeDot={{ r: 4 }}
|
||||
/>
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user