devWaves / SwitchBot-MQTT-BLE-ESP32

Allows for multiple SwitchBot bots and curtains to be controlled via MQTT sent to ESP32. ESP32 will send BLE commands to switchbots and return MQTT responses to the broker. Also supports Temperature, Motion, Contact sensors
MIT License
539 stars 66 forks source link

adding Contact Sensor & Motion Sensor #27

Closed BangerTech closed 2 years ago

BangerTech commented 2 years ago

Hi there,

first of all a huuuge Thank you for the great work!!!

i was wondering if ther would/ will be a way to connect the contact & motion sensor in the near future?

thanks again

greets

devWaves commented 2 years ago

edit: switchbot released code for motion and contact sensor. I didn't notice. I will take a look this weekend

devWaves commented 2 years ago

ya I think I can make it work, but it will take some work. But while you are controlling a bot, you won't get motion/contact updates. I will need to make the code scan all the time, but not send MQTT updates all the time

problem is I don't have either. Do u have both? I will for sure need a tester

BangerTech commented 2 years ago

that sounds great! I think i wouldn’t be a problem for the users to wait till they are finished controlling a bot right? ;-) yes i got both so would be no problem to be your tester ;-)

devWaves commented 2 years ago

waiting shouldn't be a big problem ... I think. I feel like all implementations would work that way

within the switchbot app how long does it say motion for? what is the motion timeout

I noticed the contact sensor has a button on it also. That could be tricky to integrate

BangerTech commented 2 years ago

soo i would say let´s start with the contact sensor. Basically the contact sensor reading would be enough for me. But on this Contact Sensor there is a Light Indicator / Sensor, a Fresnel Lens / Pir Sensor and a Reset Button.

devWaves commented 2 years ago

I have created a new branch for testing the motion sensor and contact sensor https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContact/SwitchBot-BLE2MQTT-ESP32.ino

Please try this code out and let me know if you are getting information from the contact sensors and motion sensors and let me know if the information looks correct

Currently in the code a contact sensor and motion sensor will have their attributes collected the same was as meters. Every 30 secs a scan will be made. This is only temporary. I will make the code changes to constantly scan after I can confirm the values are at least correct

These are the values I am collecting....

For motion sensor

For contact sensor

In order to test contact sensor

In order to test motion sensor

BangerTech commented 2 years ago

Hi and thanks for the work! I´ll try this out today and let you know.

Greets

Mike

Am 04.09.2021 um 19:10 schrieb devWaves @.***>:

I have created a new branch for testing the motion sensor and contact sensor https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContact/SwitchBot-BLE2MQTT-ESP32.ino https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContact/SwitchBot-BLE2MQTT-ESP32.ino Please try this code out and let me know if you are getting information from the contact sensors and motion sensors and let me know if the information looks correct

Currently in the code a contact sensor and motion sensor will have their attributes collected the same was as meters. Every 30 secs a scan will be made. This is only temporary. I will make the code changes to constantly scan after I can confirm the values are at least correct

These are the values I am collecting....

For motion sensor

battery linkquality motion = possible values : "MOTION", "NO MOTION" illuminance = possible values : "BRIGHT", "DARK" led = possible values : "ON", "OFF" sensedistance = possible values : "LONG", "MIDDLE", "SHORT" lastmotion = NOT COMPLETED, will eventually be a timestamp For contact sensor

battery linkquality contact = possible values : "OPEN", "CLOSED", "TIMEOUT" motion = possible values : "MOTION", "NO MOTION" illuminance = possible values : "BRIGHT", "DARK" buttoncount = cycles between values 1-15 , resets after 15 entrancecount = cycles between values 1-3 , resets after 3 outcount = cycles between values 1-3 , resets after 3 lastmotion = NOT COMPLETED, will eventually be a timestamp lastcontact = NOT COMPLETED, will eventually be a timestamp In order to test contact sensor

leave the door open for at least 35 seconds, and see if HA updates contact properly leave the door closed for at least 35 seconds, and see if HA updates contact properly have no motion for 35 seconds, and see if HA updates motion properly have constant motion for 35 seconds, and see if HA updates motion properly push the button, wait 35 seconds, check if the buttoncount value changed (1 to 3) make it dark, wait 35 secs and see if HA updates illuminance properly make it bright, wait 35 secs and see if HA updates illuminance properly In order to test motion sensor

