AlarmHandler.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. // Data wire is plugged into port 2 on the Arduino
  4. #define ONE_WIRE_BUS 2
  5. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  6. OneWire oneWire(ONE_WIRE_BUS);
  7. // Pass our oneWire reference to Dallas Temperature.
  8. DallasTemperature sensors(&oneWire);
  9. // arrays to hold device addresses
  10. DeviceAddress insideThermometer, outsideThermometer;
  11. // function that will be called when an alarm condition exists during DallasTemperatures::processAlarms();
  12. void newAlarmHandler(const uint8_t* deviceAddress)
  13. {
  14. Serial.println("Alarm Handler Start");
  15. printAlarmInfo(deviceAddress);
  16. printTemp(deviceAddress);
  17. Serial.println();
  18. Serial.println("Alarm Handler Finish");
  19. }
  20. void printCurrentTemp(DeviceAddress deviceAddress)
  21. {
  22. printAddress(deviceAddress);
  23. printTemp(deviceAddress);
  24. Serial.println();
  25. }
  26. void printAddress(const DeviceAddress deviceAddress)
  27. {
  28. Serial.print("Address: ");
  29. for (uint8_t i = 0; i < 8; i++)
  30. {
  31. if (deviceAddress[i] < 16) Serial.print("0");
  32. Serial.print(deviceAddress[i], HEX);
  33. }
  34. Serial.print(" ");
  35. }
  36. void printTemp(const DeviceAddress deviceAddress)
  37. {
  38. float tempC = sensors.getTempC(deviceAddress);
  39. if (tempC != DEVICE_DISCONNECTED_C)
  40. {
  41. Serial.print("Current Temp C: ");
  42. Serial.print(tempC);
  43. }
  44. else Serial.print("DEVICE DISCONNECTED");
  45. Serial.print(" ");
  46. }
  47. void printAlarmInfo(const DeviceAddress deviceAddress)
  48. {
  49. char temp;
  50. printAddress(deviceAddress);
  51. temp = sensors.getHighAlarmTemp(deviceAddress);
  52. Serial.print("High Alarm: ");
  53. Serial.print(temp, DEC);
  54. Serial.print("C");
  55. Serial.print(" Low Alarm: ");
  56. temp = sensors.getLowAlarmTemp(deviceAddress);
  57. Serial.print(temp, DEC);
  58. Serial.print("C");
  59. Serial.print(" ");
  60. }
  61. void setup(void)
  62. {
  63. // start serial port
  64. Serial.begin(9600);
  65. Serial.println("Dallas Temperature IC Control Library Demo");
  66. // Start up the library
  67. sensors.begin();
  68. // locate devices on the bus
  69. Serial.print("Found ");
  70. Serial.print(sensors.getDeviceCount(), DEC);
  71. Serial.println(" devices.");
  72. // search for devices on the bus and assign based on an index
  73. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  74. if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
  75. Serial.print("Device insideThermometer ");
  76. printAlarmInfo(insideThermometer);
  77. Serial.println();
  78. Serial.print("Device outsideThermometer ");
  79. printAlarmInfo(outsideThermometer);
  80. Serial.println();
  81. // set alarm ranges
  82. Serial.println("Setting alarm temps...");
  83. sensors.setHighAlarmTemp(insideThermometer, 26);
  84. sensors.setLowAlarmTemp(insideThermometer, 22);
  85. sensors.setHighAlarmTemp(outsideThermometer, 25);
  86. sensors.setLowAlarmTemp(outsideThermometer, 21);
  87. Serial.print("New insideThermometer ");
  88. printAlarmInfo(insideThermometer);
  89. Serial.println();
  90. Serial.print("New outsideThermometer ");
  91. printAlarmInfo(outsideThermometer);
  92. Serial.println();
  93. // attach alarm handler
  94. sensors.setAlarmHandler(&newAlarmHandler);
  95. }
  96. void loop(void)
  97. {
  98. // ask the devices to measure the temperature
  99. sensors.requestTemperatures();
  100. // if an alarm condition exists as a result of the most recent
  101. // requestTemperatures() request, it exists until the next time
  102. // requestTemperatures() is called AND there isn't an alarm condition
  103. // on the device
  104. if (sensors.hasAlarm())
  105. {
  106. Serial.println("Oh noes! There is at least one alarm on the bus.");
  107. }
  108. // call alarm handler function defined by sensors.setAlarmHandler
  109. // for each device reporting an alarm
  110. sensors.processAlarms();
  111. if (!sensors.hasAlarm())
  112. {
  113. // just print out the current temperature
  114. printCurrentTemp(insideThermometer);
  115. printCurrentTemp(outsideThermometer);
  116. }
  117. delay(1000);
  118. }