I tried to operate home appliances by holding a smartphone over the NFC tag

Introduction

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.

What to prepare

Overall flow

Roughly speaking, the final overall configuration looks like this: image.png

Advance preparation

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

Raspberry Pi setup

First, set up the Raspberry Pi.

Allow SSH to 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.

Get the IP address of the Raspberry Pi

arp -a | grep raspberry-pi-home.lan

SSH to Raspberry Pi

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. image.png

Enable nature_remo_api on 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

Ruby setup

Next, we will set up Ruby. If you already have Ruby 3.0.0 running, go to Install nature_remo_api.

Install rbenv

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

Added rbenv settings to .bashrc

.bashrc


PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Load .bashrc

source ~/.bashrc

Install ruby-build

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Install & configure Ruby via rbenv

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.

Confirm that it is displayed in the list.

rbenv install --list-all | grep 3.0.0
If it does not appear in the list
Update rbenv and ruby-build.

Update rbenv

cd ~/.rbenv
git pull

update ruby-build

cd ~/.rbenv/plugins/ruby-build
git pull

Install Ruby 3.0.0

rbenv install 3.0.0

Specify the Ruby version of the current directory

rbenv local 3.0.0

Install nature_remo_api

gem install nature_remo_api

Set Nature Remo access token in environment variable

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.

Get infrared rays of home appliances you want to operate

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.

Create automation

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!

At the end

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

I tried to operate home appliances by holding a smartphone over the NFC tag
[Java] I tried to make a maze by the digging method ♪
I tried to decorate the simple calendar a little
I tried to implement the like function by asynchronous communication
I tried to build the environment little by little using docker
I tried to illuminate the Christmas tree in a life game
I tried to explain the method
I tried connecting the dot counter to the MZ platform by serial communication
I tried to summarize the methods used
I tried to implement the Iterator pattern
I tried to summarize the Stream API
A story when I tried to make a video by linking Processing and Resolume
What I tried when I wanted to get all the fields of a bean
[Small story] I tried to make the java ArrayList a little more convenient
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to develop a man-hour management tool
I tried to set tomcat to run the Servlet.
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I want to go back to a specific VC by tapping the back button on the NavigationBar!
A Java user over a dozen years ago tried to study the functions of Java8 (Generics).
I tried to create a log reproduction script at the time of apt install
[Rails 6.0] I implemented a tag search function (a function to narrow down by tags) [no gem]
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
[VBA] I tried to make a tool to convert the primitive type of Entity class generated by Hibernate Tools to the corresponding reference type.
I tried to organize the cases used in programming
I tried to develop a ramen shop sharing website.
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried to implement the Euclidean algorithm in Java
I want to add a delete function to the comment function
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried a puzzle that can only be solved by the bottom 10% of bad engineers