Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motion sensor example for Zigbee #10684

Closed
1 task done
ilker-aktuna opened this issue Dec 4, 2024 · 65 comments · Fixed by #10720
Closed
1 task done

Motion sensor example for Zigbee #10684

ilker-aktuna opened this issue Dec 4, 2024 · 65 comments · Fixed by #10720
Assignees
Labels
Area: Zigbee Issues and Feature Request about Zigbee Status: In Progress ⚠️ Issue is in progress Type: Feature request Feature request for Arduino ESP32

Comments

@ilker-aktuna
Copy link

Related area

Zigbee , ZHA

Hardware specification

Support for motion sensor

Is your feature request related to a problem?

I am working on a motion sensor. Is the motion sensor feature available in Zigbee stack ?
if yes, where can I find an example code ?

Describe the solution you'd like

motion sensor zigbee example

Describe alternatives you've considered

No response

Additional context

No response

I have checked existing list of Feature requests and the Contribution Guide

  • I confirm I have checked existing list of Feature requests and Contribution Guide.
@ilker-aktuna ilker-aktuna added the Type: Feature request Feature request for Arduino ESP32 label Dec 4, 2024
@P-R-O-C-H-Y P-R-O-C-H-Y self-assigned this Dec 5, 2024
@P-R-O-C-H-Y
Copy link
Member

Hi @ilker-aktuna, there is currently no PIR endpoint available in the Zigbee library.
The occupancy cluster (PIR) is available in esp-zigbee-sdk, so if you want you can create your own implementation of this endpoint type. There is currently no plan to add it. But I will have this in the list of feature requests for the Zigbee library.

@ilker-aktuna
Copy link
Author

ok. what is the correct path for creating my own implementation ?
For example, there is the temp. sensor example. Where can I find the code for temperature sensor ? maybe I can clone it and change the cluster IDs etc.

@P-R-O-C-H-Y
Copy link
Member

Here you can find the tempSensor EP source file.
https://github.com/espressif/arduino-esp32/blob/release/v3.1.x/libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp

If you will have any working example + sources for the EP PIR sensor, feel free to open a pull request with this addition. I will be glad to help on that to make it available for everyone :)

@ilker-aktuna
Copy link
Author

ok thank you.
Which source should I look to find options like "ESP_ZB_DEFAULT_TEMPERATURE_SENSOR_CONFIG" or "ESP_ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT" ?

@P-R-O-C-H-Y
Copy link
Member

P-R-O-C-H-Y commented Dec 6, 2024

There is no default config for PIR motion sensor in esp-zigbee-sdk. You may need to create your own "default config", which means declaring the clusters it should contain.

To be honest it's not that simple to do a new EP type with all the stuff around. Maybe you can keep this feature request open and I will take a look into it and add the implementation in future updates.

I am planning to add a Common sensor device type, where you will be able to add any clusters into it easily. So you will be able to create a single/multi sensor device.

@ilker-aktuna
Copy link
Author

ok. good.
In fact, if I had time, I would like to learn and add a new EP type as a contribution but I won't have time to learn soon.
I'll be waiting. But if I do something, I'll inform here.

@P-R-O-C-H-Y
Copy link
Member

Hi @ilker-aktuna, you can check the PR I linked. I have added the PIR motion sensor (occupancy) endpoint.
Feel free to test. I have been testing using C6 + HC-SR501 PIR module connecting to HomeAssistant.

@michapr
Copy link

michapr commented Dec 12, 2024

sorry for hijacking :-)) -> could you add environmental sensors too? CO2, VOC sensor?
(don't want to open new issue....)

@P-R-O-C-H-Y
Copy link
Member

In progress right now :) almost done with the CO2.

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Dec 12, 2024

Hi @ilker-aktuna, you can check the PR I linked. I have added the PIR motion sensor (occupancy) endpoint. Feel free to test. I have been testing using C6 + HC-SR501 PIR module connecting to HomeAssistant.

I could get the example from the PR but I also need the updated core library to test this.
how can I get the merged code for the library ?

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
I have downloaded the branch with PR but I can't find any way to update the esp32-core on my Windows pc.
is that even possible ?

@P-R-O-C-H-Y
Copy link
Member

