Let's make a smart home with Ruby!

Introduction

HelloLife is Tech! # 1 Advent Calendar 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!

What is a smart home?

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!

Which technology should I use? (Technology selection)

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!

About the development environment

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.

Preparation

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.

gem 'ruby_home'

After adding, execute the bundle command in the terminal of VS Code to install the library.

$ bundle

Now you are ready for development! !!

Let's try making lighting

Create a file called app.rb and write the following code.

app.rb


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

Code description

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!

Try to link with actual lighting

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

app.rb


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!

IMG_0625.gif

It worked like this! !!

in conclusion

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! !!

Postscript: I also made a curtain

Recommended Posts

Let's make a smart home with Ruby!
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Make a typing game with ruby
Let's make a Christmas card with Processing!
Let's make draw poker with ruby ~ Preparation ~
Let's make draw poker with ruby ~ test-unit preparation ~
Let's make a search function with Rails (ransack)
[Java basics] Let's make a triangle with a for statement
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Let's make a simple API with EC2 + RDS + Spring boot ①
Make electronic music with randomness with Ruby
Make a digging maze with Ruby2D
Make a slideshow tool with JavaFX
Let's use Amazon Textract with Ruby
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
Make Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
Make a note of Ruby keyword arguments
Let's make draw poker with ruby-Implementation 1 (card)-
Make a family todo list with Sinatra
Let's make draw poker with ruby-Implementation 4 (Deck)-
Make a family todo list with Sinatra
I made a risky die with Ruby
Make a login function with Rails anyway
[docker] [nginx] Make a simple ALB with nginx
Extract a part of a string with Ruby
Let's make draw poker with ruby-Implementation 3 (player)-
Make a site template easily with Rails
[Java] Let's make a DB access library!
Let's make draw poker with ruby-Implementation 2 (role)-
Let's make an error screen with Rails
Let's go with Watson Assistant (formerly Conversation) ② Make a hotel reservation chatbot
If you want to make a zip file with Ruby, it's rubyzip.
Let's get started with Java-Create a development environment ②
Let's get started with Java-Create a development environment ①
AtCoder Beginner Contest 169 A, B, C with ruby
Creating a browser automation tool with Ruby + Selenium
Let's create a timed process with Java Timer! !!
Let's make a shopping site using stripe! (Purchase)
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
Make System.out a Mock with Spock Test Framework
Yes, let's make a Minecraft server (Ubuntu 20.04 + Bedrock Server)
I made a portfolio with Ruby On Rails
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
Run Scala with GraalVM & make it a native image
How to make a factory with a model with polymorphic association
How to make LINE messaging function made with Ruby
[Ruby] Generate a concatenated QR code with rqrcode (Knowledge)
Let's make a pseudo model using active_hash ~ Prefecture data ~
Creating a Cee-lo game with Ruby 4th Creating a game progress process
[Ruby] I made a crawler with anemone and nokogiri.
[Personal application work memo] Make a calendar with simple_calendar
[Ruby] Generate a concatenated QR code with rqrcode (Practice)
Let's make a robot! "A simple demo of Java AWT Robot"
Make Nginx of CentOS8 SSL compatible with Let's Encrypt
[Introduction to Android application development] Let's make a counter
Use Coveralls with GitHub Actions in a Ruby repository