openwonderlabs / python-host Goto Github PK
View Code? Open in Web Editor NEWThe python code running on Raspberry Pi or other Linux based boards to control SwitchBot.
License: Apache License 2.0
The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.
License: Apache License 2.0
I have two Meters with firmware 2.5. Although the Switchbot app works well, the API and Linux tools fail to see the Meters.
sudo python3 switchbot_py3.py --scan
results in
No Switchbots found
Also I cannot see info from the Meters BLE advertisements using, for example, hcitool lescan
. The meters show up in the hcitool
scanned devices list with just the MAC but no name like so:
F4:CC:5A:AF:7E:B5 (unknown)
Similarly only the MAC is known when scanning with nRF from an Android phone.
I suspect that it may have something to do with a firmware change from 2.4 to 2.5. My meters were 2.5 out of the box. I've spoken to others online using 2.4 for whom the API, Linux tools, nRF, etc. work with no problem.
On its face, the issue looks similar to this #13, which was closed but not really resolved.
Is anyone else experiencing the same? Any tips?
Thanks.
Hi,
I have a question. Is there a way to download the historic measurements of the SwitchBot Meter Plus directly with this library ? It is possible by using the android app, but it would be great if it could be done via a linux system.
I'am thankfull for any recommendation or help
Lukas
Add Download Temperature and Humidity as CSV from SwitchBot Meter Plus
No response
The Driver can get firmware version and battery?
i want to manage the SwitchBot.
I followed the same steps for python3 deployment with a freshly installed ubuntu, but the scanning ending up with:
Scanning for bluetooth low-energy devices
Discovering Switchbot services
Traceback (most recent call last):
File "switchbot_py3.py", line 178, in
main()
File "switchbot_py3.py", line 153, in main
devices = scanner.scan()
File "switchbot_py3.py", line 92, in scan
return [dev for dev in devices
File "switchbot_py3.py", line 93, in
if self.is_switchbot(dev, self.bt_interface, self.connect_timeout)]
File "switchbot_py3.py", line 74, in is_switchbot
with connect(device, bt_interface, timeout) as req:
File "/usr/lib/python3.6/contextlib.py", line 81, in enter
return next(self.gen)
File "switchbot_py3.py", line 45, in connect
req.connect(False, 'random')
gattlib.BTIOException: connect: Connection refused (111)
While, i try with pybluez, i can see the bot in the list of discovered devices. Any guidance here?
OS: Ubuntu 18.04
Python: 3.6.9
Dongle: CSL - USB Nano V4.0 Bluetooth Adapter
Bot: Curtain
I combined two Switchbot Curtains 2 and I would like to have the same options as the app. To control both sides via a single cover entity, but also to control both sides separately via cover entities.
Currently I added both mac addresses with the Python code and the behavior is like this:
Left: this will move both sides (as left is the main switchbot in the switchbot app)
Right: this will move only the right side
It seems that there is no way to only move the left side seperately, while this is possible via the app. Is there any documentation how this works?
n/a
n/a
n/a
No response
Cannot read the display mode and value of the Meter device via BLE
With the Bot device the communation works as below shows:
gatttool -b xx:xx:xx:xx:xx:xx -t random -I
connect
char-write-cmd 0x0010 570F31
char-read-uuid cba20003-224d-11e6-9fb8-0002a5d5c51b
No logs are required.
No config
The firmware version on the Meter device is: 2.7
Thanks for your great work. Any news and timeline for support of contact sensor, motion sensor and including brightness sensor? It would be great seeing the sensors work with @donavanbecker homebridge-switchbot-openapi soon.
Thanks in advance.
I am having trouble with this, even though I installed bluepy, the switchbot.py cannot find it
pi@raspberrypi:~/python-host` $ python switchbot.py
Traceback (most recent call last):
File "switchbot.py", line 17, in
from bluepy.btle import Scanner, DefaultDelegate
ImportError: No module named bluepy.btle
but I already did the bluepy install and it shows here:
pi@raspberrypi:~/python-host $ sudo pip3 install bluepy
Requirement already satisfied: bluepy in /usr/local/lib/python3.5/dist-packages
Running on a Raspberry Pi 4 using the inbuilt BT adapter
I've attempted to use the switchbot_py3.py as well as doing this myself and get the following when sending the handles which it should be sending:
gattlib.GATTException: Characteristic value/descriptor operation failed: Invalid handle
E.g.
python3 switchbot_py3.py -d XX:XX:XX:XX:XX:XX -c on
Connected!
Traceback (most recent call last):
File "/root/python-host/switchbot_py3.py", line 180, in <module>
main()
File "/root/python-host/switchbot_py3.py", line 175, in main
driver.run_command(opts.command)
File "/root/python-host/switchbot_py3.py", line 124, in run_command
return req.write_by_handle(self.handles[command], self.commands[command])
gattlib.GATTException: Characteristic value/descriptor operation failed: Invalid handle
Is there a change to the commands/handles with Switchbot Bot version 5?
Hello
I've tried to run this on the Raspberry Pi Zero W on the latest Raspbian lite image. The Pi comes with Bluetooth built int but the connection requests always time out.
Any idea why this is happening?
Thanks
Hi,
do you thin that is possible to add curtain info to the script?
I mean a command that passing the mac-address output something like this:
{
"id": "ec58c5d00111",
"address": "ec:58:c5:d0:01:11",
"rssi": -39,
"serviceData": {
"model": "c",
"modelName": "WoCurtain",
"calibration": true,
"battery": 91,
"position": 1,
"lightLevel": 1
}
}
This was already supported on this project, but i want avoid to use node
thanks
I just bought a switchbot... And thought the API will be very quick to use but was not.
Can you do a total review of your readme.md to make it more understandable to people, according to your code ?
Tooks many minutes to understand that :
sudo python2 switchbot.py <mac_adress> <component_type> <action>
sudo python2 switchbot.py <mac_adress> Bot Turn On
As python3 script and basic python doesn't work really well on Rpi even following your step and review .o in dependant libraries.
And then, show to people how to get easily mac adresse without scanning with the app on the phone (with a screenshot for exemple).
$ sudo python switchbot.py <mac_adress> Turn On
Usage: "sudo python switchbot.py [mac dev_type cmd]" or "sudo python switchbot.py"
Preparing to connect.
Connection successful.
Unsupported operations
Complete
Well all configuration is OK, this is a bug from documentation and message shown using the script, it's not the good command that is asked to.
No response
I run the script:
sudo python switchbot.py
Usage: "sudo python switchbot.py [mac_addr cmd]" or "sudo python switchbot.py"
Start scanning...
scan timeout
No SwitchBot nearby, exit
Raspberry Pi 4 with Bluetooth 5.5.
SwitchBot S1, FCC ID:2AKXBS1
The documentation for the command to change the RGB value is incorrect.
The column shows BYTE 3 is the Lvl but it is actually the Red colour. That in turn offsets the Green and Blue.
From the document I expect that sending [0x47, 0x01, 0x16, 0x32, 0xFF, 0x00, 0x00] would set the light to red and 50% brightness.
It actually sets it to a red / green colour.
Sending [0x47, 0x01, 0x16, 0xFF, 0x00, 0x00] does actually set red.
Send [0x47, 0x01, 0x16, 0x32, 0xFF, 0x00, 0x00] the colour is red / green
Send [0x47, 0x01, 0x16, 0xFF, 0x00, 0x00] the colour is red.
No logs are required.
No config
I have 3 switchbots running and operate them via node-red with this command:
sudo python3 python-host/switchbot_py3.py -d XX:XX:XX:XX:XX:XX -c on
i have many devices with batteries running in my flat and i want to keep an overview of what will need batteries soon. Unfortunatly this project does not give me that possibility and i wondered if it would be possible to implement this since other pythonlibraries seem to be able to do that.... unfortunatly these lack documentation and i dont know hot to get these running in node-red.
iE: https://github.com/Danielhiversen/pySwitchbot
is this even a planned feature?
Make a command that gives me a batterylevel
No response
It would be great to be able to move the curtain bot left and right (outside of the calibrated area) just as the buttons in the app do during calibration. I'd put a PR myself for this but I'm not sure what the command data would be?
Hi
I’m using a raspberry pi 4 and a 3b and both have problems connecting to the switchbots on the first connection attempt. Generally after the first try the second command works fine.
Is this a known issue. I don’t believe there are range issues and the switchbot is about 2m away with direct line of sight.
Thanks
I have the switchbot curtain. I wondered if that is already supported by this library?
Below is the error I get when trying to download gattlib. It seems pip3 wants to install and not simply download. Is there a way to make it download only?
$ pip3 download gattlib Collecting gattlib Using cached gattlib-0.20200122.tar.gz (44 kB) ERROR: Command errored out with exit status 1: command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-download-5wiu4h96/gattlib/setup.py'"'"'; __file__='"'"'/tmp/pip-download-5wiu4h96/gattlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-download-5wiu4h96/gattlib/pip-egg-info cwd: /tmp/pip-download-5wiu4h96/gattlib/ Complete output (7 lines): Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-download-5wiu4h96/gattlib/setup.py", line 51, in <module> long_description = fh.read() File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 7189: ordinal not in range(128) ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The pip3 version I have
$ pip3 --version pip 20.0.2 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
I also tried to find the .gz in ~/.cache/pip but it wasn't there.
$ python -c "from pip._internal.locations import USER_CACHE_DIR; print(USER_CACHE_DIR)" ~/.cache/pip ~/.cache/pip$ find . | grep gattlib
it's not working for me, but why?
MagicSwitchbot
Raspberry 3 Model B -> has Bluetooth Low Energy (BLE) on board
Raspbian Buster -> bluez 5.50
# hciconfig hci0 down
# btmgmt le on
# hciconfig hci0 up
# hcitool lescan
...
34:14:B5:4A:8E:19 MagicSwitchBot
# sudo python3 switchbot_py3.py -d 34:14:B5:4A:8E:19 -c close
Traceback (most recent call last):
File "switchbot_py3.py", line 178, in <module>
main()
File "switchbot_py3.py", line 173, in main
driver.run_command(opts.command)
File "switchbot_py3.py", line 120, in run_command
with connect(self.device, self.bt_interface, self.timeout_secs) as req:
File "/usr/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "switchbot_py3.py", line 51, in connect
format(device, timeout))
ConnectionError: Connection to 34:14:B5:4A:8E:19 timed out after 5 seconds
cannot connect to the device no matter what I do. I also tried gatttool, nothing works...
Have you got an idea? Is an older version of the bluetooth stack needed (stretch?)?
Ubuntu 18.04
`
$ sudo python3 switchbot_py3.py --device C7:96:B8:○○:○○:○○
Connected!
Traceback (most recent call last):
File "switchbot_py3.py", line 109, in
main()
File "switchbot_py3.py", line 104, in main
driver.run_command('press')
File "switchbot_py3.py", line 56, in run_command
return self.req.write_by_handle(self.handle, self.commands[command])
RuntimeError: Characteristic value/descriptor operation failed: Invalid handle
$ sudo python3 switchbot_py3.py --device C7:96:B8:○○:○○:○○ Humiture
Connected!
Traceback (most recent call last):
File "switchbot_py3.py", line 109, in
main()
File "switchbot_py3.py", line 104, in main
driver.run_command('press')
File "switchbot_py3.py", line 56, in run_command
return self.req.write_by_handle(self.handle, self.commands[command])
RuntimeError: Characteristic value/descriptor operation failed: Invalid handle
`
Switchbot allows you to set a Password, could you add this so a password can be used?
I think this is related to issue #7
My Switchbot needs to hold a button down for 8 sec or more to shut down a device.
The same Switchbot needs to hold the same button down for 5 sec or less to power up the device.
If the Press hold time is set long enough to shut down the device, then the Switchbot can not power up the device; the device will simply stay down.
It would be great if the Switchbot API could be made available for those who would like to extend this program.
Thank you.
Hi,
i'm using the script python to open\close my curtain, but i want also to pause it. Is it possible?
Which are the command to send over BLE?
Thanks
Please give the ability to use this on macOS and give directions to do so rather than just Linux.
Although the same Bot can be controlled successfully via the app no response occurs when using sudo python switchbot.py. Note that the device is discovered and connects just fine. It's just that nothing happens on the Bot (no Press).
After many hours of trying various methods to send this command via BLE I have come to the conclusion that this recent firmware has changed the command structure as even this simple script will not work:
https://gist.github.com/mugifly/a29f34df7de8960d72245fcb124513c7
pi@raspberrypi:/python-host $ sudo python switchbot.py
Usage: "sudo python switchbot.py [mac dev_type cmd]" or "sudo python switchbot.py"
Scanning...
Scan timeout.
(' 0', [u'd9:31:3b:81:03:56', 'Bot', 'Press'])
Input the device number to control:0
[u'd9:31:3b:81:03:56', 'Bot', 'Press']
Preparing to connect.
Connection successful.
Complete
Bot firmware version 6.3
No response
sudo python3 switchbot_py3.py -s
Scanning for bluetooth low-energy devices
Discovering Switchbot services
* Found Switchbot service on device F1:E2:0A:F3:6C:56 handle 13
Found 1 devices: ['F1:E2:0A:F3:6C:56']
Enter the number of the device you want to control:
0 F1:E2:0A:F3:6C:56
0
Connected!
Traceback (most recent call last):
File "switchbot_py3.py", line 178, in <module>
main()
File "switchbot_py3.py", line 173, in main
driver.run_command(opts.command)
File "switchbot_py3.py", line 122, in run_command
return req.write_by_handle(self.handles[command], self.commands[command])
gattlib.GATTException: Characteristic value/descriptor operation failed: Invalid handle
sudo python3 switchbot_py3.py -d F1:E2:0A:F3:6C:56 -c open
Connected!
Command execution successful
sudo python3 switchbot_py3.py -d F1:E2:0A:F3:6C:56 -c close
Connected!
Command execution successful
Yet nothing happened, is there supposed to be error handling here ?
I think if I go out through the door, the going out counter is increased, and if I enter in, the entry counter is increased.
But when I did that actions and read the broadcast message, the counters were not increased.
At that time, I received the push notification which sent when the counter sensor detect the action of going out or enter in.
Therefore, I think the sensor detects my action correctly but counters do not working.
Is this wrong usage of counter?
Many of the executables in the old Bluez package have now been deprecated (see https://www.spinics.net/lists/linux-bluetooth/msg70489.html).
This includes the executables used to interact with the bluetooth stack in this script as well, such as hciconfig (replaced by btmgmt), gatttool (replaced by btgatt-client) and hcitool (replaced by direct DBus call) - see https://wiki.archlinux.org/index.php/bluetooth#Shell_command_is_missing_from_bluez-utils for more details about the mapping.
Although I strongly disagree with the decision of deprecating something before the new alternative can cover all the features provided by the old implementation (bgmgmt and bluetoothctl require some dirty piping to do the same things that were previously possible with a single hciconfig line, hcitool has been killed and replaced by a way less intuitive call to the DBus API), it's also good to keep in mind that if this script isn't updated to the new Bluez style then it'll soon stop working on most of the Linux devices.
The version of Bluez compiled on Raspbian still includes the deprecated executable for back-compatibility reason (and Raspbian is the only Linux distro I know where the script would run now), but when they decide to drop the deprecation support then the only way to run the script would be to compile an old version of Bluez on your system, which is quite far from ideal.
Two alternatives:
Replace the existing references to hciconfig, hcitool and gatttool with btmgmt, DBus and btgatt-client.
Replace the references to those executables with PyBluez. It'd probably be the most elegant solution to get a completely native Python code without relying on the presence of specific executables on the system. This would also make the script compatible with any system that supports PyBluez, not only Raspbian.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.