Skip to content

Commit

Permalink
Merge pull request #8 from heythisisnate/alarm
Browse files Browse the repository at this point in the history
implement Alarm capability and two-way communication
  • Loading branch information
Nate Clark authored Apr 8, 2017
2 parents 0f68369 + 599243e commit 509b147
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 35 deletions.
48 changes: 37 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This project will help you connect SmartThings to wired contact sensors (for doors and windows) and motion sensors that you may already have pre-wired in your home from a built-in home security system. There are three components to the project:

1. a SmartThings Device Hander for contact sensors and motion sensors
1. a SmartThings Device Handler for contact sensors and motion sensors
2. a SmartThings SmartApp that receives HTTP POST messages
3. Lua code for the NodeMCU device that connects your wired system to the cloud

Expand All @@ -16,20 +16,24 @@ The house I live in was built in the early 90s and came with a built-in home sec
1. A [basic breadboard](https://www.amazon.com/gp/product/B00WZSSDCW/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B00WZSSDCW&linkId=9d7c5cb3d9c1dde81a0789567f296c78) or [3-pack](https://www.amazon.com/gp/search/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&tag=heythisisnate-20&keywords=B01N12ZULY&index=aps&camp=1789&creative=9325&linkCode=xm2&linkId=ea15eee83d4897e13cfab03e3ffea1b3).
1. Some [extra wires of various male/female combinations](https://www.amazon.com/gp/product/B01FSGGJLY/ref=as_li_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01FSGGJLY&linkId=c23cd9573b73d437a52781fee10722e6).
1. A [microUSB power supply](https://www.amazon.com/gp/product/B00GF9T3I0/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B00GF9T3I0&linkId=72fc777b3abc63d2c2b86d1ff1343e18) or try one of these [other ways to power the device](http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/)
1. To connect your alarm siren or strobe light, you'll need a relay that the board can switch with a 3.3V signal. [These work well](http://amzn.to/2olmuNb) and are
only a few dollars [each](http://amzn.to/2obFako) (ship from China).

_Update:_ Later on I saw [this NodeMCU board with a base](https://www.amazon.com/gp/product/B01MDTKAR2/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01MDTKAR2&linkId=f5b8649950c46c0644e88dfbace7567d) that looks like it eliminates the need for a breadboard. And [this kit](https://www.amazon.com/gp/product/B01N3KO7QV/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01N3KO7QV&linkId=d4335e6ef6331d73be4fa83bf3cfc338) also includes 40 jumper cables making it an attractive all-in-one starter set.

_Update 2:_ One user reported that he had success with [this board](https://www.amazon.com/gp/product/B01N3P763C/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&tag=heythisisnate-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01N3P763C&linkId=f546e4e49606dfb8ec7bd89b0cadfeb4) which has 7 GPIO pins available for connecting sensors. With the board I bought and linked above, I could only get 4 or 5 pins working reliably. This may be a good option if you have more sensors and don't want to set up multiple devices.

### Some photos

| a | b | c |
| --- | --- | --- |
| ![](pics/20170129_104032_sm.jpg) | ![](pics/20170129_102807_sm.jpg) | ![](pics/Screenshot_20170129-233727.png) |
| I opened up the alarm panel and rerouted the wires for the sensors to the NodeMCU resting on top | Closeup of the NodeMcu | Devices in SmartThings |
### Annotated Photo

![](pics/20170405_094441.jpg)

## Updates

##### v1.5 / 2017-04-07
_Feature:_ Connect a wired siren and/or strobe. Integrates seamlessly with the Smart Home Monitor app.

**IMPORTANT: Read the [upgrade notes](https://github.com/heythisisnate/nodemcu-smartthings-sensors/releases/tag/1.5) if you're upgrading from an earlier version.**

##### v1.3 / 2017-03-29

_Feature:_ Blink the onboard LED on successful communication with SmartThings. To enable set `blink_led = true` in
Expand Down Expand Up @@ -68,6 +72,7 @@ Initial release.
* [NodeMCU Connected Contact Sensor](https://raw.githubusercontent.com/heythisisnate/nodemcu-smartthings-sensors/master/SmartThings/DeviceHandlers/nodemcu-connected-contact-sensor.groovy)
* [NodeMCU Connected Motion Sensor](https://raw.githubusercontent.com/heythisisnate/nodemcu-smartthings-sensors/master/SmartThings/DeviceHandlers/nodemcu-connected-motion-sensor.groovy)
* [NodeMCU Connected Smoke Detector](https://raw.githubusercontent.com/heythisisnate/nodemcu-smartthings-sensors/master/SmartThings/DeviceHandlers/nodemcu-connected-smoke-detector.groovy)
* [NodeMCU Connected Alarm](https://raw.githubusercontent.com/heythisisnate/nodemcu-smartthings-sensors/master/SmartThings/DeviceHandlers/nodemcu-connected-alarm.groovy)
1. Click Publish -> For Me
1. Repeat for the other device handler if you need both types

Expand All @@ -82,6 +87,11 @@ You'll need to create a device for each sensor that you plan on connecting. Repe
1. The Device Network Id doesn't seem to really matter, I just put a number.
1. Once you've created the device, make note of the device's URL. It will be something like `https://graph-na02-useast1.api.smartthings.com/device/show/22433333-1111-41dc-0000-00000000000`. The last part of the url is the DeviceId. Copy this DeviceId to the `variables.lua` file.

If you're connecting your alarm system siren or strobe, follow these steps:
1. Click New Device and fill out the form giving your siren a name like "Alarm"
1. In the Device Type dropdown, select _NodeMCU Connected Alarm_
1. Put any unique string in for Device Network Id. This will be overwritten later when you connect.

### 4. Create the SmartApp

The SmartApp receives data from your NodeMCU device, and updates the status of your devices in SmartThings.
Expand Down Expand Up @@ -149,19 +159,19 @@ on which board you have, there are different drivers:
* **Mac OS X Sierra users**: [use this driver](http://kig.re/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html)

#### Firmware
1. The [firmware](firmware/nodemcu-master-9-modules-2017-01-15-08-48-34-integer.bin) contained in this repo is a recent
build from https://nodemcu-build.com/ with following packages: `file`, `GPIO`, `HTTP`, `net`, `node`, `timer`, `UART`,
1. The [firmware](firmware/nodemcu-1.5.4.1-final-10-modules-2017-03-23-20-42-52-integer.bin) contained in this repo is a recent
build from https://nodemcu-build.com/ with following packages: `file`, `cjson`, `GPIO`, `HTTP`, `net`, `node`, `timer`, `UART`,
`WiFi` and `TLS/SSL support`. This firmware is on SDK version 1.5.4.1. Between the time that I did this project and wrote
up this README, the NodeMCU firmware team has released a 2.0.0 firmware which has a bug with SSL/TLS and this program
doesn't work. Until this is fixed, please use the 1.5.4.1-final branch if you're building your own firmware at https://nodemcu-build.com/.
doesn't work. Until this is fixed, you must use the 1.5.4.1-final branch if you're building your own firmware at https://nodemcu-build.com/.
More info in https://github.com/nodemcu/nodemcu-firmware/issues/1707.

1. I used [esptool.py](https://github.com/espressif/esptool) to flash the firmware (I'm using a Mac). There's pretty
good [documentation here](https://nodemcu.readthedocs.io/en/master/en/flash/) including a couple other options for Windows users.

1. The exact command I used to flash the firmware is:

`esptool.py --port=/dev/cu.SLAB_USBtoUART write_flash --flash_mode dio 0x00000 firmware/nodemcu-master-9-modules-2017-01-15-08-48-34-integer.bin`
`esptool.py --port=/dev/cu.SLAB_USBtoUART write_flash --flash_mode dio 0x00000 firmware/nodemcu-1.5.4.1-final-10-modules-2017-03-23-20-42-52-integer.bin`

Your port may vary depending on your platform, OS and UART driver.

Expand All @@ -186,6 +196,22 @@ good [documentation here](https://nodemcu.readthedocs.io/en/master/en/flash/) in
1. Now go around your house testing it out!
1. Once everything is working properly, you can plug the NodeMCU into a standard USB power adapter and it will automatically boot up, connect to WiFi, and start listening for switches.

### 8. Connect your siren and/or strobe (optional)

The SmartThings _Alarm_ capability supports both a siren and a strobe state, so you can connect up to two wired devices
to be controlled independently (they don't necessarily have to be a siren or a strobe). This is designed to integrate
seamlessly with the Smart Home Monitor app and the _Alert with Sirens_ function. My alarm system has a siren that activates
with a 12V current from the alarm panel. You need a [relay](http://amzn.to/2obFako) to switch the 12V power on with a 3.3V
signal from the ESP8266 board. These instructions are written for the relay linked above, but should be similar for other relays.

1. Connect the GND on the relay to the ground (-) on the ESP8266 board.
1. Connect the VCC on the relay to a 3V3 output pin on the board.
1. Connect the IN on the relay to the configured GPIO pin on the board, D1 for example.
1. Connect the NO (Normally Open) on the relay to the red (+) wire going to your siren or strobe.
1. Make sure the black (-) wire of the siren/strobe is connected to the ground (-) on the alarm panel.
1. Connect the COM on the relay to the 12V aux power out (+) on the alarm panel with a jumper wire.


## Problems or Questions

Please [open an issue](https://github.com/heythisisnate/nodemcu-smartthings-sensors/issues) if you run into problems or have feature requests. You can also [join the discussion on SmartThings community](https://community.smartthings.com/t/connect-wired-alarm-system-sensors-to-smartthings-with-a-nodemcu-esp8266/76010)
Expand Down
113 changes: 113 additions & 0 deletions SmartThings/DeviceHandlers/nodemcu-connected-alarm.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* NodeMCU Cloud Connected Alarm
*
* Copyright 2017 Nate Clark | @heythisisnate
*
* A description of this project and documentaion can be found at:
* https://github.com/heythisisnate/nodemcu-smartthings-sensors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/

metadata {
definition (name: "NodeMCU Connected Alarm", namespace: "heythisisnate", author: "Nate Clark") {
capability "Alarm"
command "sync"
}

tiles {
standardTile("alarm", "device.alarm", decoration: "flat") {
state "off", label:'${name}', action:"alarm.both", icon:"st.alarm.alarm.alarm"
state "siren", label:'${name} on', action:"alarm.off", icon:"st.alarm.alarm.alarm", backgroundColor: "#ffa81e"
state "strobe", label:'${name} on', action:"alarm.off", icon:"st.alarm.alarm.alarm", backgroundColor: "#ffa81e"
state "both", label:'ALARM!', action:"alarm.off", icon:"st.alarm.alarm.alarm", backgroundColor: "#e86d13"
}
standardTile("siren", "device.alarm", inactiveLabel: false, decoration: "flat") {
state "default", label:'', action:"alarm.siren", icon:"st.secondary.siren"
state "siren", label:'', action:"alarm.off", icon:"st.secondary.siren", backgroundColor: "#ffa81e"
}
standardTile("strobe", "device.alarm", inactiveLabel: false, decoration: "flat") {
state "default", label:'', action:"alarm.strobe", icon:"st.secondary.strobe"
state "strobe", label:'', action:"alarm.off", icon:"st.secondary.strobe", backgroundColor: "#ffa81e"
}
}
main "alarm"
details "alarm", "siren", "strobe"
}

def both() {
httpAction("both")
}

def siren() {
httpAction("siren")
}

def strobe() {
httpAction("strobe")
}

def off() {
httpAction("off")
}

// parse events into attributes
def parse(String description) {
def msg = parseLanMessage(description)
createEvent(name: "alarm", value: msg.json?.alarm)
}

def sync(ip, port, mac) {
def existingIp = getDataValue("ip")
def existingPort = getDataValue("port")
def existingMac = getDataValue("mac")
def updated = false
if (ip && ip != existingIp) {
updateDataValue("ip", ip)
updated = true
}
if (port && port != existingPort) {
updateDataValue("port", port)
updated = true
}
if (mac && mac != existingMac) {
updateDataValue("mac", mac)
updated = true
}
if (updated) {
device.deviceNetworkId = ipToHex(ip, port)
}
}

// accepts an IP and PORT string and converts it to a hex identifier
// for setting the SmartThings deviceNetworkId
private String ipToHex(String address, String port) {
def octets = address.tokenize('.')
def hex = ""

octets.each {
hex = hex + Integer.toHexString(it as Integer).toUpperCase()
}
hex = hex + ":" + Integer.toHexString(port as Integer).toUpperCase().padLeft(4,'0')
return hex
}

private httpAction(action) {
def result = new physicalgraph.device.HubAction(
method: "POST",
path: "/" + device.id + "/" + action,
headers: [
HOST: getDataValue("ip") + ":" + getDataValue("port")
]
)
log.debug result
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright 2017 Nate Clark | @heythisisnate
*
* A desccription of this project and documentaion can be found at:
* A description of this project and documentation can be found at:
* https://github.com/heythisisnate/nodemcu-smartthings-sensors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -42,4 +42,4 @@ def open() {

def close() {
sendEvent(name: "contact", value: "closed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright 2017 Nate Clark | @heythisisnate
*
* A desccription of this project and documentaion can be found at:
* A description of this project and documentation can be found at:
* https://github.com/heythisisnate/nodemcu-smartthings-sensors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -42,4 +42,4 @@ def open() {

def close() {
sendEvent(name: "motion", value: "inactive")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* NodeMCU Cloud Connected Smoke Detector
*
* Copyright 2017 Nate Clark | @heythisisnate
*
* A desccription of this project and documentaion can be found at:
*
* A description of this project and documentation can be found at:
* https://github.com/heythisisnate/nodemcu-smartthings-sensors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,7 +16,7 @@
* for the specific language governing permissions and limitations under the License.
*
*/

metadata {
definition (name: "NodeMCU Connected Smoke Detector", namespace: "heythisisnate", author: "Nate") {
capability "Smoke Detector"
Expand All @@ -41,4 +41,4 @@ def open() {

def close() {
sendEvent(name: "smoke", value: "clear")
}
}
18 changes: 17 additions & 1 deletion SmartThings/cloud-sensor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ preferences {
input "contactSensors", "capability.contactSensor", title: "Contact sensors", multiple:true, required:false
input "motionSensors", "capability.motionSensor", title: "Motion sensors", multiple:true, required:false
input "smokeDetectors", "capability.smokeDetector", title: "Smoke detectors", multiple:true, required:false
}
input "alarm", "capability.alarm", title: "Alarm", required:false
}
}

mappings {
Expand All @@ -41,6 +42,12 @@ mappings {
POST: "handle_event"
]
}

path("/sync") {
action: [
POST: "sync"
]
}
}

def handle_event() {
Expand All @@ -64,3 +71,12 @@ def handle_event() {

return [ "success": true ]
}

def sync() {
def sync_data = request.JSON
if (sync_data.device_id == alarm.id) {
alarm.sync(sync_data.ip, sync_data.port as String, sync_data.mac)
}
return [ "success": true ]
}

Binary file not shown.
Loading

0 comments on commit 509b147

Please sign in to comment.