@ilker-aktuna Please follow the Manual installation part of documentation. You can use the branch you downloaded instead of cloning :) https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html#windows-manual-installation

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Dec 13, 2024

@P-R-O-C-H-Y
ok I unzipped the folder to the Arduino/hardware/espressif/esp32 folder and then used the "get.exe" under tool folder.
but on my PC , under Sketchbook Directory there was no "hardware" folder.
So now I believe I have 2 installations of arduino-esp32 on my PC (one is the default one which is accessible through "boards manager", the other one is your branch which I installed manually)
How shall I use the one that I manually installed ?

@P-R-O-C-H-Y
Copy link
Member

@ilker-aktuna If you go to the Arduino IDE Tools menu -> Board:
You can see 2 options for ESP32 Arduino. The one with (in sketchbook) is the manual installed core.
So you can see in the image, I am using the manually installed core for developing :)

Screenshot 2024-12-14 at 0 22 58

@ilker-aktuna
Copy link
Author

ok. now I compiled and trying with my Hubitat hub.
This time with a Hubitat C8

The device starts , then I start pairing on my Hub.
Hub finds and says "Found a Zigbee device, initializing..."
But it gets stuck there.

On the device, I see that it is rebooting while tryign to connect to Zigbee network:

13:03:00.130 -> Starting Zigbee...
13:03:00.161 -> [ 2201][I][ZigbeeCore.cpp:138] zigbeeInit(): List of registered Zigbee EPs:
13:03:00.161 -> [ 2208][I][ZigbeeCore.cpp:140] zigbeeInit(): Device type: Simple Sensor device, Endpoint: 10, Device ID: 0x000c
13:03:00.201 -> [ 2221][I][ZigbeeCore.cpp:219] esp_zb_app_signal_handler(): Zigbee stack initialized
13:03:00.201 -> [ 2230][I][ZigbeeCore.cpp:225] esp_zb_app_signal_handler(): Device started up in factory-reset mode
13:03:00.201 -> [ 2239][I][ZigbeeCore.cpp:232] esp_zb_app_signal_handler(): Start network steering
13:03:00.201 -> Zigbee started successfully!
13:03:00.201 -> Connecting to network
13:03:00.201 -> ..............................ESP-ROM:esp32c6-20220919
13:03:03.207 -> Build:Sep 19 2022
13:03:03.251 -> rst:0xc (SW_CPU),boot:0x8 (SPI_FAST_FLASH_BOOT)
13:03:03.251 -> Saved PC:0x4001975a
13:03:03.251 -> SPIWP:0xee
13:03:03.251 -> mode:DIO, clock div:2
13:03:03.251 -> load:0x40875720,len:0x1228
13:03:03.251 -> load:0x4086c110,len:0xd9c
13:03:03.251 -> load:0x4086e610,len:0x2f74
13:03:03.251 -> entry 0x4086c110
13:03:03.482 -> �[0;31mE (274) ESP_ZIGBEE_CLUSTER: The requested add cluster ID:0x3 is already existed�[0m
13:03:04.518 -> Zigbee TEST

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Dec 15, 2024

@P-R-O-C-H-Y

if I put the log level to "verbose" , I get this line before reboot:

13:08:27.383 -> Connecting to network
13:08:27.383 -> .............................[ 5289][V][ZigbeeCore.cpp:344] factoryReset(): Factory resetting Zigbee stack, device will reboot
13:08:30.300 -> .ESP-ROM:esp32c6-20220919

I see in the code:


case ESP_ZB_ZDO_SIGNAL_LEAVE:  // End Device + Router
      // Device was removed from the network, factory reset the device
      if ((zigbee_role_t)Zigbee.getRole() != ZIGBEE_COORDINATOR) {
        Zigbee.factoryReset();
      }
      break;

but I don'T understand why it goes into that. Hub is still in pairing mode when it does that.

@P-R-O-C-H-Y
Copy link
Member

This ZDO_SIGNAL_LEAVE is recieved when device is removed from the network. This means the hubitat removes the device.

If you want to investigate this further, you will need to sniff the Zigbee network and send us a pcap file from the WireShark. Do sou have another C6/H2 devkit available?

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
I have another C6
how do I sniff ?

@P-R-O-C-H-Y
Copy link
Member

@ilker-aktuna please follow this comment: #10601 (comment)

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
is there an easier way of creating a sniffer ?
I have IDF 5.2 and I'M really not using it. I am not sure how to update to 5.3
is there a prebuilt binary maybe ?

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
I compiled with 5.2 (attached)
I hope it works..
esp_ot_rcp.zip

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Dec 15, 2024

@P-R-O-C-H-Y

I followed the tutorial and did everything.
However, wireshark shows "no packet" after this command:

python sniffer.py -c 13 -u COM19 --crc --rssi -b 460800 | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -

Initializing sniffer...
** (wireshark:13284) 20:34:44.119428 [Capture MESSAGE] -- Capture Start ...

on COM19 , I have the esp32-c6 with esp_ot_rcp binary
it is the correct port , if disconnect the cable, wireshark says "end of file on pipe"

I have the key "5A:69:67:42:65:65:41:6C:6C:69:61:6E:63:65:30:39" on pre-configured keys list Wireshark protocol zigbee.

then what is missing ?

@P-R-O-C-H-Y
Copy link
Member

@P-R-O-C-H-Y

I followed the tutorial and did everything. However, wireshark shows "no packet" after this command:

python sniffer.py -c 13 -u COM19 --crc --rssi -b 460800 | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -
Initializing sniffer...
** (wireshark:13284) 20:34:44.119428 [Capture MESSAGE] -- Capture Start ...

on COM19 , I have the esp32-c6 with esp_ot_rcp binary it is the correct port , if disconnect the cable, wireshark says "end of file on pipe"

I have the key "5A:69:67:42:65:65:41:6C:6C:69:61:6E:63:65:30:39" on pre-configured keys list Wireshark protocol zigbee.

then what is missing ?

The -c 13 in the command means your network channel. You need to check in your system on what channel is your Zigbee network running and change it to the proper channel.

@P-R-O-C-H-Y
Copy link
Member

I am closing this issue as it's solved by #10720. But let's continue here with the "debugging" on the Hubitat.

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y , @michapr
I'm glad that I am not the only one failing with sniffer.
I had thought that I'm missing a very obvious point. Seeing I'm not alone, I think there is either a problem with this sniff mehod, or there is a point that everyone could miss.
I hope we can figure out what's wrong together. Any ideas ?

@P-R-O-C-H-Y
Copy link
Member

If you can folks install the ESP-IDF v5.3 and build/flash it on your own by using idf.py command.

The main difference I there might be is a Board you are using and idk yours, but I sm using a UART port on the H2 Espressifs devkit.

Can you tell me your setup?

@ilker-aktuna
Copy link
Author

If you can folks install the ESP-IDF v5.3 and build/flash it on your own by using idf.py command.

The main difference I there might be is a Board you are using and idk yours, but I sm using a UART port on the H2 Espressifs devkit.

Can you tell me your setup?

I already compiled myself and flashed using idf.py before you shared the binaries. I had the same result.
My device is a esp32c6-zero with one USB port (uart + power)

@michapr
Copy link

michapr commented Dec 20, 2024

I was using the USB port before - every time no output.
Now have seen in esp_ot_config.h that UART is using (no idea how to change it to USB port,....)

I have connected an adapter UART-USB and here I receive data now....

But in this time (after adding Zigbee key and made setting from the link) do not receive valid data:
grafik

Something is still missing....

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
update:
I got a "normal" C6 from a friend. Using the UART port it can sniff actually.

So I got the attached traces showing the issue on my zigbee network.
Before explaining further, let me remind you that Hubitat actually detects the device and pairs successfully. But the device (esp32) reports it has failed.

So the first file is capture when my hub has already paired and shows a motion sensor.
Then I removed the sensor from zigbee network (Hubitat) and restarted capture (and esp32) to record a full failing scenario.
That is the second capture file. I hope it is clear for you and you can see the problem in these traces.

pairing_2.zip

Btw, do you have any idea on how I can use sniffer on esp32-c6 zero which has no UART port ?

@coolibre
Copy link

coolibre commented Dec 23, 2024

There is no default config for PIR motion sensor in esp-zigbee-sdk. You may need to create your own "default config", which means declaring the clusters it should contain.

To be honest it's not that simple to do a new EP type with all the stuff around. Maybe you can keep this feature request open and I will take a look into it and add the implementation in future updates.

I am planning to add a Common sensor device type, where you will be able to add any clusters into it easily. So you will be able to create a single/multi sensor device.

@P-R-O-C-H-Y

Is this something that is already on the horizon, or to complex for a quickshot? Im currently failing on adapting TempSensor example and registering a cluster for an object speed measuring sensor. Basically I only need one float attribute speed but had yet no luck on figuring out the co working with esp zigbee SDK. https://docs.espressif.com/projects/esp-zigbee-sdk/en/latest/esp32/user-guide/zcl_custom.html

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
Did you have any chance to look at my capture files ?

@SuGlider SuGlider added the Area: Zigbee Issues and Feature Request about Zigbee label Dec 29, 2024
@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
I hope everything is fine, wishing you a happy new year.
Did you have any chance to analyze my sniff output ?

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Feb 2, 2025

@P-R-O-C-H-Y

Finally I had some time to test the motion sensor example.
On my Hubitat the device is recognized and I selected the "generic zigbee motion sensor" profile.
From the arduino skecth I am sending the following commands:
zbOccupancySensor.setOccupancy(true);
and
zbOccupancySensor.setOccupancy(false);

followed by
zbOccupancySensor.report();

but on the hub "motion" stated does not change.
Is "occupancy" equivalent to "motion" ? I don't see a "occupancy sensor" profile on the hub.

Btw, my sensor also has temperature capability. (and also most of the zigbee motion sensor devices/profiles have dual function as motion & temperature)
But I could not find a way of adding 2 capabilities to one device on Arduino sketch.
If I define another endpoint with a new endpoint number, my hub only gets the first endpoint.
If I define both endpoints with same number then code crashes.

Should we have a separate device type supporting both motion and temperature ?

@P-R-O-C-H-Y
Copy link
Member

Hi @ilker-aktuna, there is no issue with the Occupancy sensor when its connected to HomeAssistant.
It gets automatically recognized by the ZHA, that it is an occupancy sensor.
About the 2 endpoints, this also perfectly works on HomeAssistant. You can add even more than 2 endpoints, so you can combine whatever you want like:
EP1 - Temperature sensor
EP2 - ON/OFF Light
EP3 - Occupancy sensor
...

I don't have the hubitat device so Idk if I would be able to help you more on this.
I can just say it works perfectly using the HomeAssistant, and also if I program any Coordinator, which controls device with multiple endpoints.

@ilker-aktuna
Copy link
Author

Hi @P-R-O-C-H-Y ,

I understand that it works perfectly with HA but I am trying to make this run on a Hubitat hub. 2 EPs on 1 device might be a problem on Hubitat side. I will try to get help from Hubitat community on that (because their support is not really taking any feedback)
But if this is a limitation on Hubitat side, how can I overcome this ?
Can I create a single EP with both features ?

About the occupancy sensor; can you give me a specification on how it is designed on esp32 side ?
Maybe an occupancy sensor is not a motion sensor. If that is the case, I should give up because there is no occupancy sensor profile on Hubitat.
But if motion sensor and occupancy sensor are same thiğng with different names then I have to understand why it does not work.

@P-R-O-C-H-Y
Copy link
Member

The main thing is the Zigbee 3.0 Specification. So there are many defined Home Automation profiles, which defines what the device is doing. So each end device have its own type, only some few sensors are just "Simple Sensor" device type. This is how the systems recognizes the devices (also by check of the clusters). I think this is something not completely done on the Hubitat side.

You can try to create an endpoint device having custom clusters with extended functionality, but I am not sure if it will work well.
Try to create a custom end device, so you will have only 1 endpoint. Let me know if that worked for you.
But TBH, this is not how the things should be done the right way.

The occupancy is implemented as:
ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID = 0x000C, /*!< Simple Sensor device */

having this cluster:
ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING = 0x0406U, /*!< Occupancy sensing */

@P-R-O-C-H-Y
Copy link
Member

@ilker-aktuna Good example of multiple functionality device in simple endpoint is the TemperatureSensor end device.
There is an option to add the humidity cluster, so you can have both TEMP+HUM in one endpoint.

@ilker-aktuna
Copy link
Author

ilker-aktuna commented Feb 3, 2025

You can try to create an endpoint device having custom clusters with extended functionality, but I am not sure if it will work well.
Try to create a custom end device, so you will have only 1 endpoint. Let me know if that worked for you.

@P-R-O-C-H-Y
how and where should I do that ? On the sketch itself or should I change the core esp32 code to add a new device ?

@P-R-O-C-H-Y
Copy link
Member

You can implement that in a separate cpp and header file directly in your sketch. Just don't forget to use correct includes in the files :)

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
I learnt that on Hubitat 2 Endpoints in one Zigbee device is supported.
But I am confused. As I wrote in this thread, if I add 2 sensors to the zigbee core in Arduino sketch , the device is only recognized as the first cluster defines on Hubitat.
What is the correct implementation of adding 2 endpoints in one device on Arduino sketch ?

Also, as another option, I don't understand how I should add the second cluster to the motion sensor.
On the Temp+Humidity sensor example, there is a "zbTempSensor.addHumiditySensor(0, 100, 1);" command which calls the "addHumiditySensor" function in the tempsensor code.
But there is no such "addTemperatureSensor" in the OccupancySensor code.

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y

did you notice my question above ?

@P-R-O-C-H-Y
Copy link
Member

Hi @ilker-aktuna,
sorry for no reply on that. It's too many issues/pr to check and time to time you can forget something. Good you pinged me :)

Only the Temperature sensor have the option to add a Humidity cluster, as the device type for HA is Temperature_Humidity_Sensor. SO there is the function addHumidity to add this cluster to the endpoint.

For all other devices, you can just combine multiple endpoints in the sketch itself with unique numbers. As example you can check the Pressure + Flow example :

ZigbeeFlowSensor zbFlowSensor = ZigbeeFlowSensor(FLOW_SENSOR_ENDPOINT_NUMBER);

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
Multiple-endpoints issue on Hubitat seems to be resolved. I just need to write my own driver on Hubitat for that. So with a custom driver this implementation works (PIR & temperature sensor for example)

I just have a side question:
Esp32-c6 seems to support coexistence of Zigbee and Wifi as explained here with some level of performance degradation:
espressif/esp-zigbee-sdk#361
But when I try it on Arduino sketch, wifi connects , but loses connectivity when zigbee network is joined.
Do we need to implement something on Arduino side to enable coexistence of wifi and zigbee ?
on native code , there is this "esp_coex_wifi_i154_enable()" command.

@P-R-O-C-H-Y
Copy link
Member

@ilker-aktuna I am glad to hear that its now working also with the Hubitat.

About the C6 and WiFi+Zigbee coexistence, please refer to this issue #10871

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y
thanks.
how can I stop zigbee when I need wifi ?
also visa versa, how can I stop wifi and restart zigbee ?

@ilker-aktuna
Copy link
Author

@P-R-O-C-H-Y

I have a problem with status reporting of the motion sensor.
If the zigbee coordinator sends a read command to the cluster 0x0406 , it can read the motion value reported.
But normally the code is sending updates on change of status with following commands:
zbOccupancySensor.setOccupancy(true);
zbOccupancySensor.report();

I believe the corrdinator shouldn't need to send a read command because the device will update status when needed.
So what is missing ?
is there another command to send the zigbee status report to the coordinator ?

@P-R-O-C-H-Y
Copy link
Member

Can you provide more info of what is your setup? Who is the coordinator and if you use any system like HA with ZHA or Z2M?

@ilker-aktuna
Copy link
Author

Coordinator is Hubitat hub.

@P-R-O-C-H-Y
Copy link
Member

P-R-O-C-H-Y commented Mar 20, 2025

Please can we move to our Discord server to discuss this or to discussions to not spam closed issue?

@ilker-aktuna
Copy link
Author

sure what is the address ?

@P-R-O-C-H-Y
Copy link
Member

Arduino-ESP32 Discord

@P-R-O-C-H-Y P-R-O-C-H-Y changed the title Motion sensor example for Zigee Motion sensor example for Zigbee Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Zigbee Issues and Feature Request about Zigbee Status: In Progress ⚠️ Issue is in progress Type: Feature request Feature request for Arduino ESP32
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants