diff --git a/InfluxDataService.cs b/InfluxDataService.cs new file mode 100644 index 0000000..cee9e33 --- /dev/null +++ b/InfluxDataService.cs @@ -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 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 + } +} \ No newline at end of file diff --git a/MatrixRenderer.cs b/MatrixRenderer.cs index 05a6652..d2e949c 100644 --- a/MatrixRenderer.cs +++ b/MatrixRenderer.cs @@ -12,7 +12,9 @@ public class MatrixRenderer _wled = new WledDdpClient(wledIp); } + // public async Task DrawAndSendFrameAsync(float currentTemp) + //public async Task DrawAndSendFrameAsync(string currentTemp) { // 1. Create a 64x64 canvas using SKBitmap bitmap = new SKBitmap(_width, _height, SKColorType.Rgba8888, SKAlphaType.Premul); diff --git a/Program.cs b/Program.cs index 5b700b6..0605054 100644 --- a/Program.cs +++ b/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) { - // TODO: Query InfluxDB for your BME680 data here - float bme680Temp = 22.5f; + // Only query the database every 5 seconds (5000 ms) + 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); } \ No newline at end of file diff --git a/WLED_DDP_Client.csproj b/WLED_DDP_Client.csproj index 8c278a7..fe0bf5c 100644 --- a/WLED_DDP_Client.csproj +++ b/WLED_DDP_Client.csproj @@ -8,6 +8,7 @@ +