Install-Package HiveMQ.Client
Install-Package System.Reactive

Code to get Earthquake data from USGS and sent to HiveMQ server on localhost

using System;
using System.IO;
using System.Net;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Text;
using HiveMQ.Client;
using HiveMQ.Client.MqttClient;
using HiveMQ.Client.MqttClient.Callbacks;
using HiveMQ.Client.MqttClient.Exceptions;
 
class Program
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        // MQTT broker details
        string brokerIpAddress = "localhost"; // Update with the hostname or IP address of your local server
        int brokerPort = 1883;
 
        // MQTT topic to subscribe and publish to
        string topic = "your_topic";
 
        // Create an MQTT client
        var mqttClient = new MqttClientFactory().CreateMqttClient();
 
        // Register to message received event using Rx.NET
        var messageReceived = mqttClient
            .MessageStream
            .Where(e => e.ApplicationMessage.Topic == topic)
            .Select(e => Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
 
        using (messageReceived.Subscribe(message => Console.WriteLine("Received message: " + message)))
        {
            // Connect to the MQTT broker
            try
            {
                await mqttClient.ConnectAsync(new MqttClientOptionsBuilder()
                    .WithClientId(Guid.NewGuid().ToString())
                    .WithTcpServer(brokerIpAddress, brokerPort)
                    .Build());
            }
            catch (MqttClientException ex)
            {
                Console.WriteLine("Error connecting to MQTT broker: " + ex.Message);
                return;
            }
 
            // Send a message to the MQTT broker
            string message = await GetMessageFromURL("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojsonp");
            await mqttClient.PublishAsync(new MqttApplicationMessageBuilder()
                .WithTopic(topic)
                .WithPayload(Encoding.UTF8.GetBytes(message))
                .WithExactlyOnceQoS()
                .Build());
 
            Console.WriteLine("Message sent to MQTT broker.");
 
            // Keep the application running until user input
            Console.ReadLine();
 
            // Disconnect from the MQTT broker
            await mqttClient.DisconnectAsync();
        }
    }
 
    // Retrieve a message from a URL
    private static async System.Threading.Tasks.Task<string> GetMessageFromURL(string url)
    {
        string message = string.Empty;
        try
        {
            WebClient client = new WebClient();
            string jsonp = await client.DownloadStringTaskAsync(url);
 
            // Extract the JSON content from JSONP
            int startIndex = jsonp.IndexOf('(') + 1;
            int endIndex = jsonp.LastIndexOf(')');
            if (startIndex >= 0 && endIndex >= 0)
            {
                message = jsonp.Substring(startIndex, endIndex - startIndex);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error retrieving message from URL: " + ex.Message);
        }
        return message;
    }
}

Installing HiveMQ

To install and run HiveMQ server locally, follow these steps:

  1. Download the HiveMQ Community Edition ZIP file from the HiveMQ website: https://www.hivemq.com/downloads/#hivemq-ce.
  2. Extract the contents of the ZIP file to a directory on your local machine.
  3. Open a command prompt or terminal and navigate to the HiveMQ installation directory.
  4. Run the following command to start the HiveMQ server: bashCopy code bin/run.sh On Windows, use the following command instead: bashCopy code bin\run.bat This command will start the HiveMQ server.
  5. By default, the HiveMQ server listens on port 1883 for MQTT connections and port 8000 for the Web UI. You can change these settings in the conf/config.xml file if needed.

With the HiveMQ server running locally, you can now use the provided code to connect to the server, publish and subscribe to topics, and exchange messages with MQTT clients.

Make sure to update the brokerIpAddress variable in the code with the IP address or hostname of your local machine where the HiveMQ server is running.

Remember to configure appropriate security measures such as authentication and encryption if required for your local HiveMQ setup, especially in a production environment. The default installation is for local development and may not include advanced security features.