[{"element_type":"markdown","content":"\u003ch2\u003eIntroduction\u003c/h2\u003e\n\n\u003cp\u003eI got burned buying into the Google security ecosystem a while back and promised myself to never do it again. Plus now I know exactly what all the pieces of our home monitoring system are doing and where they're communicating. Easy upgrading too!\u003c/p\u003e\n\n\u003cp\u003eAll other sensors will use the same code base and construction.\u003c/p\u003e\n\n\u003ch2\u003eDesign\u003c/h2\u003e\n\n\u003cp\u003eThe sensor is coded in CircuitPython\u003c/p\u003e\n\n\u003cp\u003eThe current case is the \u003ca href=\"https://www.thingiverse.com/thing:5196253\"\u003esnap fit case\u003c/a\u003e provided by team Adafruit. There will be a smaller case for the sensors mounted on doors and windows. \u003c/p\u003e\n\n\u003cp\u003eYou will need an MQTT broker for all of this to work together. You can use Adafruit IO.\u003c/p\u003e\n\n\u003cp\u003eIn my implementation I set up a Mosquitto MQTT broker on a RaspberryPi 4 Model B running Debian Bookworm and Mosquitto v2.022. \u003c/p\u003e\n\n\u003ch5\u003eThe first sensor built and put into use is for the garage door\u003c/h5\u003e\n\n\u003cp\u003eOur house has a detached garage. The opening for the garage is perpendicular to the house which is good for esthetics and access, bad for knowing if the door is open.\u003c/p\u003e\n\n\u003ch2\u003eParts\u003c/h2\u003e\n","metadata":{"markdown":"## Introduction\n\nI got burned buying into the Google security ecosystem a while back and promised myself to never do it again. Plus now I know exactly what all the pieces of our home monitoring system are doing and where they're communicating. Easy upgrading too!\n\nAll other sensors will use the same code base and construction.\n\n## Design\n\nThe sensor is coded in CircuitPython\n\nThe current case is the [snap fit case](https://www.thingiverse.com/thing:5196253) provided by team Adafruit. There will be a smaller case for the sensors mounted on doors and windows. \n\nYou will need an MQTT broker for all of this to work together. You can use Adafruit IO.\n\nIn my implementation I set up a Mosquitto MQTT broker on a RaspberryPi 4 Model B running Debian Bookworm and Mosquitto v2.022. \n\n##### The first sensor built and put into use is for the garage door\nOur house has a detached garage. The opening for the garage is perpendicular to the house which is good for esthetics and access, bad for knowing if the door is open.\n\n## Parts"}},{"element_type":"product","content":"https://www.adafruit.com/product/5395","metadata":{"name":"QT PY","url":"https://www.adafruit.com/product/5395","description":"QT PY ESP32 Pico","quantity":"1","featured":false}},{"element_type":"product","content":"https://www.adafruit.com/product/375","metadata":{"name":"Door Sensor","url":"https://www.adafruit.com/product/375","description":"Magnetic Door Sensor","quantity":"1","featured":false}},{"element_type":"parts","content":"\n  \u003cdiv class=\"parts-details\"\u003e\n    \u003cdiv\u003e\n      \u003cspan class=\"parts-quantity\"\u003e1\u003c/span\u003e\u003cspan\u003e x \u003c/span\u003e\n        \u003ca href=\"https://www.amazon.com/California-JOS-Breadboard-Optional-Multicolored/dp/B0BRTHR2RL/ref=sr_1_3?sr=8-3\" class=\"parts-name\" target=\"_blank\"\u003eBreadboard Jumper Wires (optional)\u003c/a\u003e\n      \u003cdiv class=\"parts-description\"\u003eBreadboard jumper wires\u003c/div\u003e\n    \u003c/div\u003e\n  \u003c/div\u003e\n  \u003cdiv class=\"parts-action\"\u003e\n  \u003cdiv\u003e\n      \u003ca href=\"https://www.amazon.com/California-JOS-Breadboard-Optional-Multicolored/dp/B0BRTHR2RL/ref=sr_1_3?sr=8-3\" class=\"parts-url btn-primary\" target=\"_blank\"\u003eBuy Now\u003c/a\u003e\n\n  \u003c/div\u003e\n\u003c/div\u003e\n\n  \u003cdiv class=\"clearfix\"\u003e\u003c/div\u003e\n","metadata":{"name":"Breadboard Jumper Wires (optional)","url":"https://www.amazon.com/California-JOS-Breadboard-Optional-Multicolored/dp/B0BRTHR2RL/ref=sr_1_3?sr=8-3","description":"Breadboard jumper wires","quantity":"1"}},{"element_type":"markdown","content":"\u003ch2\u003eCode\u003c/h2\u003e\n\n\u003ch4\u003eSensor\u003c/h4\u003e\n\n\u003cp\u003e\u003ca href=\"https://github.com/niccit/disturbance-in-force/blob/main/garage_sensor.py\"\u003eThe code for this project\u003c/a\u003e can be found on Github.\u003c/p\u003e\n\n\u003cp\u003eIt is one part of the larger \u003ca href=\"https://github.com/niccit/disturbance-in-force\"\u003ehome monitoring project\u003c/a\u003e.\u003c/p\u003e\n\n\u003ch4\u003eThe following libraries are required:\u003c/h4\u003e\n\n\u003cul\u003e\n\u003cli\u003eos\u003c/li\u003e\n\u003cli\u003etime\u003c/li\u003e\n\u003cli\u003eboard\u003c/li\u003e\n\u003cli\u003edigitalio\u003c/li\u003e\n\u003cli\u003ewifi\u003c/li\u003e\n\u003cli\u003eadafruit debouncer \u003c/li\u003e\n\u003cli\u003eadafruit minimqtt\u003c/li\u003e\n\u003cli\u003eadafruit minimqtt\u003c/li\u003e\n\u003cli\u003eadafruit connection_manager\u003c/li\u003e\n\u003cli\u003eadafruit logging\u003c/li\u003e\n\u003c/ul\u003e\n\n\u003ch5\u003eFeature Highlights\u003c/h5\u003e\n\n\u003cul\u003e\n\u003cli\u003eWhen the door is open the sensor publishes a 1 to monitoring.garage-sensor\u003c/li\u003e\n\u003c/ul\u003e\n\n\u003ch4\u003eNode-Red subscribes and will\u003c/h4\u003e\n\n\u003cul\u003e\n\u003cli\u003eChange the indicator on the sensor page; red - open, green - closed\u003c/li\u003e\n\u003cli\u003eChange the indicator on the home page; red - open, green - closed. Since we often reference if the door is open or closed, having an indicator on the home page saves having to switch tabs every time we want to know the state of the door\u003c/li\u003e\n\u003c/ul\u003e\n\n\u003ch2\u003eNode-Red\u003c/h2\u003e\n\n\u003cp\u003eI have a \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/149/large1024/sensors_flow_rz.png?1767747179\"\u003eflow\u003c/a\u003e dedicated to handling sensor data.\u003c/p\u003e\n\n\u003ch4\u003eConfiguration\u003c/h4\u003e\n\n\u003cp\u003eAdd an MQTT in node and configure it to subscribe to the topic for the sensor. For me it's monitoring.garage-sensor.\u003c/p\u003e\n\n\u003cp\u003eAfter that you'll need to connect it to a \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/181/large1024/garage_door_ui-led1.png?1768001303\"\u003eUI LED node\u003c/a\u003e that will take action based on the message payload. \u003c/p\u003e\n\n\u003cp\u003eI have three functions in my flow, two are disabled and will be used when traveling. The active function will generate an email when it's past a certain hour and the garage door is still open. \u003c/p\u003e\n\n\u003cp\u003eTo build the function nodes, you will need some familiarity with Javascript. Word of caution, there are some differences between Javascript and Javascript within Node-Red. I had to narrow a few searches to get some of the answers I initially needed.\u003c/p\u003e\n\n\u003cp\u003eThe code in the function for sending a notification looks like this:\u003c/p\u003e\n\u003ccode class=\"\"\u003e\u003cpre\u003econtext.data = context.data || {};\nswitch (msg.topic) {\n    case \"monitoring.garage-sensor\":\n        context.data.garage = msg.payload;\n        msg = null;\n        break;\n    case \"home.hour\":\n        context.data.hour = msg.payload;\n        msg = null;\n        break;\n    default:\n        msg = null;\n        break;\n}\nif (context.data.garage != null \u0026amp;\u0026amp; context.data.hour != null) {\n    if (context.data.hour \u0026gt;= 20 \u0026amp;\u0026amp; context.data.garage == 1)\n    {\n        var newMsg = {\n        \"topic\": \"Your Attention Needed\"\n        ,\"payload\": \"Garage door is open\"\n        }\n    }\n}   \nreturn newMsg\n\u003c/pre\u003e\u003c/code\u003e\n\u003cp\u003eTo receive email, you'll need to have an MTA. I have a secondary Gmail account that I'm using to send mail.\u003c/p\u003e\n\n\u003ch2\u003eProject Construction\u003c/h2\u003e\n\n\u003ch4\u003eCircuit Board\u003c/h4\u003e\n\n\u003cul\u003e\n\u003cli\u003eSolder \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/182/large1024/qtpy1.png?1768004014\"\u003ebreadboard jumper\u003c/a\u003e wires to the earth and data pins. \u003c/li\u003e\n\u003cli\u003eConnect \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/183/large1024/qtpy2.png?1768004044\"\u003eone side of contact switch\u003c/a\u003e to the jumper wires\u003c/li\u003e\n\u003cli\u003eUsing the snap fit case, secure the QT PY, ensure the jumper wires are hanging outside the case, and close the case\u003c/li\u003e\n\u003c/ul\u003e\n\n\u003ch4\u003eInstallation in the Garage\u003c/h4\u003e\n\n\u003cp\u003eI used Uglu Dashes to secure the magnets to the side of the \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/185/large1024/qtpy4.png?1768004094\"\u003egarage door\u003c/a\u003e and to the \u003ca href=\"https://cdn-learn.adafruit.com/user_assets/assets/000/002/184/large1024/qtpy3.png?1768004071\"\u003eframe\u003c/a\u003e. It's been there now for over 6 months without issue. I did clean both areas with rubbing alcohol before applying the Uglu.\u003c/p\u003e\n","metadata":{"markdown":"## Code\n\n#### Sensor\n[The code for this project](https://github.com/niccit/disturbance-in-force/blob/main/garage_sensor.py) can be found on Github.\n\nIt is one part of the larger [home monitoring project](https://github.com/niccit/disturbance-in-force).\n\n#### The following libraries are required:\n* os\n* time\n* board\n* digitalio\n* wifi\n* adafruit debouncer \n* adafruit minimqtt\n* adafruit minimqtt\n* adafruit connection_manager\n* adafruit logging\n\n##### Feature Highlights\n* When the door is open the sensor publishes a 1 to monitoring.garage-sensor\n\n#### Node-Red subscribes and will\n* Change the indicator on the sensor page; red - open, green - closed\n* Change the indicator on the home page; red - open, green - closed. Since we often reference if the door is open or closed, having an indicator on the home page saves having to switch tabs every time we want to know the state of the door\n\n## Node-Red\nI have a [flow](https://cdn-learn.adafruit.com/user_assets/assets/000/002/149/large1024/sensors_flow_rz.png?1767747179) dedicated to handling sensor data.\n\n#### Configuration\nAdd an MQTT in node and configure it to subscribe to the topic for the sensor. For me it's monitoring.garage-sensor.\n\nAfter that you'll need to connect it to a [UI LED node](https://cdn-learn.adafruit.com/user_assets/assets/000/002/181/large1024/garage_door_ui-led1.png?1768001303) that will take action based on the message payload. \n\nI have three functions in my flow, two are disabled and will be used when traveling. The active function will generate an email when it's past a certain hour and the garage door is still open. \n\nTo build the function nodes, you will need some familiarity with Javascript. Word of caution, there are some differences between Javascript and Javascript within Node-Red. I had to narrow a few searches to get some of the answers I initially needed.\n\nThe code in the function for sending a notification looks like this:\n```\ncontext.data = context.data || {};\nswitch (msg.topic) {\n    case \"monitoring.garage-sensor\":\n        context.data.garage = msg.payload;\n        msg = null;\n        break;\n    case \"home.hour\":\n        context.data.hour = msg.payload;\n        msg = null;\n        break;\n    default:\n        msg = null;\n        break;\n}\nif (context.data.garage != null \u0026amp;\u0026amp; context.data.hour != null) {\n    if (context.data.hour \u0026gt;= 20 \u0026amp;\u0026amp; context.data.garage == 1)\n    {\n        var newMsg = {\n        \"topic\": \"Your Attention Needed\"\n        ,\"payload\": \"Garage door is open\"\n        }\n    }\n}   \nreturn newMsg\n```\nTo receive email, you'll need to have an MTA. I have a secondary Gmail account that I'm using to send mail.\n\n## Project Construction\n\n#### Circuit Board\n* Solder [breadboard jumper](https://cdn-learn.adafruit.com/user_assets/assets/000/002/182/large1024/qtpy1.png?1768004014) wires to the earth and data pins. \n* Connect [one side of contact switch](https://cdn-learn.adafruit.com/user_assets/assets/000/002/183/large1024/qtpy2.png?1768004044) to the jumper wires\n* Using the snap fit case, secure the QT PY, ensure the jumper wires are hanging outside the case, and close the case\n\n#### Installation in the Garage\nI used Uglu Dashes to secure the magnets to the side of the [garage door](https://cdn-learn.adafruit.com/user_assets/assets/000/002/185/large1024/qtpy4.png?1768004094) and to the [frame](https://cdn-learn.adafruit.com/user_assets/assets/000/002/184/large1024/qtpy3.png?1768004071). It's been there now for over 6 months without issue. I did clean both areas with rubbing alcohol before applying the Uglu.\n\n\n\n"}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/149/original/sensors_flow_rz.png?1767747179","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/181/original/garage_door_ui-led1.png?1768001303","metadata":{}},{"element_type":"markdown","content":"\u003cp\u003eThis is part of the larger \u003ca href=\"https://adafruit-playground.com/u/ntynen/pages/home-hub-project-with-node-red-dashboard\"\u003eHome Hub Project with Node-Red\u003c/a\u003e\u003c/p\u003e\n","metadata":{"markdown":"This is part of the larger [Home Hub Project with Node-Red](https://adafruit-playground.com/u/ntynen/pages/home-hub-project-with-node-red-dashboard)"}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/182/original/qtpy1.png?1768004014","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/183/original/qtpy2.png?1768004044","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/184/original/qtpy3.png?1768004071","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/002/185/original/qtpy4.png?1768004094","metadata":{}}]