IOT device "SwitchBot" which physically switches operation is fully automated by IFTTT link via razpai



SwitchBot is a device that makes the switch into IoT with a simple setting, but as of September 2017 it is difficult to link with the web service such as IFTTT. Therefore, in this article, while emphasizing not to lose simplicity as much as possible, while using the Raspberry Pi (raspberry pie) as a hub of SwitchBot and using the official python library (below) to operate from IFTTT I will write. And in this way I will set the action "Switch on when I get home."

GitHub - OpenWonderLabs / python - host: The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.
https://github.com/OpenWonderLabs/python-host


You can tell what kind of terminal what Switch Bot can do with SwitchBot by following article.

"SwitchBot" review that allows you to remotely control every appliance by turning on / off the switch from the outside or outside via the net - GIGAZINE


This time I checked Gmail and prepared a code to let SwitchBot switch on if there is a mail of the set title (eg "PUSH!"). By using this code, you can operate "SwitchBot" with a specific mail as a trigger. For the judgment of "returning home", IFTTT's "When my own Android smartphone is connected to Wi-Fi at home" is used, and when I return home, IFTTT automatically sends mail from "Home" → "Mail" → I will operate "SwitchBot".

Raspberry Pi gets the Gmail application password needed to read the mail. When 2-step authentication is off, I will turn it on because I can not issue an application password. Click 2-step verification process on account login and security page.


Enter the phone number of the smartphone and click "Next".


Once you enter the code sent to your smartphone etc. via SMS you are done. When you return to the previous screen, click "New" because the "Application password" item has appeared.


Name your application password. As long as you know the name of the app password, you can tell what you are using for later.


An application password was generated. Let's take notes as we will use it later. If you close the browser, you can not reconfirm so you need to delete once and generate again.


Next, use Raspberry Pi to get the MAC address of SwitchBot. Start up the console,

[code]sudo hcitool lescan[/code]

, The raspberry pie will scan and tell you surrounding Bluetooth devices.

[code]XX:XX:XX:XX:XX:XX[/code]

The MAC address will be displayed in the format. If more than one candidate is displayed, you can not check which SwichBot it is, so you can try and check in order. For example, under the situation that there are many Bluetooth devices including three SwitchBots, there are many candidates like the image below.


We will continue working on Raspberry Pi. Download the code in the following URL and open it with a text editor.

(Python source code)
http://i.gzn.jp/img/2017/09/07/switchbot-raspberry-pi/timeline.py

This code scans Gmail's inbox every 30 seconds, checks for words set to PUSH_TRIGGER, HOLD_ON_TRIGGER, HOLD_OFF_TRIGGER in the title of the mail, if it finds the word set it will run SwitchBot, It deletes the mail.
※ It may cause unintentional movement. Please be sure to run with a Gmail account okay to run even if the data disappears.

Code was created with reference to the following site.

Since Switch Bot arrived I tried operating from Single Board Computer - blog
http://xx-prime.hatenablog.com/entry/2017/07/01/211509


Rewrite the fields of GMAIL_USER and GMAIL_APP_PASSWORD just under the comment "## rewrite inside of this" to Gmail address and application password that you set, and rewrite the field of SWITCHBOT_MAC_ADDRESS to the acquired MAC address.

[code]#######この中を書き換える#######

GMAIL_USER = "[email protected]"
GMAIL_APP_PASSWORD = "gmailapppassword"
SWITCHBOT_MAC_ADDRESS = "BT: MA: CA: DD: RE: SS"
PUSH_TRIGGER = "PUSH!"
HOLD_ON_TRIGGER = "switchboton"
HOLD_OFF_TRIGGER = "switchbotoff"
################################ [/ code]


I actually move it. Launch the console and run timeline.py as above.


Because it automatically becomes the standby state, I will send a mail with the title "PUSH!" In this state. As soon as SwitchBot pressed the button, the log was output to the console.


By using this code, you can now use Switch to operate SwitchBot. continueIFTTTWe will set up "send mail to operate SwitchBot when smartphone is connected to a specific SSID". First of allIFTTT's siteAccess. In case of the first time Since the following screen is displayed, click "Continue with Google".


I get a pop-up to connect with my Google Account. Click on the account.


Please check the authority of IFTTT and click on the permission to login.


Select "My Applets" at the top of the screen, then select "New Applet" on the right.


First choose "+ this".


Enter "android" in the search field, click "Android Device" ... ...


Click "Connect". If you are logged into IFTTT with your Google Account, you will be automatically connected.


Since I want to send mail when I connect to my home Wi-Fi, I select "Connects to a specific WiFi network".


Enter Wi-Fi SSID and click "Create trigger" to complete setting. Please note that uppercase and lowercase letters are distinguished.


The trigger was set. Next I will set the action to send mail. Click "+ that".


I will use "Gmail".


If you are logged into IFTTT with a Google Account, you can simply press "Connect" to login.


By choosing "Send yourself an email" you can send an email to yourself.


Write the word that turns on SwitchBot written in timeline.py in the Subject column and click "Create action".


Please reconfirm the setting and press "Finish" below to complete.


The trigger_device function interacts with the SwitchBot itself in the code. As an argument add is the MAC address of SwitchBot, command is set to either "push" "on" or "off". If you are confident in your arms please try to remodel.

[code]def trigger_device(add,command):

print ('Start to control')
con = pexpect.spawn ('gatttool - b' + add + '- t random - I')
con.expect ('\ [LE \]>')
print ("Preparing to connect.")
con.sendline ('connect')
#To compatible with different Bluez versions
con.expect (['\ [CON \]', 'Connection successful. * \ [LE \]>'])
print ('Write command')
if command == 'push':
con.sendline ('char-write-cmd 0x0016 570100')
if command == 'on':
con.sendline ('char-write-cmd 0x0016 570101')
if command == 'off':
con.sendline ('char-write-cmd 0x0016 570102')

con.expect ('\ [LE \]>')
con.sendline ('quit')
print ('Trigger complete') [/ code]

in Review,   Software,   Hardware, Posted by log1d_ts