Guest User

Untitled

a guest
Jan 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using System;
  2. using Microsoft.SPOT;
  3. using Microsoft.SPOT.Hardware;
  4. using System.Threading;
  5. using System.IO;
  6. using System.Net;
  7. using SecretLabs.NETMF.Hardware.Netduino;
  8. using Microsoft.SPOT.Net.NetworkInformation;
  9.  
  10. using uPLibrary.Networking.M2Mqtt;
  11. using uPLibrary.Networking.M2Mqtt.Utility;
  12. using MqttTrace = uPLibrary.Networking.M2Mqtt.Utility.Trace;
  13.  
  14. namespace NetduinoBlink
  15. {
  16. public class Program
  17. {
  18. const string MqttUsername = "...";
  19. const string MqttPassword = "...";
  20. const string MqttTopic = "home/room/temperature";
  21. const string MqttIP = "192.168.x.x";
  22.  
  23. static readonly byte[] SensorSerialNumber = new byte[8];
  24. static readonly byte[] MatchRomCommand = new byte[9];
  25.  
  26. public static void Main()
  27. {
  28. Debug.Print ("Application starting...");
  29. var port = new OutputPort (Pins.GPIO_PIN_D7, false);
  30. var oneWire = new OneWire (port);
  31. var utf8Encoding = System.Text.Encoding.UTF8;
  32.  
  33. if (!EnsureConnectedToNetwork ()) {
  34. Debug.Print ("Couldn't connect to network, bye.");
  35. return;
  36. }
  37.  
  38. MqttTrace.TraceLevel = TraceLevel.Error | TraceLevel.Frame | TraceLevel.Queuing | TraceLevel.Information | TraceLevel.Verbose | TraceLevel.Warning;
  39. MqttTrace.TraceListener = (format, args) => Debug.Print (format);
  40. MqttClient client = null;
  41. try {
  42. client = new MqttClient (MqttIP, 8887, false, null, null, MqttSslProtocols.None);
  43. if (client.Connect ("netduino", MqttUsername, MqttPassword) != 0) {
  44. Debug.Print ("Couldn't connect to MQTT broker");
  45. return;
  46. }
  47. } catch (Exception ex) {
  48. Debug.Print ("Error while connecting to MQTT");
  49. Debug.Print (ex.ToString ());
  50. return;
  51. }
  52. Debug.Print ("MQTT connected");
  53.  
  54. while (true) {
  55. try {
  56. var temp = ReadTemperature (oneWire);
  57. Debug.Print ("Current temperature is in C: " + temp);
  58.  
  59. try {
  60. var shortTemp = temp.ToString ("F2");
  61. client.Publish (MqttTopic, utf8Encoding.GetBytes (shortTemp));
  62. } catch (Exception e) {
  63. Debug.Print ("Couldn't send temp update to MQTT");
  64. Debug.Print (e.ToString ());
  65. }
  66. } catch (Exception e) {
  67. Debug.Print ("A random exception happened");
  68. Debug.Print (e.ToString ());
  69. break;
  70. } finally {
  71. oneWire.Release ();
  72. }
  73.  
  74. Thread.Sleep (20000);
  75. }
  76.  
  77. if (client.IsConnected)
  78. client.Disconnect ();
  79. }
  80.  
  81. static float ReadTemperature (OneWire oneWire)
  82. {
  83. try {
  84. oneWire.AcquireEx ();
  85. var firstDevice = oneWire.FindFirstDevice (performResetBeforeSearch: true,
  86. searchWithAlarmCommand: false);
  87. Debug.Print ("First device id: " + firstDevice);
  88. oneWire.SerialNum (SensorSerialNumber, true);
  89. Array.Copy (SensorSerialNumber, 0, MatchRomCommand, 1, 8);
  90. PrintSerial (SensorSerialNumber);
  91.  
  92. oneWire.TouchReset ();
  93. oneWire.WriteByte (0xCC /* SKIP ROM */);
  94. oneWire.WriteByte (0x44 /* ConvertT */);
  95. Thread.Sleep (750);
  96.  
  97. oneWire.TouchReset ();
  98. MatchRomCommand[0] = 0x55 /* MATCH ROM */;
  99. for (int j = 0; j < MatchRomCommand.Length; j++)
  100. oneWire.WriteByte (MatchRomCommand[j]);
  101. oneWire.WriteByte (0xBE /* Read Scratchpad */);
  102. var tempLo = (byte)oneWire.ReadByte ();
  103. var tempHi = (byte)oneWire.ReadByte ();
  104.  
  105. var temp = ConvertTemperature (tempLo, tempHi);
  106.  
  107. return temp;
  108. } finally {
  109. oneWire.Release ();
  110. }
  111. }
  112.  
  113. static float ConvertTemperature (byte tempLo, byte tempHi)
  114. {
  115. return ((short)((tempHi << 8) | tempLo)) / 16F;
  116. }
  117.  
  118. static void PrintSerial (byte[] serial)
  119. {
  120. string output = "{";
  121. for (int i = 0; i < serial.Length; i++)
  122. output += serial[i].ToString ();
  123. output += "}";
  124. Debug.Print (output);
  125. }
  126.  
  127. static bool EnsureConnectedToNetwork ()
  128. {
  129. var interfaces = NetworkInterface.GetAllNetworkInterfaces ();
  130. Debug.Print ("Found interface count: " + interfaces.Length);
  131.  
  132. if (interfaces.Length == 0)
  133. return false;
  134.  
  135. var iface = interfaces[0];
  136. if (HasDefaultIp (iface)) {
  137. if (!iface.IsDhcpEnabled) {
  138. Debug.Print ("No IP and no DHCP");
  139. return false;
  140. }
  141.  
  142. Debug.Print ("Trying to get an IP address");
  143. for (int tries = 0; tries < 100; tries++) {
  144. Thread.Sleep (100);
  145. if (!HasDefaultIp (iface))
  146. break;
  147. }
  148. if (HasDefaultIp (iface)) {
  149. Debug.Print ("Still no IP");
  150. return false;
  151. }
  152. }
  153.  
  154. Debug.Print ("Got an IP: " + iface.IPAddress);
  155.  
  156. return true;
  157. }
  158.  
  159. static bool HasDefaultIp (NetworkInterface iface) => string.Equals (iface.IPAddress, "0.0.0.0");
  160. }
  161. }
Add Comment
Please, Sign In to add comment