diff --git a/EPTracer.sln b/EPTracer.sln new file mode 100644 index 0000000..29ae540 --- /dev/null +++ b/EPTracer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30907.101 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPTracer", "EPTracer\EPTracer.csproj", "{51295B4D-8DE3-4BF7-B6AD-72629DAD922C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {51295B4D-8DE3-4BF7-B6AD-72629DAD922C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51295B4D-8DE3-4BF7-B6AD-72629DAD922C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51295B4D-8DE3-4BF7-B6AD-72629DAD922C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51295B4D-8DE3-4BF7-B6AD-72629DAD922C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C50D5042-CBEF-475D-81BC-ADF0B6909767} + EndGlobalSection +EndGlobal diff --git a/EPTracer/EPTracer - Backup.csproj b/EPTracer/EPTracer - Backup.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/EPTracer/EPTracer - Backup.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/EPTracer/EPTracer.csproj b/EPTracer/EPTracer.csproj new file mode 100644 index 0000000..df46591 --- /dev/null +++ b/EPTracer/EPTracer.csproj @@ -0,0 +1,21 @@ + + + + Exe + net5.0 + + + + + + + + + + + + PreserveNewest + + + + diff --git a/EPTracer/Program.cs b/EPTracer/Program.cs new file mode 100644 index 0000000..f73322e --- /dev/null +++ b/EPTracer/Program.cs @@ -0,0 +1,114 @@ +using InfluxDB.Client; +using InfluxDB.Client.Api.Domain; +using InfluxDB.Client.Writes; +using Microsoft.Extensions.Configuration; +using NModbus; +using NModbus.Serial; +using System; +using System.IO.Ports; + +namespace EPTracer +{ + + class Program + { + static void Main(string[] args) + { + Console.WriteLine(); + Console.WriteLine("***************************************************************"); + Console.WriteLine("Loading config..."); + + IConfiguration config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", true, true) + .Build(); + string portConf = config["port"]; + int interval = int.Parse(config["interval"]); + string influxdb = config["influxdb"]; + string token = config["token"]; + string bucket = config["bucket"]; + string org = config["org"]; + + Console.WriteLine(); + Console.WriteLine($"Serial Port: {portConf}"); + Console.WriteLine($"Interval: {interval}"); + Console.WriteLine($"InfluxDB: {influxdb}"); + Console.WriteLine($"Token: {token.Substring(0, 15)}..."); + Console.WriteLine($"Bucket: {bucket}"); + Console.WriteLine($"Org: {org}"); + Console.WriteLine("***************************************************************"); + Console.WriteLine(); + + + using (SerialPort port = new SerialPort(portConf)) + { + Console.WriteLine($"Configuring serial port: {portConf}"); + // configure serial port + port.BaudRate = 115200; + port.DataBits = 8; + port.Parity = Parity.None; + port.StopBits = StopBits.One; + + Console.WriteLine("Opening port"); + port.Open(); + + var factory = new ModbusFactory(); + IModbusSerialMaster master = factory.CreateRtuMaster(port); + + byte slaveId = 1; + ushort startAddress = 0x3100; + ushort numRegisters = 18; + + Console.WriteLine("Reading registers"); + Console.WriteLine("....."); + while (true) + { + //read registers + ushort[] registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters); + + var pvv = registers[0] / 100.00; + var pva = registers[1] / 100.00; + var pvw = registers[2] / 100.00; + var btv = registers[4] / 100.00; + var bta = registers[5] / 100.00; + var btw = registers[6] / 100.00; + var ldv = registers[8] / 100.00; + var lda = registers[9] / 100.00; + var ldw = registers[10] / 100.00; + var btt = registers[12] / 100.00; + var btc = registers[15] / 100.00; + var rbt = registers[16] / 100.00; + + using (var client = InfluxDBClientFactory.Create(influxdb, token)) + { + client.SetLogLevel(InfluxDB.Client.Core.LogLevel.Body); + + var timestamp = DateTime.UtcNow; + var point = PointData.Measurement("solar") + .Tag("unit", "1") + .Field("pvv", pvv) + .Field("pva", pva) + .Field("pvw", pvw) + .Field("btv", btv) + .Field("bta", bta) + .Field("btw", btw) + .Field("ldv", ldv) + .Field("lda", lda) + .Field("ldw", ldw) + .Field("btt", btt) + .Field("btc", btc) + .Field("rbt", rbt) + .Timestamp(DateTime.UtcNow, WritePrecision.Ns); + + using (var writeApi = client.GetWriteApi()) + { + writeApi.WritePoint(point, bucket, org); + Console.WriteLine(point.ToLineProtocol()); + } + } + + System.Threading.Thread.Sleep(interval * 1000); + } + } + } + } +} diff --git a/EPTracer/appsettings.json b/EPTracer/appsettings.json new file mode 100644 index 0000000..c5cb5af --- /dev/null +++ b/EPTracer/appsettings.json @@ -0,0 +1,8 @@ +{ + "port": "/dev/ttyXRUSB0", + "interval": 5, + "influxdb": "http://127.0.0.1:8086", + "token": "l8GGGVe6YaNTfvsE5h4WLMLOEVkOL49NyOvCD3eV1LqI4va2Uh9Acza0-_UIhbuT5FdJe5FniRlMoWtHM3kV-A==", + "bucket": "solar", + "org": "flexa" +} \ No newline at end of file