have no motion for 35 seconds, and see if HA updates motion properly have constant motion for 35 seconds, and see if HA updates motion properly make it dark, wait 35 secs and see if HA updates illuminance properly make it bright, wait 35 secs and see if HA updates illuminance properly — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/issues/27#issuecomment-913006897, or unsubscribe https://github.com/notifications/unsubscribe-auth/AROZFXIMGIPBJJUMVNFYWMDUAJHJJANCNFSM5DJD2OAQ.

devWaves commented 2 years ago

I updated the branch to v6Alpha2 and I might actually have it working with contact and motion sensor. They are no longer doing the same things as meters, the ESP32 should update based on motion, open/close, Light changes, LED changes, number of button pushes/out counts/ entrance counts

the code now scans all the time and will send MQTT on updates from the contact sensors and motion sensors.

The only thing left it to collect the last motion timestamp and last contact timestamp. So then if a message is missed I can send one based on the last timestamp

devWaves commented 2 years ago

I plan on sending a button push MQTT message whenever the button count changes

So you can use the button on the contact sensor to trigger other things in HA

BangerTech commented 2 years ago

awesome that sounds perfect!! i was busy today but i´ll test it out tomorow morning. Thanks a lot!!

BangerTech commented 2 years ago

so i´ve tested the contact sensor but i don´t get any data in mqtt.fx. I´ll try and flash the esp again maybe there was a error

BangerTech commented 2 years ago

i´ve flashed the esp again now i´ve put in the motion sensor aswell but i get no informations about the contact and motion sensor, meter and bot works fine

BangerTech commented 2 years ago

Unbenannt

devWaves commented 2 years ago

did you perform a reboot of HA? for some reason I have to reboot sometimes to see new devices show up

if the MAC were at least detected the HA MQTT diwcovery should have happened

It works for me when I fake a bot as a motion/contact sensor, but it can detect bot

And they are all setup/configured in the switchbot app already?

I guess u would at least see something in MQTT, so if you set up the macs correctly it sounds like there is a issue with the code... crap

There must be something different between bots that I missed

I'm expecting the BLE service data to be the same for all their devices

devWaves commented 2 years ago

The way that I tested was I faked a bot as a motion/contact sensor then I used the switchbot app to control the bot

when i used the switchbot app to turn the bot ON, it would update in HA to ON after about a second

I assumed this would simulate a Motion=ON from a contact/motion sensor

BangerTech commented 2 years ago

yes the contact and the motion sensor are setup in the app. wrote the mac adresses in the code the same way like the meter and bot so it should be fine. just did a reboot on the esp and checked everything again. so if i scan in mqtt.fx i don´t the a channel for example switchbot/esp32/motion/motionone..... so i´ve tried to subscribe to one manually but no data is coming. i assume the state channel should be the same way right the one from the meter or the bot right? like switchbot/esp32/motion/motionone/state

devWaves commented 2 years ago

Hmm...

I don't have state for motion or contact. I can add them though. The contact has both motion and contact, so there are technically 2 states

The first MQTT message you would see when the device is discovered would be on your MQTT discovery topic. Example:

homeassistant/
homeassistant/sensor/contactone/
homeassistant/sensor/contactone/
homeassistant/binary_sensor/contactone/
homeassistant/binary_sensor/contactone/
homeassistant/sensor/contactone/contact/config
homeassistant/sensor/contactone/buttoncount/config

the issue seems like the MACs are not being discovered if you aren't at least getting those

Then once after that the data would all be on these topics

switchbot/esp32/contact/contactone/
switchbot/esp32/motion/motionone/
BangerTech commented 2 years ago

hmm yeah the mac´s are not discovered. i don´t understand why it should be the same way like the bots, curtains & meters are discovered right?

devWaves commented 2 years ago

ya I can only read the available documentation at this point. It sucks switchbot doesn't sell in Canada and I have to pay extra in fees extra to order them

I made the code so that if there are contact/motion sensors it will scan all the time, if there are none in the lists it will work the same way as before and only scan when needed. Even when scanning all the time though, it only sends MQTT updates when needed

back to the drawing board I guess....

If you turn on printSerialOutputForDebugging, you can watch the Arduino IDE Serial monitor logs to see if you see you MAC addresses in the lists as they fly by

static bool printSerialOutputForDebugging = true;  

you can unplug the ESP32 to stop the logging, then copy/paste the contents to notepad and search the text for your MACs

devWaves commented 2 years ago

crap... I think I know what I did wrong. They dont work the same as bots. You actually have to connect and subscribe to BLE notifications. Hopefully I can make that work

