Beispiel #1
0
#pragma warning disable 1998
        public async Task SendMessageToIoTHubAsync(string deviceId, string location, string key, object value)
#pragma warning restore 1998
        {
            try
            {
                string strvalue = null;
                if (value is double || value is float)
                {
                    strvalue = $"{((double)value):F}";
                }
                else
                {
                    strvalue = $"\"{value.ToString()}\"";
                }

                var payload =
                    $"{{\"deviceid\": \"{deviceId}\", \"location\": \"{location}\", \"channelvalue\": {value}, \"channelkey\": \"{key}\", \"localtimestamp\": \"{DateTime.Now.ToUniversalTime():O}\"}}";

                _mqttclient.Publish($"devices/{_deviceid}/messages/events/", Encoding.UTF8.GetBytes(payload));
                Debug.WriteLine(payload);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                //TODO Log
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***********PUBLISHER***********");

            //MqttClient client = new MqttClient("dev.rabbitmq.com", 1883, false, null);
            MqttClient client = new MqttClient("localhost", 1883, false, null);
            var state = client.Connect("Client993", "guest", "guest", false, 0, false, null, null, true, 60);

            string strValue = Convert.ToString("On");

            while (true)
            {
                // publish a message with command to turn LED on
                client.Publish("ABC12345/led", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                mcount++;

                Console.WriteLine(String.Format("Message {0} published.", mcount.ToString()));

                Console.WriteLine("Press enter to send Off command...");
                Console.ReadLine();

                // publish a message with command to turn LED on
                client.Publish("ABC12345/led", Encoding.UTF8.GetBytes("Off"), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                mcount++;

                Console.WriteLine(String.Format("Message {0} published.", mcount.ToString()));

                Console.WriteLine("Press enter to send On comamnd...");
                Console.ReadLine();
            }
        }
        static void Main(string[] args)
        {
            try
            {
                localClient = new uPLibrary.Networking.M2Mqtt.MqttClient("172.30.28.5");
                string clientId = Guid.NewGuid().ToString();
                localClient.Connect(clientId);

                XmlDocument doc = new XmlDocument();
                doc.Load("\\Users\\Rodric\\Desktop\\values.xml");

                XmlNodeList elementList = doc.GetElementsByTagName("book");
                XmlNode node = elementList[1];

                for (int i = 0; i < elementList.Count; i++)
                {
                    string loc = elementList[i]["location"].InnerText;
                    string temp = elementList[i]["temperature"].InnerText;
                    string time = elementList[i]["time"].InnerText;

                    string allData = loc + ";" + temp + ";" + time;
                    byte[] data = Encoding.UTF8.GetBytes(allData);

                    localClient.Publish("/home/temperature", Encoding.UTF8.GetBytes(allData), uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Connection Failed: " + ex.Message);
            }
        }
Beispiel #4
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // create client instance
            MqttClient client = new MqttClient("test.mosquitto.org");

            client.Connect("Test VS",null,null);

             //           string strValue = Convert.ToString(value);

            // publish a message on "/home/temperature" topic with QoS 2
            client.Publish("/home/temperature", Encoding.UTF8.GetBytes("test"));
        }
        protected override void OnStart(string[] args)
        {
            t = new Timer(10000); // Timer de 10 secondes.
            t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            t.Start();
            // create client instance
            MqttClient client = new MqttClient("test.mosquitto.org");

            client.Connect("SMO_IoT_WindowsService", null, null);
            // publish a message on "/home/temperature" topic with QoS 2
            IoT_Topics iot = new IoT_Topics();
            Iot_Constants IOTCONST = new Iot_Constants();
            string sTopic = iot.Get_Topics("", "");
            client.Publish("ou", Encoding.UTF8.GetBytes(sTopic));
        }
Beispiel #6
0
        public RendererControl()
        {
            client = new MqttClient("127.0.0.1");
            client.Connect(new Guid().ToString());

            Get["/"] = (v) =>
            {
                return "See <a href=\"on\">on</a> and <a href=\"off\">off</a>";
            };

            Get["/on"] = _ =>
            {
                client.Publish("/lights/standing/state", new[] { (byte)1 });

                return "Lights set to on";
            };

            Get["/off"] = _ =>
            {
                client.Publish("/lights/standing/state", new[] { (byte)0 });

                return "Lights set to off";
            };
        }
Beispiel #7
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            m_Deferral = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstanceCanceled;

            // initialize I2C
            var i2cSettings = new I2cConnectionSettings(TSL2561.Address)
            {
                BusSpeed = I2cBusSpeed.FastMode,
                SharingMode = I2cSharingMode.Shared,
            };
            var selector = I2cDevice.GetDeviceSelector(m_DeviceSelector);
            var deviceInformation = await DeviceInformation.FindAllAsync(selector);
            m_Device = await I2cDevice.FromIdAsync(deviceInformation[0].Id, i2cSettings);

            // initialize sensor
            m_Sensor = new TSL2561(m_Device);
            m_Sensor.SetTiming(m_Gain, m_Timing);
            m_Sensor.PowerUp();

            // initialize MQTT
            try
            {
                m_Client = new MqttClient(m_Host);
                m_Client.Connect(m_ClientId);
            }
            catch (MqttConnectionException ex)
            {
                // ignore connection exception and retry to connect later
                Debug.WriteLine("Cannot connect to MQTT broker: " + ex.Message);
                m_Client = null;
            }

            var appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (appService != null && appService.Name == m_ServiceName)
            {
                m_AppServiceConnection = appService.AppServiceConnection;
                m_AppServiceConnection.RequestReceived += OnRequestReceived;
            }

            m_Client.Publish(m_ServiceTopic, Encoding.UTF8.GetBytes(DateTime.Now.ToUniversalTime().ToString("O")), m_QoS, true);

            // start timer
            m_Timer = new Timer(LuxProvider, null, m_TimerDueTime, m_TimerInterval);
        }
Beispiel #8
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                MqttClient client = new MqttClient("m20.cloudmqtt.com:18478");
                byte code = client.Connect("MyClient_MQTT", "xzofiqbg", "igNpeCm_8j6f");
                client.MqttMsgPublished += client_MqttMsgPublished;
                ushort msgId = client.Publish("/my_topic", // topic
                              Encoding.UTF8.GetBytes("MyMessageBody"), // message body
                              MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level
                              false); // retained

            }
            catch
            {
                textBox1.Text = "Erreur catch";
            }
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            // create client instance 
            MqttClient client = new MqttClient(IPAddress.Parse("192.168.178.30"), 1883,false, MqttSslProtocols.None,null,null);

            string clientId = Guid.NewGuid().ToString();
            client.Connect(clientId);

            Random r = new Random((int)DateTime.Now.Ticks);
            //string strValue = Convert.ToString(23.00);
            double stuff = 0;
            while (true)
            {
                // publish a message on "/home/temperature" topic with QoS 2
                stuff = r.NextDouble() * 100;
                client.Publish("sensors/temperature/dht22", Encoding.UTF8.GetBytes(Convert.ToString(stuff)), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                Console.WriteLine(stuff);
                Thread.Sleep(1000);
            }
        }
        static void Main(string[] args)
        {
            MqttClient clientPub = new MqttClient(IPAddress.Parse("127.0.0.1"));
            string clientIdPub = Guid.NewGuid().ToString();
            clientPub.Connect(clientIdPub);
            string strValue = Convert.ToString("{‘data’ : {‘id’ : ‘121212122’},'timestamp' : '2015-07-16T11:35:02+00:00'}");
            clientPub.Publish("v1/items/hanger/1003430/event/active",
            Encoding.UTF8.GetBytes(strValue),
            MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
            try
            {

            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
            //It is important to have the last param set false. It is the retain parameter, if set to true
            //this will retain the message in broker and will send it for new subscribers which will
            //trigger unsolicited events.
        }
Beispiel #11
0
        private void Publish_Click(object sender, RoutedEventArgs e)
        {
            MqttClient Mq;
            string broker = "192.168.42.1";
            //string broker = "127.0.0.1";

            Refresh_Click(this, null);
            Mq = new MqttClient(broker);
            Mq.Connect("pNavPlan3");
            System.Diagnostics.Trace.WriteLine($"Connected to MQTT @ {broker}", "1");
            var jsn = NavPointText;
            Mq.Publish("Navplan/WayPoints", Encoding.ASCII.GetBytes(jsn));
            System.Threading.Thread.Sleep(200);

            Mq.Disconnect();
            StatusText = "NavPoints published";
        }
 private void sendMQT(KPIN kpin)
 {
     //this is same as the code taken from the pdf
     //MqttClient clientPub = new MqttClient(IPAddress.Parse(ipAddress));
     string timeStamp = (DateTime.UtcNow).ToString("o");
     string topic = "v1/items/pin/" + kpin.getUnitId() + "/" + kpin.getEvent();
     MqttClient clientPub = new MqttClient(ipAddress);
     string clientIdPub = Guid.NewGuid().ToString();
     clientPub.Connect(clientIdPub);
     //{"datetime": "2015-07-15t13:18:46z","data" :{“actuatorID”:”1234”,”productID”:”123”}}
     string strValue = Convert.ToString("{\"datetime\": \"" + timeStamp + "\",\"data\" :{\"actuatorID\": \"" + kpin.getUnitId() + "\",\"productID\":\"\"}}");
     //string strValue = Convert.ToString("{‘data’ : {‘id’ : ‘" + kpin.getUnitId() + "’},'timestamp' : '"+ timeStamp +"'}");
     clientPub.Publish(topic, Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
     //It is important to have the last param set false. It is the retain parameter, if set to true
     //this will retain the message in broker and will send it for new subscribers which will
     //trigger unsolicited events.
 }
Beispiel #13
0
 private void SendMessage(JSONNode json)
 {
     byte[] message = Encoding.UTF8.GetBytes(json.ToString());
     mqttClient.Publish(mqttTopic, message, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
 }