|
Post by papa on Oct 22, 2022 17:36:31 GMT
ESP32 Node + PIR Motion Sensor Needed besides the ESP32 board: Sensor such as Mini PIR motion sensor (AM312) or PIR motion sensor (HC-SR501) Ways to connect ESP32 pins to the sensor's 3 male pins. I have female headers on my ESP32 board so I use (color-coded) male to female dupont cables. Connect as follows: ESP32 | AM312 Sensor
| HC-SR501 | 3.3 v
| power pin
| not used
| 5 v
| not used
| 5v pin
| 26 ??
| Data | Data
| GND | GND | GND |
The provided software sketch uses ESP32 pin 26 for sensor data. You can change that, but avoid ESP32 pins 6-11 Next, Software & Initial Customization
|
|
|
Post by papa on Oct 22, 2022 17:46:52 GMT
ESP32 PIR Node Software, Initial & Node Customization Use this thread for the latest software & initial customization. Software Customization for PIR Node: In the software sketch, deactivate #define lines from previous nodes (have // at line's start). Change the nodeId if you have another node with that ID. In the software sketch, find & activate these #define lines (delete // at the start):
// #define PIR // activates code for PIR presence / motion sensor // #define PIR_PIN 26 // ESP32 pin for PIR data pin, avoid 6-11 [^^ change if you don't use pin 26] If you want to use an OLED display with the PIR ... in the software sketch, find & activate these #define lines (delete // at the start): // #define ENABLE_SSD1306 // #define I2C_ SDA 5 // #define I2C_ SCL 4 If you connect SDA & SDL to different pins, change the 2nd & 3rd lines accordingly. Upload the customized software to the ESP32 board. Next, Expected results
|
|
|
Post by papa on Oct 23, 2022 18:55:18 GMT
ESP32 PIR Node on HiLetgo OLED ESP32, Expected Results When the upload finishes, open the Arduino IDE Serial Monitor (SM). At the SM's lower right, make sure the SM's baud matches that in the sketch line #define SERIAL_BAUD 115200. It may help to press the ESP32 board's reset button. Then the SM will display boot info from the board. A little after the node boots & connects to WiFi/MQTT, move your hand in front of the PIR sensor. Then remove your hand. Note the SM reports of PIR states.
The SM should show typical Node output (my notes in [ ] ) : Node 36 Version ESP32_nodes_nxxx MQCON 12 PIR 26 [Node ID #, code version, MQTT indicator is on pin 12, PIR on pin 26] Connecting to XXX [connecting to WiFi station named in sketch] .... [several dots may show while WiFi is connecting] WiFi connected IP address: 192.168.10.140 [Your IP will differ.]
Connect to MQTT broker...connected topic home/esp_gw/nb/node36/dev99 value:NODE 35 WAKEUP: ESP_36 [wakeup reported via MQTT broker]
topic home/esp_gw/nb/node36/dev02 value:-29 [Radio Signal strength device 2, closer to zero the better] topic home/esp_gw/nb/node36/dev10 value:192.168.10.140 [IP device 10] topic home/esp_gw/nb/node36/dev41 value:OFF [PIR state starts as OFF] PIR reads motion [motion detected ... topic home/esp_gw/nb/node36/dev41 value:ON [device 41 state confirms detection] PIR reads no motion [PIR detects nothing ... topic home/esp_gw/nb/node36/dev41 value:OFF [so device 41 reverts to OFF]
Next, Add OLED Display to the ESP32 PIR Node
|
|
|
Post by papa on Oct 23, 2022 19:12:58 GMT
Add OLED Display to the ESP32 PIR NodeIf, as in this thread, you use an OLED display & add the OLED sketch customization, PIR state reports will show at the bottom of one of the OLED screens.
Next, Configure OpenHAB for a ESP32 PIR Node
|
|
|
Post by papa on Oct 23, 2022 19:21:49 GMT
Configure OpenHAB for a ESP32 PIR Node
Prepare to Transform the Node's Message
From Serial Monitor results above, we see that channel 41 (PIR) sends the MQTT message of ON or OFF. We'll use an openHAB feature to transform ON to MOTION & OFF to NO MOTION.
First in openHAB's transform folder (Linux /etc/openhab/transform), create a pir.map file with the contents:
ON=MOTION OFF=NO MOTION
Next, Create OpenHAB Channel for PIR
|
|
|
Post by papa on Oct 23, 2022 19:26:36 GMT
Create Node36 Channel for a Node's Device 41 (PIR) [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/dev41Logged 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 Node36Channel41 ^^ arbitrary ? as long as we match it in a linked Item later (see below) Label Node36_PIR ^^ 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/dev41MQTT Command Topic [leave blank] Click "Show Advanced" & at Transform values/Incoming value transformations, enter MAP:pir.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 41
|
|
|
Post by papa on Oct 23, 2022 19:38:41 GMT
Create Item to Link to Node36's Channel 41, 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 41. [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 41 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:Node36Channel41
So my Item (your channel UID for the Item will be different) is String Node36PIR "Door36pir [%s]" { channel="mqtt:topic:c08f02ca8d:3f938be488:Node36Channel41" } 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 PIR state, e.g. MOTION or NO MOTION.
Next: Display Node36's PIR Data on HABPanel
|
|
|
Post by papa on Oct 23, 2022 19:55:00 GMT
Display a Node's PIR 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 Door36pir. In the openHAB Item field, choose the desired Item to link. You can click the drop down icon & scroll down to the Item named Node36PIR. OR you can enter Node36PIR in the search box. Either way, click on the Node36PIR Item listed. Scroll to the bottom of the Edit Widget Window & click [ Save]. Click the [Run] button to see "Door36pir" in the Main dashboard. If the node is programmed & powered & something enters the PIR field of vision, "MOTION" should be displayed on the widget. If nothing is in the PIR field of vision, "NO MOTION" should be displayed. Next, Upgrade for ESP32 PIR Node
|
|
|
Post by papa on Oct 23, 2022 21:55:42 GMT
Upgrade for ESP32 PIR Node (Interrupt) The last PIR software is moderately responsive in detecting motion. However, putting PIR code on an interrupt might be more responsive, especially if we create a node with more functions. At this post, download updated code with the option to put a PIR on an interrupt. To use the new option, follow customization shown above (incl possible OLED display). Then find & activate (delete // at the start) the new line #define PIRirq
Results on the Serial Monitor (& optional OLED display) should be similar to before, but may be somewhat more responsive. To see how the new code works, search the sketch for "ifdef PIRirq"
If you deactivate (add // to the start) #define PIRirq, the code will work as before & you can compare.
|
|