oh well the other changes I made are still a good addition

BangerTech commented 2 years ago

Unbenan11nt

BangerTech commented 2 years ago

so the mac´s are both in there. ......b8:3d is the contact sensor and ....21:16 is the motion sensor.

ahh ok so the ble notification would be the solution? yaa i hope so too! but i think you can ;-)

devWaves commented 2 years ago

so ya they should still work the way I did it, but wont be instant status. Instant status can only be done by subscribing to BLE notifications. Ill see if that works. One device would work, but not sure if subscribing to multiple devices works

Maybe the BLE scan timeout is too short to get all the data because from your screenshot it looks like that is the issue. They are being found by MAC but not matching to the service uuid

This is what I would expect to see

Advertised Device found: Name: , Address: xx:xx:xx:xx:xx:xx, manufacturer data: 5900e57df2529d9c, serviceUUID: cba20d00-224d-11e6-9fb8-0002a5d5c51b
Service Data:
UUID: 0x0d00, Data: ⸮⸮
Adding Our Service ... 
contactone
callForInfoAdvDev
deviceName: WoContact
Assigned advDevService

Does the contact or motion sensor support password?

devWaves commented 2 years ago

You can try changing these values. Specifically setWindow

pScan->setInterval(45);
pScan->setWindow(1000);

I believe this will make it scan longer, and might get more data from the device

BangerTech commented 2 years ago

so setWindow to 1000 didn´t help. no i don´t think contact or motion supports password i don´t see any of that option in the app

devWaves commented 2 years ago

ok. not sure why. According to how I read the documentation that should work

I will make some changes to force add the devices if the MACs match, then I will add the BLE connect/notify stuff

from your screenshot, the data packet only has one byte = d

Can you search your logs again to see if there are more entries for your MACs. The lines under the MAC address will say UUID: .... data: ......

I am hoping to see more on the data part

maybe you can try a passive scan instead of active scan

set this to false. I have it as true

pScan->setActiveScan(false);
BangerTech commented 2 years ago

1 2

BangerTech commented 2 years ago

so i did change the ...ActiveScan to false. hope this screenshot i useful. the adress: .....:b8:3d is the contact sensor

devWaves commented 2 years ago

No more usueful info in the screenshots. thanks tho. I still have an idea though, just need to code it up

devWaves commented 2 years ago

Can you try using a bluetooth BLE GATT tool to get a screenshot of the service data?

the tool I just tried for Android is called "nRF Connect for Mobile". you can use any app that will provide the service data. Within the app, under scanner, start a scan and find your device. Click on it to expand it and take a screenshot

Do you have cloud access enabled? maybe you need to do that for it to broadcast? not sure why it isnt seeing all the data in the scan

devWaves commented 2 years ago

if you have your switchbot hub connected to them, turn your switchbot hub off. They require a constant connection so maybe the switchbot hub being connected to them messes things up

I know that only one BLE connection can be made to a bot at a time, so that would make sense

maybe pop the battery out of the sensor to ensure it disconnects also

BangerTech commented 2 years ago

soo the hub is not installed so that shouldn’t be a problem.

I’ll enable cloud function to see whats up ;-)

Am 07.09.2021 um 23:44 schrieb BangerTECH @.***>:



>> Am 07.09.2021 um 22:57 schrieb devWaves ***@***.***>: >> >  > if you have your switchbot hub connected to them, turn your switchbot hub off. They require a constant connection so maybe the switchbot hub being connected to them messes things up > > I know that only one BLE connection can be made to a bot at a time, so that would make sense > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub, or unsubscribe.
BangerTech commented 2 years ago

IMG_7062

devWaves commented 2 years ago

ok that looks correct but without the service UUID. I guess it doesnt broadcast while not connected to it?

the data part looks correct so I can use that. I'll make some changes later today to add the devices if the MAC is found

also your RSSi connections are not good at least with your phone. anything below -80 is not good. From experience with the bot, a connection requires a good rssi value

devWaves commented 2 years ago

I commited a new change that will ignore looking for the service UUID and add it if it find the MAC address

You should at least get the MQTT discovery device created now. The values is the question if its correct or not

Try it out and let me know

BangerTech commented 2 years ago

you sir... are great ;-) so now i get every data correct! so for example the contact sensor it gets data ever second. get the rssi, batt, motion with (Motion/NO Motion), contact: OPEN/CLOSED, light: (BRIGHT/DARK), entrancecount: 1/2....., outcount: 0/1/2..., buttoncount: 0/1/2.... maybe you could change the counts to a YES/NO or something and the button aswell.

for the motion: updates every second too. rssi, batt, motion: MOTION/NO MOTION, led: ON/OFF, sensedistance: SHORT/MEDIUM/LONG, light: BRIGHT/DARK

BangerTech commented 2 years ago

contact motion

BangerTech commented 2 years ago

the only wrong function i´ve discovered is when i´m controlling the switchbot bot via the app i get no mqtt message on switchbot/esp32/bot/switchbotone/attributes if i send a mqtt message switchbot/esp32/bot/switchbotone/set ON then the attributes are updated to but the state is still OFF

BangerTech commented 2 years ago

bot

devWaves commented 2 years ago

awesome! so I guess I do have some idea of what I am doing, lol

Ya I don't have the bot updating attributes when you call with switchbot app atm. I did that during testing though and it worked, so I will add it in the final release. The code only scans all the time if you have a contact sensor or motion sensor in the list. I will add a true/false value for people that want to scan all the time in order to get these kinds of updates

the outCount,entranceCount,buttonCount are all counts they are not booleans. Is it the number of times ......, then the value resets to 0 to start again. Thats how switchbot did it. I will add a binary sensor for when the value changes though

2 main things left to do

  1. see if I can connect and subscribe to BLE notifications to get instant status, and leave it subscribed
  2. figure out how to get last contact, last motion timestamps
BangerTech commented 2 years ago

Hey just wanted to check if you need any more infos from my side? ;-)

Am 09.09.2021 um 14:22 schrieb devWaves @.***>:

 awesome! so I guess I do have some idea of what I am doing, lol

Ya I don't have the bot updating attributes when you call with switchbot app atm. I did that during testing though and it worked, so I will add it in the final release. The code only scans all the time if you have a contact sensor or motion sensor in the list. I will add a true/false value for people that want to scan all the time in order to get these kinds of updates

the outCount,entranceCount,buttonCount are all counts they are not booleans. Is it the number of times ......, then the value resets to 0 to start again. Thats how switchbot did it. I will add a binary sensor for when the value changes though

2 main things left to do

see if I can connect and subscribe to BLE notifications to get instant status, and leave it subscribed figure out how to get last contact, last motion timestamps — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

devWaves commented 2 years ago

I think I've got everything for now. I'll try to look at the other stuff this weekend

just let me know if you are seeing mqtt updates every second even if nothing changes, because you shouldnt. you will get updates on a rescan also

devWaves commented 2 years ago

I will need you to test something and report back to me please

test this code with ONLY one contact sensor or one motion sensor (no other devices) https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/v6MotionContactTesting/SwitchBot-BLE2MQTT-ESP32.ino

I need you to let me know if you see any messages on these topics (or whatever you used for topics names that match this)

make sure to open/close the door, or make movement while testing. It will only publish on topic 2 if the values change

Topic 1)

switchbot/esp32/contact/contactone/subscribetest
switchbot/esp32/motion/motionone/subscribetest

I am looking for at least one message with payload "subscribed"

Topic 2)

switchbot/esp32/contact/contactone/gotsubscribedmsg
switchbot/esp32/motion/motionone/gotsubscribedmsg

payloads

"GOT MESSAGE"
"PROCESSED MESSAGE"

If you aren't getting any messages on the 2nd topic specifically, then it doesn't work the way I think it does

This code is only for testing and won't work fully correctly

BangerTech commented 2 years ago

Erfassen Erfasse2n

BangerTech commented 2 years ago

so for the contact sensor i got 3 topics.

see the payloads on the pictures, first one is when i set the payload decoded by plain text, second one is when i set payload decoded by json pretty format

BangerTech commented 2 years ago

Erfasse222n

BangerTech commented 2 years ago

got no ...../gotsubscribedmsg on a movement or by a physical change of the device

devWaves commented 2 years ago

ok thanks a lot. It looks like my original way of doing it is the "proper" way, I just need to make some changes.

I assumed if you kept a connection active and subscribed to BLE notifications it would provide instant status update. But thats not the case from what I can tell

When using the app is it instant status update? is it just as fast with the way I coded it before? (with the esp32 similar distance as phone)

it looks like I was overthinking it

BangerTech commented 2 years ago

no worries i do what i can ;-) ya on the app it takes like a second so pretty instant ;-) kinda the same way like with the esp.

BangerTech commented 2 years ago

hey buddy what´s up ;-) did you got any chance to check the code yet? can´t wait to test it...