First functioning draft
This commit is contained in:
54
InfluxDataService.cs
Normal file
54
InfluxDataService.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using InfluxDB.Client;
|
||||||
|
|
||||||
|
public class InfluxDataService
|
||||||
|
{
|
||||||
|
private readonly string _url;
|
||||||
|
private readonly string _token;
|
||||||
|
private readonly string _org;
|
||||||
|
private readonly string _bucket;
|
||||||
|
|
||||||
|
public InfluxDataService(string url, string token, string org, string bucket)
|
||||||
|
{
|
||||||
|
_url = url;
|
||||||
|
_token = token;
|
||||||
|
_org = org;
|
||||||
|
_bucket = bucket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<float> GetLatestTemperatureAsync()
|
||||||
|
{
|
||||||
|
// Connect to the InfluxDB container
|
||||||
|
using var client = new InfluxDBClient(_url, _token);
|
||||||
|
var queryApi = client.GetQueryApi();
|
||||||
|
|
||||||
|
// This is a standard Flux query to get the last recorded value in the last 15 mins.
|
||||||
|
// You will need to change "_measurement" and "_field" to match your setup.
|
||||||
|
string flux = $@"
|
||||||
|
from(bucket: ""{_bucket}"")
|
||||||
|
|> range(start: -15m)
|
||||||
|
|> filter(fn: (r) => r[""_measurement""] == ""LoRa_Test_DB_2"")
|
||||||
|
|> filter(fn: (r) => r[""_field""] == ""temperature"")
|
||||||
|
|> last()";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tables = await queryApi.QueryAsync(flux, _org);
|
||||||
|
|
||||||
|
// Check if we actually got data back
|
||||||
|
if (tables != null && tables.Any() && tables[0].Records.Any())
|
||||||
|
{
|
||||||
|
var record = tables[0].Records[0];
|
||||||
|
return Convert.ToSingle(record.GetValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error querying InfluxDB: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0f; // Fallback value if query fails or no data is found
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,7 +12,9 @@ public class MatrixRenderer
|
|||||||
_wled = new WledDdpClient(wledIp);
|
_wled = new WledDdpClient(wledIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
public async Task DrawAndSendFrameAsync(float currentTemp)
|
public async Task DrawAndSendFrameAsync(float currentTemp)
|
||||||
|
//public async Task DrawAndSendFrameAsync(string currentTemp)
|
||||||
{
|
{
|
||||||
// 1. Create a 64x64 canvas
|
// 1. Create a 64x64 canvas
|
||||||
using SKBitmap bitmap = new SKBitmap(_width, _height, SKColorType.Rgba8888, SKAlphaType.Premul);
|
using SKBitmap bitmap = new SKBitmap(_width, _height, SKColorType.Rgba8888, SKAlphaType.Premul);
|
||||||
|
|||||||
34
Program.cs
34
Program.cs
@@ -1,13 +1,35 @@
|
|||||||
var renderer = new MatrixRenderer("192.168.0.150"); // Replace with WLED IP
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
var renderer = new MatrixRenderer("192.168.0.150"); // Your WLED IP
|
||||||
|
|
||||||
|
// Replace these with your actual InfluxDB v2 credentials
|
||||||
|
// If Influx is on the same Pi, you can use the Pi's local IP and port 8086
|
||||||
|
var influx = new InfluxDataService(
|
||||||
|
url: "http://192.168.0.76:8086",
|
||||||
|
token: ":",
|
||||||
|
org: "-",
|
||||||
|
bucket: "sensor_data"
|
||||||
|
);
|
||||||
|
|
||||||
|
Console.WriteLine("Starting DDP stream with live InfluxDB data...");
|
||||||
|
|
||||||
|
float currentTemp = 1f;
|
||||||
|
Stopwatch dbTimer = Stopwatch.StartNew();
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// TODO: Query InfluxDB for your BME680 data here
|
// Only query the database every 5 seconds (5000 ms)
|
||||||
float bme680Temp = 22.5f;
|
if (dbTimer.ElapsedMilliseconds > 5000 || currentTemp == 0f)
|
||||||
|
{
|
||||||
|
currentTemp = await influx.GetLatestTemperatureAsync();
|
||||||
|
Console.WriteLine($"Fetched new temp: {currentTemp}c");
|
||||||
|
dbTimer.Restart();
|
||||||
|
}
|
||||||
|
|
||||||
await renderer.DrawAndSendFrameAsync(bme680Temp);
|
// Keep pushing frames to WLED at 30 FPS
|
||||||
|
await renderer.DrawAndSendFrameAsync(currentTemp);
|
||||||
|
|
||||||
// Update screen at 30 FPS for smooth transitions,
|
|
||||||
// or every 5 seconds if just showing static data.
|
|
||||||
await Task.Delay(1000 / 30);
|
await Task.Delay(1000 / 30);
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="InfluxDB.Client" Version="5.1.0" />
|
||||||
<PackageReference Include="SkiaSharp" Version="4.151.1" />
|
<PackageReference Include="SkiaSharp" Version="4.151.1" />
|
||||||
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />
|
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user