private static bool SubscribeTopic(byte address, bool type) { // When we found given device in device list, generate MQTT vendor(s) for (ushort index = 0; index < Slave.Length; index++) { if (address == Slave[index].Address) { string convertedAddress = "0x" + GenerateHexadecimal(address); string[] topicList = { "isConnected", "brand", "model", "version" }; string[] messageList = { type ? "1" : "0", Slave[index].Vendor.Brand, Slave[index].Vendor.Model, Slave[index].Vendor.Version }; for (int iterator = 0; iterator < topicList.Length; iterator++) { string[] data = { convertedAddress, topicList[iterator] }; string result = '/' + Serializer.Encode('/', data); if (type || iterator == 0) { NetduinoMQTT.PublishMQTT(Socket, result, messageList[iterator]); } } // ----- for (ushort subindex = 0; subindex < Slave[index].Function.Length; subindex++) { if (!Slave[index].Function[subindex].Request) { continue; } string[] data = { convertedAddress, Slave[index].Function[subindex].Name }; string result = '/' + Serializer.Encode('/', data); // Looking good, inline if-loop if (type) { NetduinoMQTT.SubscribeMQTT(Socket, new string[] { result }, new int[] { 0 }, 1); } else { NetduinoMQTT.UnsubscribeMQTT(Socket, new string[] { result }, new int[] { 0 }, 1); } } // Do not need to search all data, we are OK now return(true); } } // Worst case, when not find anything we will arrive there return(false); }
private static bool InitializeMQTT() { // Wait a little bit, That will make better the next step Thread.Sleep(500); try { // Get broker's IP of MQTT address //IPHostEntry hostEntry = Dns.GetHostEntry(MQTT_SERVER); IPHostEntry hostEntry = Dns.GetHostEntry(MQTT_SERVER); // Create socket and connect to the broker's IP address and port Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp); Socket.Connect(new IPEndPoint(hostEntry.AddressList[0], MQTT_PORT)); // Create MQTT and connect to the broker's port with username and password NetduinoMQTT = new NetduinoMQTT(); if (NetduinoMQTT.ConnectMQTT(Socket, DEVICE_MODEL, MQTT_TIMEOUT, true, MQTT_USER, MQTT_PASSWORD) != 0) { throw new Exception(); } Debug.Print("Done! Socket Connection was established successfully."); // IMPORTANT NOTICE: First of all, we need to subscribe main device // We call it like XXXX/status and this broker is related with SSR // State of device. We will listen something about this and execute it string[] data = { DEVICE_MODEL, "status" }; string result = '/' + Serializer.Encode(new char[1] { '/' }, data); NetduinoMQTT.SubscribeMQTT(Socket, new string[] { result }, new int[] { 0 }, 1); Debug.Print("Done! MQTT Configuration was established successfully <" + MQTT_PORT + "> port on <" + MQTT_SERVER + "> server."); // Best case return(true); } catch (SocketException error) { // Worst case Debug.Print("Error! Unexpected Socket Error <" + error.ErrorCode + "> triggered."); return(false); } }
public static void Main() { int returnCode = 0; // You can subscribe to multiple topics in one go // (If your broker supports this RSMB does, mosquitto does not) // Our examples use one topic per request. // //int[] topicQoS = { 0, 0 }; //String[] subTopics = { "test", "test2" }; //int numTopics = 2; int[] topicQoS = { 0 }; String[] subTopics = { "test" }; int numTopics = 1; // Get broker's IP address. //IPHostEntry hostEntry = Dns.GetHostEntry("test.mosquitto.org"); IPHostEntry hostEntry = Dns.GetHostEntry("192.168.1.106"); // Create socket and connect to the broker's IP address and port mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { mySocket.Connect(new IPEndPoint(hostEntry.AddressList[0], 1883)); } catch (SocketException SE) { Debug.Print("Connection Error: " + SE.ErrorCode); return; } // Send the connect message // You can use UTF8 in the clientid, username and password - be careful, this can be a pain //returnCode = NetduinoMQTT.ConnectMQTT(mySocket, "tester\u00A5", 2000, true, "roger\u00A5", "password\u00A5"); returnCode = NetduinoMQTT.ConnectMQTT(mySocket, "tester402", 20, true, "roger", "password"); if (returnCode != 0) { Debug.Print("Connection Error: " + returnCode.ToString()); return; } // Set up so that we ping the server after 1 second, then every 10 seconds // First time is initial delay, Second is subsequent delays Timer pingTimer = new Timer(new TimerCallback(pingIt), null, 1000, 10000); // Setup and start a new thread for the listener listenerThread = new Thread(mylistenerThread); listenerThread.Start(); // setup our interrupt port (on-board button) InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow); // assign our interrupt handler button.OnInterrupt += new NativeEventHandler(button_OnInterrupt); // Subscribe to our topic(s) returnCode = NetduinoMQTT.SubscribeMQTT(mySocket, subTopics, topicQoS, numTopics); //*********************************************** // This is just some example stuff: //*********************************************** // Publish a message NetduinoMQTT.PublishMQTT(mySocket, "test", "Testing from NetduinoMQTT"); // Subscribe to "test/two" subTopics[0] = "test/two"; returnCode = NetduinoMQTT.SubscribeMQTT(mySocket, subTopics, topicQoS, numTopics); // Send a message to "test/two" NetduinoMQTT.PublishMQTT(mySocket, "test/two", "Testing from NetduinoMQTT to test/two"); // Unsubscribe from "test/two" returnCode = NetduinoMQTT.UnsubscribeMQTT(mySocket, subTopics, topicQoS, numTopics); // go to sleep until the interrupt or the timer wakes us // (mylistenerThread is in a seperate thread that continues) Thread.Sleep(Timeout.Infinite); }