Recently, I've been working remotely and spending more time at home. I wanted to eliminate the hassle even a little, so I made it possible to operate home appliances just by holding a smartphone over the NFC tag. It's a bit of a promotion, but I made a gem called nature_remo_api a while ago, so I'm going to use that.
Roughly speaking, the final overall configuration looks like this:
Since it will be used later, I think that it will proceed smoothly if you complete the following two points first.
-Register the operation you want to set for home appliances and NFC tags in Nature Remo official app --Get an access token from here to use Nature Remo's API
First, set up the Raspberry Pi.
After that, in automation, we will SSH to the Raspberry Pi to execute the Ruby code, so first let's SSH to the Raspberry Pi.
arp -a | grep raspberry-pi-home.lan
Specify the acquired IP address and SSH.
ssh pi@<Enter the IP address obtained above>
You will be asked for a password when you run it, so enter it. Here is the screen SSHed to the Raspberry Pi.
First, create a working directory & move to the created directory.
Here, the directory name is ios_automation
.
mkdir ios_automation
cd ios_automation
Next, we will set up Ruby. If you already have Ruby 3.0.0 running, go to Install nature_remo_api.
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
.bashrc
PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
source ~/.bashrc
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
After that, when writing Ruby code, we will proceed on the assumption that Ruby 3.0 is installed
, so I think that it will proceed smoothly if you match the version.
rbenv install --list-all | grep 3.0.0
Update rbenv
cd ~/.rbenv
git pull
update ruby-build
cd ~/.rbenv/plugins/ruby-build
git pull
rbenv install 3.0.0
rbenv local 3.0.0
gem install nature_remo_api
Set the access token obtained by here in the environment variable.
bashrc
#Nature Remo API KEY
export NATURE_REMO_ACCESS_TOKEN='<Obtained access token>'
After setting the above, execute source ~/.bashrc
to reflect it.
Now that nature_remo_api has been set up, we will get the infrared rays of the home appliances we want to operate on the console.
First, run irb
to launch the Ruby REPL.
I will omit the detailed code explanation, but first I will make it possible to hit the API with the following code.
require('nature_remo_api')
NatureRemoApi::Client.configure{|config| config.access_token = ENV['NATURE_REMO_ACCESS_TOKEN']}
client = NatureRemoApi::Client.new
Next, get the registered home appliances. The obtained hash key will be the ID of the home appliance.
client.appliances.to_h{[_1.id, _1.nickname]}
If you find the appliance you want to operate, put it in appliance_id
.
appliance_id = <Home appliance ID>
Next, acquire the operation (infrared) registered in the target home appliance. The obtained hash key becomes the operation ID for home appliances.
client.signals(appliance_id: appliance_id).to_h{[_1.id, _1.name]}
If you find the operation you want to register in the NFC tag, put it in signal_id
.
signal_id = <Operation ID for home appliances>
Next, let's actually operate the home appliances via Nature Remo.
client.send_signal(signal_id: signal_id)
If all goes well, the operation for the registered home appliances should be executed.
Make a note of the operation ID (which was treated as signal_id
above) for future use.
From here, we will create a simple script for operating home appliances. (Please change the file name as appropriate)
hoge.rb
require('nature_remo_api')
NatureRemoApi::Client.configure{|config| config.access_token = ENV['NATURE_REMO_ACCESS_TOKEN']}
client = NatureRemoApi::Client.new
client.send_signal(signal_id: '<ID of the operation obtained earlier>')
Just in case, let's run the script.
ruby ~/ios_automation/hoge.rb
If the registered operation is executed, the preparation on the Raspberry Pi side is complete. Make a note of the above command for later use.
Next, we will create the automation in the "shortcut" so that we can execute the Ruby script we created earlier.
Automation creation
Select "Create Personal Automation"
Select "NFC"
Select "NFC Tag Scan"
When this screen is displayed, hold your smartphone over the NFC tag.
If the reading is successful, a screen like this will be displayed, so set any name.
After setting the name, select "Next"
Select "Add Action"
Enter "ssh" in the search window and select "Run script via SSH"
Fill in the red frame. After entering the command used in the operation check in the input field, select "Next"
Select "Done"
This completes the settings. Let's try holding your smartphone over the NFC tag. If you can operate home appliances, you are successful!
This time, we used NFC tags to make it easy to operate home appliances. I felt that the NFC tag I used this time was a little unresponsive, so I would be grateful if you could tell me if you have a recommended NFC tag! I will continue to improve and have fun ...!
Recommended Posts