示例#1
0
        private async void onMqttConnectionClosed(object sender, EventArgs e)
        {
            var retryCount = 0;

            while (!Mqtt.IsConnected && retryCount < 3)
            {
                try
                {
                    Mqtt.Connect(Config.Mqtt.ClientId, null, null, false, 60);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Reconnect failed: " + exception.Message);
                }
                finally
                {
                    await Task.Delay(TimeSpan.FromSeconds(1), Token);
                }
            }
        }
        static void Main()
        {
            Debug.WriteLine("Setup Ethernet");
            SetupEnc28_MicroBus(SC20100.SpiBus.Spi3, SC20100.GpioPin.PD14, SC20100.GpioPin.PA8, SC20100.GpioPin.PD15, SC20100.GpioPin.Id);
            Thread.Sleep(50);

            Debug.WriteLine("Setup Date and Time from Internet");
            Debug.WriteLine("===================================");

            while
            ((DateTime.UtcNow.ToString("yyyy.MM.dd") == "1900.01.01") || (DateTime.UtcNow.ToString("yyyy.MM.dd") == "2017.01.01"))
            {
                SystemTime.SetTime(GetNetworkTime(2));
                Thread.Sleep(15000);
                // Debug.WriteLine("Try again after 15 sec");
            }

            Debug.WriteLine("Set Proper Time now is :" + DateTime.UtcNow.ToString("yyyy.MM.dd HH:mm:ss"));
            Debug.WriteLine("===================================");

            //test.mosquitto.org
            var caCertificate = new X509Certificate(Certificate());

            var mqttHost = "test.mosquitto.org";
            //var mqttHost = "broker.hivemq.com";

            var mqttPort = 1883;//1883 no ssl - 8883 with ssl
            var deviceId = "tiny_clr_device_001";

            string username = null;
            string password = null;

            string topic1 = "tinyclr/temp";
            string topic2 = "tinyclr/humidity";

            string subscribe1 = "tinyclr/light1";
            string subscribe2 = "tinyclr/light2";

            var clientSetting = new MqttClientSetting
            {
                BrokerName = mqttHost,
                BrokerPort = mqttPort,
                //  ClientCertificate = null,
                //  CaCertificate = caCertificate,
                //  SslProtocol = System.Security.Authentication.SslProtocols.Tls12
            };

            try
            {
                client = new Mqtt(clientSetting);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failure one");
                Debug.WriteLine(e.Message.ToString());
            }


            var connectSetting = new MqttConnectionSetting
            {
                ClientId         = deviceId,
                UserName         = username,
                Password         = password,
                KeepAliveTimeout = 60,
                LastWillQos      = QoSLevel.MostOnce

                                   /*
                                    * QOS(0) = MostOnce
                                    * QOS(1) = LeastOnce
                                    * QOS(2) = ExactlyOnce
                                    */
            };

            //Connect to host


            var returnCode = client.Connect(connectSetting);
            int packetId   = 1;

            client.PublishReceivedChanged += Client_PublishReceivedChanged;

            client.SubscribedChanged += Client_SubscribedChanged;



            // Subscribe to a topic
            client.Subscribe(new string[] { subscribe1, subscribe2 }, new QoSLevel[] { QoSLevel.MostOnce, QoSLevel.MostOnce }, (ushort)packetId++);  // there was no QoS Level for the second topic in your code


            while (true)
            {
                Debug.WriteLine("------------------------------");
                Debug.WriteLine(DateTime.UtcNow.ToString(">> yyyy.MM.dd HH:mm:ss.fff"));

                //Publish a topic
                client.Publish(topic1, GetRandomTemperature(), QoSLevel.MostOnce, false, (ushort)packetId);
                packetId++;



                //Publish a topic
                client.Publish(topic2, GetRandomHumidity(), QoSLevel.MostOnce, false, (ushort)packetId);
                packetId++;


                client.PublishedChanged += (a, b, c) =>
                {
                    Debug.WriteLine("Published " + a.ToString());
                };

                Debug.WriteLine("Wait next 15 sec");
                Thread.Sleep(15000);
            }
        }