|
Post by papa on Oct 17, 2022 21:45:11 GMT
ESP32 Node for Door, Gate, Window, Mailbox... Switch Sensor NodeVarious switches can be effective sensors for the states of things like doors (garage & people), windows, gates, mailboxes, etc. Such sensors include reed switches, tilt switches, & even dupont cable jumpers between an ESP32 I/O pin & a GND pin. See this thread & this thread for the latest program code, initial customization, examples of node customization, & related openHAB configuration. A switch sensor node might have one, two, or three switches to monitor one or more doors &/or windows &/or mailbox features. The program code uses ESP32 pins 13, 14, & 15. One could change the code to use other pins as long as those pins are available ( see here). The code applies a pullup resistor to the pins (avoids a physical resistor) to give the pins a reliable state of one/HIGH. Then the code detects when the switch closes & changes the pins' state to zero/LOW. Via MQTT, a switch node can send a switch's state to openHAB. OpenHAB can display the state (e.g. HABPanel) & take action based on the state (e.g. rules). See the following wiring diagram for one to three switches. << Forum members, click on pic for larger view. Next, Installing a Switch Sensor for a Door's Deadbolt
|
|
|
Post by papa on Oct 18, 2022 15:56:27 GMT
ESP32 Node with Reed Switch for Window, Gate or Garage Door:
This method senses when a window or gate is TOTALLY closed or (even partly) open. For installing the switch sensors, besides the schematic above, see the following diagram & picture: << Forum members, click on pic for larger view For wiring, I suggest you use insulated 24 gauge wire which can be hidden under trim, etc.
Sensing the status of the window or gate: A strong magnet is mounted on the edge of the window or gate. A magnet could be attached (magnetically, glue) to the steel cable pin at the bottom corner of a garage door.
Opposite the magnet at the closed position, mount a (normally open) reed switch to be near, but not touching the magnet. A magnet near the reed switch closes the switch, connecting ESP32 pin 14 to GND, making the pin LOW which will help signal OpenHAB that the window or gate is totally closed. When the magnet & reed switch are away from each other, a software pull-up holds ESP32 14 HIGH, indicating the window or gate is at least partly open. With customization, the software code allows one to use one to three of the ESP32 pins 13-15. Other available ESP32 pins could be used. A node can monitor more than one switch.
Next, ESP32 Node for Deadbolt Lock
|
|
|
Post by papa on Oct 18, 2022 18:27:51 GMT
ESP32 Node for Dead Bolt LockThis method senses when a garage's vehicle door is TOTALLY closed or (even partly) open & when a people door's deadbolt is closed or open. For installing the switch sensors, besides the schematic above, see the following diagram & picture: << Click on pic for larger view
<< Click on pic for larger view ^^ Looking into the dead bolt receiving hole Sensing the status of the people entrance door: My people door dead bolt is metal. With steel wool, I polished the end for best conductivity. Across the bottom of the dead bolt receiving hole, I installed two bared ends of 24 gauge insulated wire so the bottom of the hole was divided into horizontal (unequal) thirds. Using a 16 finishing nail as a drill bit, I drilled two holes that went about level from the drywall right next to the door frame to the inside edge of the end of the dead bolt hole. Level & opposite to the first two holes, I made two more tiny holes to hold tightly the end of the bared wires. The insulated part of each wire goes through from the dead bolt hole to the side of the door frame & to its ESP32 connection. The bared part of each wire (with separation between wires) goes across the (padded?) bottom of the dead bolt hole & is anchored in its tight hole. As I did, you may have to put folded cardboard (or other insulating material) under both bared wires so a fully extended dead bolt touches them. Thread the longer insulated parts of the wires through the side of the dead bolt hole toward the ESP32. There one bared end goes to ESP32 GND & the other bared end to ESP32 pin 13. As necessary, conceal the wires next to or beneath door trim pieces. When the people door is NOT dead bolted, an software pull-up holds pin 13 HIGH. When the dead bolt fully extends & touches the two bare wires, it functionally closes a switch, connecting 13 to GND & making it LOW. The node sends this data to OpenHAB via MQTT.
Next, ESP32 Node with More Than One Switch
|
|
|
Post by papa on Oct 18, 2022 18:35:07 GMT
ESP32 Node with More Than One Switch
An ESP32 Node can monitor more than one switch. See the following example of monitoring both a reed switch (for a garage door) AND a dead bolt switch for a people door.
In this case, ESP32 pins 13 & 14 are used.
<< Click on pic for larger view
Next, ESP32 Node Software for Switches
|
|
|
Post by papa on Oct 18, 2022 19:51:32 GMT
ESP32 Node Software for SwitchesSee this thread & this thread for the latest program code, initial customization, examples of node customization, & related openHAB configuration.
Node Specific Customization:
Remember that each WiFi node must have a unique nodeId.
Deactivate node #defines left over from a previous node.
Find the 3 lines: // #define SWITCH1pin 13 // ... // #define SWITCH2pin 14 // ... // #define SWITCH3pin 15 // ...
Edit those lines to match how you wired your node. For every switch you use, activate its #define (delete // at the start). If you change the pin a node switch uses from 13, 14, or 15, change the #define accordingly. For example, if you wire a switch to pin 13, change // #define SWITCH1pin 13 to #define SWITCH1pin 13 [no //]
To test the node, wire a male to male dupont cable from the switch's pin (13 for example) to an ESP32 GND
Upload to a USB connected node.
Next, Serial Monitor Results for ESP32 Node with Switch
|
|
|
Post by papa on Oct 18, 2022 20:10:37 GMT
Serial Monitor Results for ESP32 Node with SwitchThis is only the results that relates directly to a switch.
If SWITCH1pin is activated on node 36: [Your node ID may differ]
topic home/esp_gw/nb/node36/dev43 value:ON [switch closed, ?? dupont cable connected on both ends] topic home/esp_gw/nb/node36/dev43 value:OFF [switch open, perhaps when dupont cable pulled from ESP32 GND If SWITCH2pin is activated on node 36: topic home/esp_gw/nb/node36/dev44 value:ON [switch closed] topic home/esp_gw/nb/node36/dev44 value:OFF [switch open, perhaps when dupont cable pulled from ESP32 GND If SWITCH3pin is activated on node 36: topic home/esp_gw/nb/node36/dev45 value:ON [switch closed] topic home/esp_gw/nb/node36/dev45 value:OFF [switch open, perhaps when dupont cable pulled from ESP32 GND
Next, Configure OpenHAB for Node SWITCHes
|
|
|
Post by papa on Oct 19, 2022 15:12:48 GMT
Configure OpenHAB for Node SWITCHes Prepare to Transform Node's SWITCH1 Message
(For SWITCH2 or SWITCH3, adapt the following posts.)
From Serial Monitor results above, we see that channel 43 (SWITCH1) sends the MQTT message of ON or OFF. We'll use an openHAB feature to transform ON to CLOSED & OFF to OPEN.
First in openHAB's transform folder (Linux /etc/openhab/transform), create a door.map file with the contents:
ON=CLOSED OFF=OPEN
Next, Create OpenHAB Channel for SWITCH1
|
|
|
Post by papa on Oct 19, 2022 15:36:07 GMT
Create Node36 Channel for a Node's Device 43 (SWITCH1) [Your node ID & device may differ.]I'm adding a channel to a previously created Thing Node36. To create a different Thing, see here. See this post. We're making a channel for the MQTT topic home/esp_gw/nb/node36/dev43Logged in to your openHAB configuration, go to localhost:8080/settings/things/In the listed Things, click Node36. Then click the [channels tab] near the top. Near the bottom, click Add Channel. In the Add Channel Window, fill the fields with configs: Channel identifier Node36Channel43 ^^ arbitrary ? as long as we match it in a linked Item later (see below) Label Node36_Door ^^ arbitrary ?, make it useful for yourself, but not too long Channel type Text Value ^^ choose type compatible with the device/channel data type MQTT State Topic home/esp_gw/nb/node36/dev43MQTT Command Topic [leave blank] Click "Show Advanced" & at Transform values/Incoming value transformations, enter MAP:door.map [<< This will use the transform file we created earlier.] Outgoing Value Format, enter %s
Click [Create] to save your configs Next: Create Item to Link to Node36's Channel 43
|
|
|
Post by papa on Oct 19, 2022 15:52:02 GMT
Create Item to Link to Node36's Channel 43, My Preference: Text Config File
Remember: Use only one method to create & edit an Item (Dashboard OR .items file) NOT BOTH. This method both creates the Item AND links it to the Node36 Thing's Channel 43. [Your node & Thing may differ.]
In openHAB's items folder (Linux /etc/openhab/items), have an .items file to hold the Item. Mine is called ESP32.items, but the name before the .items tag is up to you. The channel 43 Item takes the form: [ Your Item's Name & Label could differ.] String Name "Label [%s]" { channel=" Node36channelUID" } Go to localhost:8080/settings/things/ Click on the listed Node36 Thing Click on the [ channels] tab After the Node36_RSSI channel is a small stacked pages icon. Click on that icon to copy the channel UID Into the Item, paste that channel UID in place of Node36channelUID color-coded above. For example, my channel's UID is mqtt:topic:c08f02ca8d:3f938be488:Node36Channel43
So my Item (your channel UID for the Item will be different) is String nDoor "northDoor36 [%s]" { channel="mqtt:topic:c08f02ca8d:3f938be488:Node36Channel43" }
For display purposes only, go to localhost:8080/settings/items/nDoor Look at the field just below the top brightly-colored rectangle. If our Node is programmed & powered, that field should display the Node36's SWITCH1 state, e.g. OPEN or CLOSED.
Next: Display Node36's SWITCH1 Data on HABPanel
|
|
|
Post by papa on Oct 19, 2022 19:03:34 GMT
Display a Node's SWITCH1 Data on HABPanelGo to the browser address localhost:8080. At the upper right are two small icons, a pencil & a "page." Hovering the mouse pointer over the pencil shows "Edit Home Page." Over the "page," shows "Other apps." Click on the "Other apps" icon & then click on HABPanel. The browser address becomes " http://localhost:8080/habpanel/" The first time you use HABPanel, click the gear icon at the upper right. At the middle left, click on "Add new dashboard." For the new dashboard, enter a Name, perhaps "Main" & click OK.
(I assume Node36 & a dashboard named "Main." Yours may differ.)
Hover the mouse pointer over "Main" at the upper left. Click on the small pencil icon (edit dashboard) that appears. At the upper right, click on the [Add Widget] button. In Add Widget, choose Dummy (useful for text).
In the new widget window at the upper right, click the 3 stacked dots. Choose Edit. Enter a Name, perhaps nDoor36. In the openHAB Item field, choose the desired Item. You can click the drop down icon & scroll down to the Item named nDoor. OR you can enter nDoor in the search box. Either way, click on the nDoor Item listed. Scroll to the bottom of the Edit Widget Window & click [ Save]. Click the [ Run] button to see "nDoor36" in the Main dashboard. If the node is programmed & powered & a jumper connects ESP32 pin 13 & GND pin, "CLOSED" should be displayed on the widget. If the jumper does not connect the pins, "OPEN" should be displayed. Other Uses of SWITCH1 States
|
|
|
Post by papa on Oct 19, 2022 19:55:40 GMT
Other Uses for SWITCH1 (& Other) StatesVia rules, openHAB can be configured to act based on states reported from nodes. In openHAB's rules folder (Linux /etc/openhab/items), you could have a .rules text file, for example ESP32.rules. (See here.) The start of some textual rules: "ruleOne"
when Item nDoor received update then [ action ] end "ruleTwo"
when Item nDoor changed from CLOSED to OPEN then [ action ] end
----------------------------------------------- Or you could go to http://localhost:8080/settings/rules/ & use the dashboard to create rules. (See here.) ==============================
If WiFi will connect reliably to it, a ESP32 node might monitor a mailbox. Via SWITCH1 (or ...2 or ...3), a tilt switch could signal change in a mailbox door &/or flag.
|
|