private static void OnCallback(object state) { switch ((ETimer)state) { case ETimer.Ping: if (NetduinoMQTT == null) { break; } lock (NetduinoMQTT) { // Our keep alive is 15 seconds - we ping again every 10, So we should live forever Debug.Print("Pinging <" + MQTT_PORT + "> port on <" + MQTT_SERVER + "> server..."); if (NetduinoMQTT.PingMQTT(Socket) == -1) { NetduinoMQTT.DisconnectMQTT(Socket); NetduinoMQTT = null; } } break; case ETimer.Scan: if (I2C == null) { break; } lock (I2C) lock (NetduinoMQTT) { Scanner.ScanSlaves(I2C); FetchFunction(); } break; case ETimer.RGB: if (LockRGB == null) { break; } lock (LockRGB) { if (Notify.Length != 0) { // Notify end user, status is reserved Debug.Print("Done! RGB LED status was changed from <" + NotifyFlag.ToString() + "> to <" + Notify.Peek().ToString() + "> status."); SetRGBStatus(Notify.Dequeue()); } } break; default: break; } }
// the interrupt handler static void button_OnInterrupt(uint data1, uint data2, DateTime time) { int returnCode = 0; int connectionError = 0; // Get broker's IP address. IPHostEntry hostEntry = Dns.GetHostEntry("192.168.1.106"); // Create socket and connect to the broker's IP address and port Socket 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"); connectionError = 1; } if (connectionError != 1) { // Send the connect message returnCode = NetduinoMQTT.ConnectMQTT(mySocket, "tester\u00A5", 2, true, "roger\u00A5", "password\u00A5"); if (returnCode != 0) { Debug.Print("Connection Error:"); Debug.Print(returnCode.ToString()); } else { // Send our message NetduinoMQTT.PublishMQTT(mySocket, "test", "Ow! Quit it!"); for (int i = 0; i < 11; i++) { returnCode = NetduinoMQTT.PingMQTT(mySocket); if (returnCode == 0) { Debug.Print("Ping Received"); Thread.Sleep(1000); } } Thread.Sleep(3000); // Send the disconnect message NetduinoMQTT.DisconnectMQTT(mySocket); } // Close the socket mySocket.Close(); } }
// The function that the timer calls to ping the server // Our keep alive is 15 seconds - we ping again every 10. // So we should live forever. static void pingIt(object o) { Debug.Print("pingIT"); NetduinoMQTT.PingMQTT(mySocket); }