From e4de8509a0549f84e7a7fe3f55e2eace935c2e6e Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 4 Dec 2019 20:53:18 +0100 Subject: [PATCH] Change stuff --- IotThermometer.sln | 42 ++++++++ IotThermometer/IotThermometer.csproj | 29 ++++++ IotThermometer/Librarys/Bme280.cs | 2 + IotThermometer/Librarys/Rainbowdruino.cs | 36 +++++-- IotThermometer/Librarys/TSL2591.cs | 4 + IotThermometer/Models/Measurings.cs | 122 +++++++++++++++++++++++ IotThermometer/Program.cs | 86 +++++++++++++--- 7 files changed, 298 insertions(+), 23 deletions(-) create mode 100644 IotThermometer/Models/Measurings.cs diff --git a/IotThermometer.sln b/IotThermometer.sln index 08a3821..4f4ed19 100644 --- a/IotThermometer.sln +++ b/IotThermometer.sln @@ -13,6 +13,20 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unosquare.Swan.Lite", "..\L EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot-Utils", "..\Utils\Bot-Utils\Bot-Utils\Bot-Utils.csproj", "{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iot-Interfaces", "..\Utils\Iot-Interfaces\Iot-Interfaces\Iot-Interfaces.csproj", "{4DAADA29-C600-4CF3-8AD3-9C97C8D7F632}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorDataMqtt", "..\Utils\ConnectorDataMqtt\ConnectorDataMqtt\ConnectorDataMqtt.csproj", "{EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Posix", "..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj", "{E2CA132E-E85C-40AD-BE94-B138AA68772B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt_4.7.1", "..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +53,34 @@ Global {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.ActiveCfg = Release|Any CPU {FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.Build.0 = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.Build.0 = Release|Any CPU + {4DAADA29-C600-4CF3-8AD3-9C97C8D7F632}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DAADA29-C600-4CF3-8AD3-9C97C8D7F632}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DAADA29-C600-4CF3-8AD3-9C97C8D7F632}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DAADA29-C600-4CF3-8AD3-9C97C8D7F632}.Release|Any CPU.Build.0 = Release|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.Build.0 = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.Build.0 = Release|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.Build.0 = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IotThermometer/IotThermometer.csproj b/IotThermometer/IotThermometer.csproj index 27df099..485d96b 100644 --- a/IotThermometer/IotThermometer.csproj +++ b/IotThermometer/IotThermometer.csproj @@ -44,6 +44,7 @@ + @@ -51,6 +52,18 @@ + + {91A14CD2-2940-4500-8193-56D37EDDDBAA} + litjson_4.7.1 + + + {e2ca132e-e85c-40ad-be94-b138aa68772b} + Mono.Posix + + + {a11aef5a-b246-4fe8-8330-06db73cc8074} + M2Mqtt_4.7.1 + {8C5D4DE9-377F-4EC8-873D-6EEF15F43516} Unosquare.RaspberryIO @@ -63,6 +76,22 @@ {2ea5e3e4-f8c8-4742-8c78-4b070afcfb73} Unosquare.Swan + + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9} + Bot-Utils + + + {ee6c8f68-ed46-4c1c-abdd-cfcdf75104f2} + ConnectorDataMqtt + + + {4DAADA29-C600-4CF3-8AD3-9C97C8D7F632} + Iot-Interfaces + + + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC} + Utils-IoT + {FAC8CE64-BF13-4ECE-8097-AEB5DD060098} Utils diff --git a/IotThermometer/Librarys/Bme280.cs b/IotThermometer/Librarys/Bme280.cs index 53547b5..bea23be 100644 --- a/IotThermometer/Librarys/Bme280.cs +++ b/IotThermometer/Librarys/Bme280.cs @@ -42,6 +42,8 @@ namespace BlubbFish.Iot.Thermometer.Librarys { this.CalcHum(humidity); } } + + public void Dispose() => this.init = false; #endregion #region Calculation diff --git a/IotThermometer/Librarys/Rainbowdruino.cs b/IotThermometer/Librarys/Rainbowdruino.cs index 44799f0..d681168 100644 --- a/IotThermometer/Librarys/Rainbowdruino.cs +++ b/IotThermometer/Librarys/Rainbowdruino.cs @@ -6,6 +6,7 @@ namespace BlubbFish.Iot.Thermometer.Librarys { class Rainbowdruino { private readonly SerialPort serial; private readonly UInt32[,] zbuffer = new UInt32[8, 16]; + private Boolean init = false; private struct Digets { public static readonly Boolean[][] Null = { @@ -80,9 +81,20 @@ namespace BlubbFish.Iot.Thermometer.Librarys { }; } - public Rainbowdruino(String com) { - this.serial = new SerialPort(com, 19200); - this.serial.Open(); + + + public Rainbowdruino(String com) => this.serial = new SerialPort(com, 19200); + + public void Begin() { + if(!this.init) { + this.serial.Open(); + this.init = true; + } + } + + public void Dispose() { + this.serial.Close(); + this.init = false; } public void DrawDigets(String number, UInt32 color) { @@ -125,15 +137,21 @@ namespace BlubbFish.Iot.Thermometer.Librarys { } public void Write() { - this.Clear(); - List colors = this.GetAllColors(); - foreach(UInt32 item in colors) { - this.serial.Write(String.Join(" ", this.GetZbufferPage(0, item)) + " " + String.Join(" ", this.GetZbufferPage(1, item)) + " " + String.Join(" ", this.GetAllColors(item)) + "\n"); + if(this.init) { + this.Clear(); + List colors = this.GetAllColors(); + foreach(UInt32 item in colors) { + this.serial.Write(String.Join(" ", this.GetZbufferPage(0, item)) + " " + String.Join(" ", this.GetZbufferPage(1, item)) + " " + String.Join(" ", this.GetAllColors(item)) + "\n"); + } + this.ClearZbuffer(); } - this.ClearZbuffer(); } - public void Clear() => this.serial.Write("clear\n"); + public void Clear() { + if(this.init) { + this.serial.Write("clear\n"); + } + } private void ClearZbuffer() { for(Int32 i = 0; i < 8; i++) { diff --git a/IotThermometer/Librarys/TSL2591.cs b/IotThermometer/Librarys/TSL2591.cs index b52ecbf..3bc76ac 100644 --- a/IotThermometer/Librarys/TSL2591.cs +++ b/IotThermometer/Librarys/TSL2591.cs @@ -31,6 +31,10 @@ namespace BlubbFish.Iot.Thermometer.Librarys { this.CalcLux(this.GetLumAdv(this.IntegrationAmplifier)); } } + public void Dispose() { + this.Disable(); + this.init = false; + } #endregion #region Calculation diff --git a/IotThermometer/Models/Measurings.cs b/IotThermometer/Models/Measurings.cs new file mode 100644 index 0000000..7c82b8c --- /dev/null +++ b/IotThermometer/Models/Measurings.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlubbFish.Utils.IoT.Interfaces; +using LitJson; + +namespace BlubbFish.Iot.Thermometer.Models { + class Measurings : ISenml { + private Int32 updateCount = 0; + + public delegate void UpdateDataEvent(Object sender, EventArgs e); + public event UpdateDataEvent DataUpdate; + + public Double Temperatur { get; private set; } + public Double Humidity { get; private set; } + public Double Pressure { get; private set; } + public Double Lumiosity { get; private set; } + + public String SenmlTopic() => "LS/v2/ZGW/linksmart-iot/senml"; + public String ToSenml() { + Object[] json = new Object[] {new Dictionary { + { "e", new Dictionary() { + { "n", "temperatur" }, + { "u", "Cel" }, + { "t", 0 }, + { "v", this.Temperatur } } }, + { "bn", "iot" }, + { "bt", ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds() } + },new Dictionary { + { "e", new Dictionary() { + { "n", "humidity" }, + { "u", "%RH" }, + { "t", 0 }, + { "v", this.Humidity } } }, + { "bn", "iot" }, + { "bt", ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds() } + },new Dictionary { + { "e", new Dictionary() { + { "n", "pressure" }, + { "u", "Pa" }, + { "t", 0 }, + { "v", this.Pressure*100 } } }, + { "bn", "iot" }, + { "bt", ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds() } + },new Dictionary { + { "e", new Dictionary() { + { "n", "luminace" }, + { "u", "lx" }, + { "t", 0 }, + { "v", this.Temperatur } } }, + { "bn", "iot" }, + { "bt", ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds() } + } }; + return JsonMapper.ToJson(json); + } + + public void Dispose() { + + } + + public void Update(Double temperatur, Double humidity, Double pressure, Double luminosity) { + this.updateCount++; + this.CheckTemp(temperatur); + this.CheckHum(humidity); + this.CheckPress(pressure); + this.CheckLumi(luminosity); + this.Temperatur = temperatur; + this.Humidity = humidity; + this.Pressure = pressure; + this.Lumiosity = luminosity; + if(this.updateCount > 60) { + this.updateCount = 0; + this.DataUpdate?.Invoke(this, new EventArgs()); + } + } + + private void CheckLumi(Double luminosity) { + Double diff = 0.5; + if(luminosity < 100 && this.Lumiosity < 100) { + diff = 1; + } + if(luminosity < 20 && this.Lumiosity < 20) { + diff = 2; + } + if(luminosity < 5 && this.Lumiosity < 5) { + diff = 4; + } + if(luminosity < 1 && this.Lumiosity < 1) { + diff = 10; + } + if(luminosity < 0.1 && this.Lumiosity < 0.1) { + return; + } + if(luminosity < this.Lumiosity && (100 - ((luminosity / this.Lumiosity) * 100)) > diff) { + this.updateCount = Int32.MaxValue; + } + if(luminosity > this.Lumiosity && (100 - ((this.Lumiosity / luminosity) * 100)) > diff) { + this.updateCount = Int32.MaxValue; + } + } + + private void CheckPress(Double pressure) { + if(Math.Abs(this.Pressure - pressure) > 0.1) { + this.updateCount = Int32.MaxValue; + } + } + + private void CheckHum(Double humidity) { + if(Math.Abs(this.Humidity - humidity) > 1) { + this.updateCount = Int32.MaxValue; + } + } + + private void CheckTemp(Double temperatur) { + if(Math.Abs(this.Temperatur - temperatur) > 0.02) { + this.updateCount = Int32.MaxValue; + } + } + } +} diff --git a/IotThermometer/Program.cs b/IotThermometer/Program.cs index 2a723d1..48f0a12 100644 --- a/IotThermometer/Program.cs +++ b/IotThermometer/Program.cs @@ -1,38 +1,96 @@ using System; using BlubbFish.Iot.Thermometer.Librarys; using System.Threading; +using BlubbFish.Utils.IoT.Bots; +using BlubbFish.Iot.Thermometer.Models; +using BlubbFish.Utils.IoT.Interfaces; +using BlubbFish.Utils.IoT.Connector; +using System.Collections.Generic; namespace BlubbFish.Iot.Thermometer { - class Program { - private readonly TSL2591 tls; - private readonly Bme280 bme; - private readonly Rainbowdruino disp; + class Program : ABot { + private readonly TSL2591 tls = new TSL2591(0x29); + private readonly Bme280 bme = new Bme280(0x76); + private readonly Rainbowdruino disp = new Rainbowdruino("/dev/ttyUSB0"); + private readonly Measurings measurings = new Measurings(); + private Thread measureThread; + private Thread displayThread; + private ADataBackend mqtt; public Program(String[] args) { - this.tls = new TSL2591(0x29); - this.bme = new Bme280(0x76); - this.disp = new Rainbowdruino("/dev/ttyUSB0"); - this.tls.Begin(); - this.bme.Begin(); + this.Startup(); + this.StartThreads(); + this.WaitForShutdown(); + this.Shutdown(); + } + + private void MeasureRunner() { + Console.WriteLine("Start Measure Runner..."); while(true) { this.tls.Measure(); this.bme.Measure(); + this.measurings.Update(this.bme.Temperatur, this.bme.Humidity, this.bme.Pressure, this.tls.Luminosity); + Thread.Sleep(780); + } + } + + private void DispayRunner() { + Console.WriteLine("Start Display Runner..."); + while(true) { this.DrawDisp(this.bme.Temperatur, 0x110000); - Thread.Sleep(5000); + Thread.Sleep(10000); + this.DrawDisp(this.bme.Temperatur, 0x110000); + Thread.Sleep(10000); + this.DrawDisp(this.bme.Temperatur, 0x110000); + Thread.Sleep(10000); + this.DrawDisp(this.bme.Temperatur, 0x110000); + Thread.Sleep(10000); this.DrawDisp(this.bme.Humidity, 0x001100); - Thread.Sleep(2000); + Thread.Sleep(10000); this.DrawDisp(this.bme.Pressure, 0x111100); - Thread.Sleep(2000); + Thread.Sleep(5000); this.DrawDisp(this.tls.Luminosity, 0x000011); - Thread.Sleep(2000); + Thread.Sleep(5000); Console.WriteLine(this.tls.Luminosity.ToString("F5") + " lux"); Console.WriteLine(this.bme.Temperatur.ToString("F2") + " °C"); Console.WriteLine(this.bme.Pressure.ToString("F2") + " mbHp"); Console.WriteLine(this.bme.Humidity.ToString("F3") + " Hm%"); - Console.WriteLine(); } } + private void Shutdown() { + this.measureThread.Abort(); + this.displayThread.Abort(); + this.tls.Dispose(); + this.bme.Dispose(); + this.disp.Dispose(); + this.measurings.Dispose(); + base.Dispose(); + } + + private void Startup() { + this.measurings.DataUpdate += this.SendSenml; + this.mqtt = (ADataBackend)ABackend.GetInstance(new Dictionary() { { "type", "mqtt" }, { "server", "iot.blubbfish.net" } }, ABackend.BackendType.Data); + this.tls.Begin(); + this.bme.Begin(); + this.disp.Begin(); + } + + private void SendSenml(Object sender, EventArgs e) { + ISenml s = sender as ISenml; + String topic = s.SenmlTopic(); + String data = s.ToSenml(); + Console.WriteLine("MQTT: " + topic + " " + data); + this.mqtt.Send(topic, data); + } + + private void StartThreads() { + this.measureThread = new Thread(this.MeasureRunner); + this.measureThread.Start(); + this.displayThread = new Thread(this.DispayRunner); + this.displayThread.Start(); + } + private void DrawDisp(Double c, UInt32 color) { this.disp.DrawDigets(c.ToString().Replace(",", "."), color); this.disp.DrawLineX(0, 0, 16, color);