Hello will be in charge of the 16th day, the web service programming course mentor Mizu! Thank you!
In this article I would like to write about how to make a smart home using Ruby!
I don't think it has become a major player in Japan yet, but smart homes are homes that have become easier to live in using "IoT" and "AI." Smart speakers (Google Home, Amazon Alexa, HomePod, etc.) are also part of smart homes as they are more convenient with traditional speakers equipped with IoT/AI.
In this article, I would like to be able to control home appliances using smartphones and smart speakers!
Well, I decided to do it. It is the control of existing home appliances. So what kind of technology should we use?
Since I want to control using a smart speaker, I think the following options will come out.
Both are compatible with the control of home appliances. I have a lot of Apple products around me, so I'd like to be able to control them with Apple's HomePod!
The HomePod uses a mechanism called HomeKit to control home appliances. The mechanism of this HomeKit is open to the public, and anyone can make it. Life is Tech!'S Web Services Programming Course uses Ruby for programming. So let's actually implement HomeKit using Ruby!
This article is based on the assumption that it will be developed on a Mac. I think it can be developed on Windows and Linux, but I think the procedure is different, so I would like you to try it yourself.
Let's prepare by typing the following command in the terminal.
$ mkdir <Project name> #Create folder $ cd <Project name> #Move to folder $ bundle init #Command to create Gemfile $ code . #Command to open VS Code
Now, VS Code will start. (If it doesn't start, start VS Code and open the folder you just created. The folder should be in your user folder!)
This time I want to use a library called
ruby_home, so let's add the following to the Gemfile.
After adding, execute the bundle command in the terminal of VS Code to install the library.
Now you are ready for development! !!
Create a file called app.rb and write the following code.
require 'ruby_home' accessory_information = RubyHome::ServiceFactory.create(:accessory_information) fan = RubyHome::ServiceFactory.create(:lightbulb) fan.on.after_update do |status| if status puts "Lighting turned on" else puts "The lights are off" end end RubyHome.run
If you run it with the
ruby app.rb command, you should see something like this!
$ ruby app.rb Please enter this code with your HomeKit app on your iOS device to pair with RubyHome ┌────────────┐ │ 766-75-746 │ └────────────┘
Launch the iPhone Home app and add accessories.
It will appear like this, so press "No code or cannot scan".
Then, it will find a compatible device that is connected to the same Wi-Fi. Tap RubyHome!
An alert will appear, but proceed without worrying about it.
From here, just repeat the continue button and it's OK!
If you can add it safely, tap the light bulb mark Ruby Home!
Did you turn it on like this? If you look at the terminal, you should see on / off like this.
$ ruby app.rb Please enter this code with your HomeKit app on your iOS device to pair with RubyHome ┌────────────┐ │ 766-75-746 │ └────────────┘ Lighting turned on The lights are off Lighting turned on The lights are off
I wrote an explanation in the comment, so please read it.
require 'ruby_home' #Library load accessory_information = RubyHome::ServiceFactory.create(:accessory_information) #It's an accessory! Added necessary functions to say fan = RubyHome::ServiceFactory.create(:lightbulb) #Added lighting function fan.on.after_update do |status| #When the lighting is turned on and off if status #True if on, False if off puts "Lighting turned on" else puts "The lights are off" end end RubyHome.run #Start-up
It can be achieved with a fairly short code like this! You can make a smart home by writing the code that actually controls the lighting in the if statement in this!
Now, let's control the lighting from here. It seems that there are some lights that can use API, but since the lighting in our house is very ordinary lighting, we will control it with infrared rays.
That's why I made a module like this. It is a device that can transmit infrared signals based on the chip (development board) called ESP32. It was okay to communicate via USB, but I wanted to place it anywhere in the room, so I connected it to Wi-Fi and made it possible to emit infrared rays via the API.
Basically, the code is as follows. Since the method of infrared control varies greatly depending on the device, we recommend that you investigate and implement it yourself! https://gist.github.com/mizucoffee/912bb2032e21568821b8ddedda13d99d
require 'ruby_home' require 'net/http' require 'uri' on = "p344A9034A4" off = "p344A90F464" def send_ir(code) #Code that calls the API uri = URI.parse('http://192.168.101.200/send?data=' + code) req = Net::HTTP::Post.new(uri) http = Net::HTTP.new(uri.host, uri.port) http.request(req) end accessory_information = RubyHome::ServiceFactory.create(:accessory_information) fan = RubyHome::ServiceFactory.create(:lightbulb) fan.on.after_update do |status| if status puts "Lighting turned on" send_ir on #Code that sends a signal to turn on else puts "The lights are off" send_ir off #Code that sends a signal to turn on end end RubyHome.run
Well, you're ready! Let's actually move it!
It worked like this! !!
In this article, I actually developed an app that supports HomeKit using Ruby and tried to control the lighting. I think it was pretty easy! It's good to buy a compatible device and make a smart home, but it's also a good idea to make it yourself.
From here on, it's my story, but I usually develop with the motto "Comfortably take care of familiar things!" This includes yourself, your family and friends, and that's all your mentors and members! This smart home is mainly for my own use, but it can be used by my family, or maybe even in the office.
Until now, I have made a sensor to notify that I got out of bed for family care and a service to back up Twitter followers and followers, but if I have some development power, I will do it like this Can make you happy!
I think that each person has a different purpose for development, but I definitely want you to take on the challenge of development that makes you and others happy!
Thank you for reading to the end! It seems that Tsutchi (@taznica) will write something tomorrow! Please look forward to it! !!
I also made a curtain pic.twitter.com/ qBQmeRlejE& mdash; Mizu @LiT! (@Lit_mizu) December 24, 2020