Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doorsensor.ino 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #define MY_DEBUG
  2. #define MY_RADIO_NRF24
  3. #define MY_NODE_ID 5
  4. #define CHILD_ID_DOOR 1
  5. #define CHILD_ID_VOLTAGE 2
  6. #define DOOR_SENSOR_PIN 3
  7. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
  8. #include <MyConfig.h>
  9. #include <MySensors.h>
  10. /**************************************************/
  11. /****************** CONSTANTS *********************/
  12. /**************************************************/
  13. static const uint64_t UPDATE_INTERVAL = 1800000;
  14. int oldBatteryPcnt = 0;
  15. int oldValue = 0;
  16. float VBAT_PER_BITS = 0.0032551319648094;
  17. float VMIN = 1.9;
  18. float VMAX = 3.24;
  19. /**************************************************/
  20. /****************** VARIABLES *********************/
  21. /**************************************************/
  22. /**************************************************/
  23. /****************** MESSAGES **********************/
  24. /**************************************************/
  25. MyMessage msgDoor(CHILD_ID_DOOR,V_TRIPPED);
  26. MyMessage msgVBat(CHILD_ID_VOLTAGE,V_CUSTOM);
  27. void presentation()
  28. {
  29. present(CHILD_ID_DOOR, S_DOOR);
  30. }
  31. void setup()
  32. {
  33. // use the 1.1 V internal reference
  34. #if defined(__AVR_ATmega2560__)
  35. analogReference(INTERNAL1V1);
  36. #else
  37. analogReference(INTERNAL);
  38. #endif
  39. pinMode(DOOR_SENSOR_PIN,INPUT);
  40. }
  41. void loop()
  42. {
  43. sleep(digitalPinToInterrupt(DOOR_SENSOR_PIN),CHANGE, UPDATE_INTERVAL);
  44. motion_on();
  45. getBatteryLevel();
  46. }
  47. /**************************************************/
  48. /**************** AUX. FUNCTIONS ******************/
  49. /**************************************************/
  50. void getBatteryLevel()
  51. {
  52. // Battery monitoring reading
  53. int sensorValue = analogRead(BATTERY_SENSE_PIN);
  54. delay(500);
  55. // Calculate the battery in %
  56. float Vbat = sensorValue * VBAT_PER_BITS;
  57. send(msgVBat.set(Vbat, 3));
  58. int batteryPcnt = static_cast<int>(((Vbat - VMIN) / (VMAX - VMIN))*100.);
  59. if (batteryPcnt > 100) {
  60. batteryPcnt = 100;
  61. }
  62. sendBatteryLevel(batteryPcnt);
  63. }
  64. void motion_on()
  65. {
  66. bool tripped = digitalRead(DOOR_SENSOR_PIN) == LOW;
  67. send(msgDoor.set(tripped?"0":"1")); // Send tripped value to gw
  68. }