How to C# connect to Ouster Gemini MQTT output
C# implementation of an MQTT client that subscribes to topics published by Ouster
Gemini. The client connects to an MQTT broker, subscribes to the specified topic, and prints the received
messages.
System Requirements (tested on Ubuntu 22.04)
.NET SDK 8.0
M2Mqtt Library
Getting Started
Step 1: Install .NET SDK
Update the package index:
sudo apt updateInstall the .NET SDK:
sudo apt install dotnet-sdk-8.0Step 2: Create a New C# Project
Create a new directory for your project:
mkdir -p ~/mqtt_client
cd ~/mqtt_clientInitialize a new C# console project:
dotnet new console -n MQTTClient
cd MQTTClientStep 3: Add the M2Mqtt Library
Install the M2Mqtt library via NuGet:
dotnet add package M2MqttStep 4: Replace the Default Code
Open the Program.cs file:
nano Program.csReplace the contents with the following code:
using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
class MQTTClient
{
static string broker = "192.168.1.15";
static int port = 1883;
static string topic = "/ouster/detect/data";
static string clientId = "subscribe-" + new Random().Next(0, 100);
static void Main(string[] args)
{
MqttClient client = new MqttClient(broker, port, false, null,
null, MqttSslProtocols.None);
client.MqttMsgPublishReceived += (sender, e) =>
{
string message =
System.Text.Encoding.UTF8.GetString(e.Message);
Console.WriteLine($"Received `{message}` from `{e.Topic}`topic");
};
client.Connect(clientId);
if (client.IsConnected)
{
Console.WriteLine("Connected to MQTT Broker!");
client.Subscribe(new string[] { topic }, new byte[] {
MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
}
else
{
Console.WriteLine("Failed to connect to MQTT Broker.");
}
// Keep the application running to listen for messages
Console.ReadLine();
}
}
Save and close the file (CTRL + S, then Enter, and CTRL + X to exit).
Step 5: Build and Run the Application
Build the project:
dotnet buildRun the application:
dotnet run