-
Notifications
You must be signed in to change notification settings - Fork 12
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
usage #10
Comments
Generally I would recommend to run the application in a setup with Gunicorn and Nginx. After installing everything on the raspberry pi, you should be able to reach the login endpoint: Afterwards you can contact the endpoint which lists all switchbots with the token in the header you received from the login: |
hey @nicolas-kuechler so I managed to run it but its asking for a password now
|
ok so I put in a random password and it says its running on 0.0.0.0:5000. I try to navigate to {{ raspberry pi ip address }}/switchbot/api/v1/login but the page doesnt work. I cannot reach the login endpoint. Can you help please? |
@bachoo786 it is the password that you then later need in order to receive a bearer token on this endpoint: if you already set a password but don't remember it (i.e. if you are not prompted again to enter one when you start), you can remove the switchbot entry in the keyring file which should be at this location: |
you cannot access it with your webbrowser, instead you have to login with a post request. I recommend to use postman (because there is a collection in the repo which you can import) If you want to use curl, this is what you need to send:
|
oh right what I am trying to do is simply make my switchbot press on for 2 mins and then go back to off or its original state. |
ok @nicolas-kuechler the curl command doesnt work either. I set the password and I used it in the curl command, but it says:
do I need to provide name as well? |
I think there is a different problem because you get the connection refused error. What you should see if you open the url with your browser is the following message: What you can try is to change http to https.
I assume the switchbot program is still running, right? Does the cmd output show something? |
@nicolas-kuechler I get the same connection refused error if I change it to https. Please tell me a few things: 1). do I have to continue running this command: the command output doesnt show anything:
also there is no |
@bachoo786 You might need to google for keyring locations or you can search the raspberry pi file system for a keyring_pass.cfg file. |
ok I changed my password to "hello" and I ran the commands please see below:
same issue. what port is this running on? 443? |
can I ask you something? I am also following this person's switchbot command but he doesnt specify any timers for the press action of the switchbot. Here is his python code: Can you please tell me what can I add to the python code to make the press command hold for 2 minutes? I am not familiar with python but learning. |
ohh sorry, I think you need to use port 5000.
|
if you don't need the rest api and you just want a script which pushes the switchbot for 2 min. The following script: from switchbot import Bot
from switchbot import Scanner
scanner = Scanner()
mac_addresses = scanner.scan(known_dict={})
print(f"mac adresses: { mac_adresses}")
bot = Bot(id=1, mac=mac_adresses[0], name="your name")
# change the hold time to 2 minutes (you only need to do this once)
bot.set_hold_time(120)
# press the bot
bot.press() Place it in the folder switchbot/src on your raspberry pi with the name that you want and then execute from the root folder the following command. (you might require sudo because I think the scanner functionality relies on it)
|
So where do I put my mac address of my switchbot? |
from switchbot import Bot
bot = Bot(id=1, mac="MAC_ADDRESS", name="your name")
# change the hold time to 2 minutes (you only need to do this once)
bot.set_hold_time(120)
# press the bot
bot.press() if you have the mac adress, you don't have to scan to find the bot, just replace |
so I used the script that you provided above however where shall i run it from? shall I run it in /home/pi/switchbot/src ? |
ok @nicolas-kuechler I ran the script but it gives me an error that the hold time can only be between 0-60 seconds. I changed in the script to 240seconds.
|
@nicolas-kuechler I changed the time in switchbot.py to 250 seconds. and now I get a new error:
can you please help? |
This check is there because the switchbot only accepts a hold time up to one minute. So you cannot do what you want directly. However, there is a workaround. You can try the following: import time
from switchbot import Bot
bot = Bot(id=1, mac="MAC_ADDRESS", name="your name")
# switch to the dual state mode (allows you to extend the arm with bot.switch(on=True))
bot.set_mode(dual_state=True, inverse=False)
# extend the switchbot arm
bot.switch(on=True)
# wait for 120 seconds
time.sleep(120)
# retract the switchbot arm
bot.switch(on=False)
# switch back to the normal operation mode
bot.set_mode(dual_state=False, inverse=False) The failed connection can happen sometimes, you can wait a bit and then retry. |
ok I tried the above script and now I get this error:
|
@bachoo786 |
Thanks I will try it. Is it possible to have a script without the flask? Because I would have to run the flask all the time using cronjob and then execute the script above. Is there a way where a script can just do the press and hold for 2 minutes? |
the script I wrote for you does not need the flask part |
@nicolas-kuechler i am having connection issues surprisingly but my raspberry pi and the switchbot is in the same room. |
does it sometimes work and sometimes not? or did it never work? If it never worked, you might have used the wrong mac address. (you can find the mac address via the mobile app) Does it always fail at the same step? (e.g. it does extend the arm but not retract it) What firmware does your switchbot have? (you can check in the mobile app) In my setup it works very reliable with this code over quite some distance. |
Well the script that I posted yesterday which you can find above it works perfectly with it I suppose that script is of the official api by switchbot. With your script it doesn't move at all. I have the correct mac address as I got it from the app. The firmware version is V4.5 |
I took the time and tested the script in my own setup and it worked.
Additionally, I also tested if it works to set a hold time above 60 seconds. What my script does is the following, it changes the mode to dual state. I don't know if this is sufficient for your needs. Another option would be to push the switchbot for one minute, then push it for another minute afterwards. |
@nicolas-kuechler Hello mate sorry for the delayed response. 1). Can I run your script in my home folder using the following command: sudo python air.py? All I need is to turn push the switch for 120 seconds and then return back to its original state. So basically it acts like a push switch but with hold. I want to be able to control the hold duration and also when to turn the switchbot on. Many thanks. |
import time
from switchbot import Bot
bot = Bot(id=1, mac="MAC_ADDRESS", name="your name")
# change the hold time to 1 minute (you only need to do this once)
bot.set_hold_time(60)
# press the bot
bot.press()
# wait for 60 seconds
time.sleep(60)
# press the bot
bot.press()
|
Hi Thanks for your reply.
Thanks. |
You can find information on how pipenv works in the documentation: In particular checkout the following; Note that the switchbot library only requires sudo for the scan functionality that you don't require because you already know the mac address. |
Hi , i have any error below but i already installed pygatt . Please advise
|
I recommend installing the switchbotpy module with pip: (this should automatically install the dependencies) Another thing you can try is omitting the sudo because the press_test.py script does not require it anyway. |
Ok , but now i got this
|
Got it to run with pipenv but now BLE connection timeout
|
|
hi , i got it to work . Onboard bluetooth range is really short resulting in timeout. i tried to use usb bluetooth and disable onboard bluetooth to improve range but i got an error . Can't init device hci0: Connection timed out (110) During handling of the above exception, another exception occurred: Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): |
Thanks for writing this app. I have a need to send the "on" command to switchbot multiple times to make sure it worked. I don't want to toggle it. I tried the below code. from switchbotpy import Bot print("turn on...") But aparantly turn_on is not an action, the only action available is switch but then how do I know if its turning it ON or OFF. Any suggestions? |
I just answered my own q.. I used bot.switch(1) to turn it on and bot.switch(0) to turn it off and it worked. Thanks. |
hi there
Thanks for this wonderful application.
However being a novice and trying to get my head around on how to use this application I do not understand the usage section in the readme file.
After installing everything do I just navigate to my raspberry pi's address and go to /switch/......
E.g. 192.168.0.10/switch/.....
The text was updated successfully, but these errors were encountered: