mqtt
This commit is contained in:
@@ -2,17 +2,18 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="InfluxDB.Client" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="NModbus" Version="3.0.72" />
|
||||
<PackageReference Include="NModbus.Serial" Version="3.0.72" />
|
||||
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
|
||||
<PackageReference Include="InfluxDB.Client" Version="4.14.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="MQTTnet" Version="4.3.3.952" />
|
||||
<PackageReference Include="NModbus" Version="3.0.81" />
|
||||
<PackageReference Include="NModbus.Serial" Version="3.0.81" />
|
||||
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
using InfluxDB.Client.Api.Domain;
|
||||
using InfluxDB.Client.Writes;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using NModbus;
|
||||
using NModbus.Serial;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
|
||||
namespace EPTracer
|
||||
{
|
||||
@@ -28,6 +32,12 @@ namespace EPTracer
|
||||
string bucket = config["bucket"];
|
||||
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($"Serial Port: {portConf}");
|
||||
Console.WriteLine($"Interval: {interval}");
|
||||
@@ -41,6 +51,9 @@ namespace EPTracer
|
||||
|
||||
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}");
|
||||
// configure serial port
|
||||
port.BaudRate = 115200;
|
||||
@@ -62,6 +75,16 @@ namespace EPTracer
|
||||
Console.WriteLine(".....");
|
||||
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
|
||||
ushort[] registers = master.ReadInputRegisters(slaveId, startAddress, numRegisters);
|
||||
|
||||
@@ -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",
|
||||
"token": "l8GGGVe6YaNTfvsE5h4WLMLOEVkOL49NyOvCD3eV1LqI4va2Uh9Acza0-_UIhbuT5FdJe5FniRlMoWtHM3kV-A==",
|
||||
"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