mqtt
This commit is contained in:
@@ -2,17 +2,18 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="InfluxDB.Client" Version="4.0.0" />
|
<PackageReference Include="InfluxDB.Client" Version="4.14.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
<PackageReference Include="NModbus" Version="3.0.72" />
|
<PackageReference Include="MQTTnet" Version="4.3.3.952" />
|
||||||
<PackageReference Include="NModbus.Serial" Version="3.0.72" />
|
<PackageReference Include="NModbus" Version="3.0.81" />
|
||||||
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
|
<PackageReference Include="NModbus.Serial" Version="3.0.81" />
|
||||||
|
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||||
<None Update="appsettings.json">
|
<None Update="appsettings.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|||||||
@@ -2,10 +2,14 @@
|
|||||||
using InfluxDB.Client.Api.Domain;
|
using InfluxDB.Client.Api.Domain;
|
||||||
using InfluxDB.Client.Writes;
|
using InfluxDB.Client.Writes;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MQTTnet;
|
||||||
|
using MQTTnet.Client;
|
||||||
using NModbus;
|
using NModbus;
|
||||||
using NModbus.Serial;
|
using NModbus.Serial;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO.Ports;
|
using System.IO.Ports;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace EPTracer
|
namespace EPTracer
|
||||||
{
|
{
|
||||||
@@ -17,7 +21,7 @@ namespace EPTracer
|
|||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("***************************************************************");
|
Console.WriteLine("***************************************************************");
|
||||||
Console.WriteLine("Loading config...");
|
Console.WriteLine("Loading config...");
|
||||||
|
|
||||||
IConfiguration config = new ConfigurationBuilder()
|
IConfiguration config = new ConfigurationBuilder()
|
||||||
.AddJsonFile("appsettings.json", true, true)
|
.AddJsonFile("appsettings.json", true, true)
|
||||||
.Build();
|
.Build();
|
||||||
@@ -27,7 +31,13 @@ namespace EPTracer
|
|||||||
string token = config["token"];
|
string token = config["token"];
|
||||||
string bucket = config["bucket"];
|
string bucket = config["bucket"];
|
||||||
string org = config["org"];
|
string org = config["org"];
|
||||||
|
|
||||||
|
string mqtt_server = config["mqtt_server"];
|
||||||
|
string mqtt_user = config["mqtt_user"];
|
||||||
|
string mqtt_password = config["mqtt_password"];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine($"Serial Port: {portConf}");
|
Console.WriteLine($"Serial Port: {portConf}");
|
||||||
Console.WriteLine($"Interval: {interval}");
|
Console.WriteLine($"Interval: {interval}");
|
||||||
@@ -41,13 +51,16 @@ namespace EPTracer
|
|||||||
|
|
||||||
using (SerialPort port = new SerialPort(portConf))
|
using (SerialPort port = new SerialPort(portConf))
|
||||||
{
|
{
|
||||||
|
var mqttClient = MqttConnect(mqtt_server, mqtt_user, mqtt_password);
|
||||||
|
var time = DateTime.Now;
|
||||||
|
|
||||||
Console.WriteLine($"Configuring serial port: {portConf}");
|
Console.WriteLine($"Configuring serial port: {portConf}");
|
||||||
// configure serial port
|
// configure serial port
|
||||||
port.BaudRate = 115200;
|
port.BaudRate = 115200;
|
||||||
port.DataBits = 8;
|
port.DataBits = 8;
|
||||||
port.Parity = Parity.None;
|
port.Parity = Parity.None;
|
||||||
port.StopBits = StopBits.One;
|
port.StopBits = StopBits.One;
|
||||||
|
|
||||||
Console.WriteLine("Opening port");
|
Console.WriteLine("Opening port");
|
||||||
port.Open();
|
port.Open();
|
||||||
|
|
||||||
@@ -62,6 +75,16 @@ namespace EPTracer
|
|||||||
Console.WriteLine(".....");
|
Console.WriteLine(".....");
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
if (mqttClient == null)
|
||||||
|
{
|
||||||
|
// try to connect every 3 minutes
|
||||||
|
if (DateTime.Now > time.AddMinutes(3))
|
||||||
|
{
|
||||||
|
mqttClient = MqttConnect(mqtt_server, mqtt_user, mqtt_password);
|
||||||
|
time = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//read registers
|
//read registers
|
||||||
ushort[] registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters);
|
ushort[] registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters);
|
||||||
|
|
||||||
@@ -81,7 +104,7 @@ namespace EPTracer
|
|||||||
using (var client = InfluxDBClientFactory.Create(influxdb, token))
|
using (var client = InfluxDBClientFactory.Create(influxdb, token))
|
||||||
{
|
{
|
||||||
client.SetLogLevel(InfluxDB.Client.Core.LogLevel.Body);
|
client.SetLogLevel(InfluxDB.Client.Core.LogLevel.Body);
|
||||||
|
|
||||||
var timestamp = DateTime.UtcNow;
|
var timestamp = DateTime.UtcNow;
|
||||||
var point = PointData.Measurement("solar")
|
var point = PointData.Measurement("solar")
|
||||||
.Tag("unit", "1")
|
.Tag("unit", "1")
|
||||||
@@ -106,9 +129,56 @@ namespace EPTracer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.Threading.Thread.Sleep(interval * 1000);
|
var messages = new List<MqttApplicationMessage>();
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/pvv").WithPayload(pvv.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/pva").WithPayload(pva.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/pvw").WithPayload(pvw.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/btv").WithPayload(btv.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/bta").WithPayload(bta.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/btw").WithPayload(btw.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/ldv").WithPayload(ldv.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/lda").WithPayload(lda.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/ldw").WithPayload(ldw.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/btt").WithPayload(btt.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/btc").WithPayload(btc.ToString("0.00")).Build());
|
||||||
|
messages.Add(new MqttApplicationMessageBuilder().WithTopic("ep/dev1/rbt").WithPayload(rbt.ToString("0.00")).Build());
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (var message in messages)
|
||||||
|
{
|
||||||
|
mqttClient.PublishAsync(message, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(interval * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static MQTTnet.Client.MqttClient MqttConnect(string server, string user, string password)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Connecting to MQTT server...");
|
||||||
|
var mqttFactory = new MqttFactory();
|
||||||
|
var mqttClient = mqttFactory.CreateMqttClient() as MQTTnet.Client.MqttClient;
|
||||||
|
var mqttClientOptions = new MqttClientOptionsBuilder()
|
||||||
|
.WithTcpServer(server)
|
||||||
|
.WithCredentials(user, password)
|
||||||
|
.Build();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
||||||
|
Console.WriteLine("MQTT connected!");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
mqttClient = null;
|
||||||
|
}
|
||||||
|
return mqttClient;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,8 @@
|
|||||||
"influxdb": "http://127.0.0.1:8086",
|
"influxdb": "http://127.0.0.1:8086",
|
||||||
"token": "l8GGGVe6YaNTfvsE5h4WLMLOEVkOL49NyOvCD3eV1LqI4va2Uh9Acza0-_UIhbuT5FdJe5FniRlMoWtHM3kV-A==",
|
"token": "l8GGGVe6YaNTfvsE5h4WLMLOEVkOL49NyOvCD3eV1LqI4va2Uh9Acza0-_UIhbuT5FdJe5FniRlMoWtHM3kV-A==",
|
||||||
"bucket": "solar",
|
"bucket": "solar",
|
||||||
"org": "flexa"
|
"org": "flexa",
|
||||||
|
"mqtt_server": "10.20.0.6",
|
||||||
|
"mqtt_user": "mqtt",
|
||||||
|
"mqtt_password": "mqtt"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user