[PYTHON] How to easily operate IOT home appliances from Siri by API hacking

Introduction

IOT home appliances that can be controlled from a smartphone through Wi-Fi are convenient, aren't they? But It's also a hassle to open a dedicated app for each home appliance every time. .. Moreover, as the number of home appliances increases, so does the number of apps. .. .. (´Д `)

simply, ** I want to operate all the home appliances in my house just by calling Siri! **

At least, I want to operate it with just a few taps from the apple Watch. We are planning to integrate IOT home appliances into Siri.

Rough flow

In this article, I will explain roughly in the following flow.

  1. Find out how the app interacts with IOT appliances
  2. Control IOT home appliances from a browser, free software, or IDE API tools and check their behavior.
  3. Code the control program
  4. Set up for use with Siri (using Raspberry Pi)

Click here for GitHub repository ↓ Github:akiraset/daikinCleaner

environment

The following environment is assumed.

--RaspberryPi: To make homebridge resident and make it a HomeKit hub --iPhone: Operate the IOT home appliance app and use it with mitmproxy ――AppleWatch: It's fun to operate ♪ It's easy to operate from the futon when you go to bed at night. (IPhone can be used as a substitute) --Mac: PC, Linux is also acceptable, but please read the installation method as appropriate.

Review: What are so-called IOT home appliances?

"Internet Of Things Things are connected to the Internet" That's explained in Chimata, but in a nutshell, the current IOT home appliances ** Home appliances that can be controlled by a smartphone app via Wi-Fi! ** I think this is one word for the time being.

As a feature, manufacturers provide apps for controlling home appliances. Air purifier → App A TV → App B Air conditioner → App C ︙ Ah ... the number of apps is increasing steadily. If you try to operate 10 home appliances, you have to install 10 apps and use them properly, which is a complicated and mysterious situation. There seems to be a move to unify, but it will take some time.

Hmmm, after all it is troublesome (´Д `) As I mentioned at the beginning, I just want to turn it on and off from Siri and apple Watch.

Interaction between IOT appliances and apps: Are IOT appliances controlled by API?

I noticed one day The fact that home appliances can be controlled when connected to the same network via Wi-Fi means Isn't it simply controlled by exchanging with API? ** If you know the API specifications, can you control it programmatically without using an app ?! **

However, there are two types of IOT home appliances that require / do not require an application and authentication, so be careful. From the conclusion, With authentication, it seems difficult to control from the program. Perhaps the app and the home appliance are communicating using the authentication key. Since the authentication key you are using cannot be obtained, it may be difficult to control it programmatically. As an example of my house, the NORITZ water heater is an IOT home appliance, but it requires certification. I searched for Iloilo, but my skills did not allow me to control the NORITZ water heater from the program. IMG_7425.png If you think about it carefully, it's too dangerous for a water heater that handles gas and fire to easily take control. As expected, NORITZ! That's why it's ** IOT appliances that don't require authentication to hack. ** **

Authentication-free IOT appliances: Daikin air purifier

I found an IOT home appliance that meets the conditions at my home. "Daikin Humidifying Streamer Air Purifier MCK70W" IMG_7423.png

This time, we will proceed based on this home appliance.

Find out API specifications with IOT home appliances

First of all, it is necessary to understand what API specifications are used for communication.

The first thing to do is to look for the API spec on the manufacturer's site first. If the manufacturer publishes the API specification, it is the most reliable and easy to refer to it.

Unfortunately, few manufacturers publish API specifications for IOT home appliances, so in that case you will have to search for them yourself. How ??: thinking:

If you can get into the middle of the control app and IOT home appliances and take a peek at the contents of the API. .. That way, you can check the API content you want to operate. You don't have to know all the API specifications, you just need to know only the functions you want to control. Also, it would be good if the app could actually give an instruction and confirm the instruction content.

Use mitmproxy

The perfect tool is mitmproxy. The name stands for "man in the middle proxy".

mitmproxy is a program written in python, and is a tool that is often used to check request and response by entering between the application and the server, mainly in smartphone application development.

For details, please refer to the following article ↓ It is a little old, but it is organized in an easy-to-understand manner. Introduction to mitmproxy for mobile app developers

What you do with mitmproxy is

  1. Install mitmproxy on Mac
  2. Set iPhone to proxy to communicate with Mac
  3. Actually issue a command with the application and check the request and response on the Mac in the middle

Then, I will briefly introduce the introduction method.

Preparing mitmproxy

First is the setup.

install To install it on your Mac, use pip. Xcode is required for installation.


#install
pip install mitmproxy

iphone preparation

Next, on the iPhone side, set mitmproxy running on Mac to be accepted as a proxy server. Select Settings> Wi-Fi> Connected Network> HTTP Proxy on your iPhone. Set the following contents.

--Server: Mac IP address --Port number: 8080 (default) --Authentication: Off

If you can, start mitmproxy on the Mac side.

mitmproxy

After starting on Mac, it is necessary to obtain the certificate of mitmproxy on the iPhone side, so Return to your iPhone again. Open your iPhone browser and go to the http://mitm.it address. IMG_7431.PNG

Select the Apple mark and install the certificate. Now you are ready to use mitmproxy.

Actually, check the API from the application with mitmproxy.

Now, let's issue a command from the app.

・ Daikin's app ↓ Try running the humidification mode. IMG_7430.PNG

・ I was able to confirm the receipt of the request with mitmproxy ↓ スクリーンショット 2020-03-08 22.00.33.png

Yes, I got it! In the same way, check the functions you think you need.

development of

Once you have grasped the minimum required API specifications, you can write a control program that uses the API. It is recommended to code while checking the movement to see if the API confirmed by mitmproxy can really control IOT home appliances, as it is easy to understand.

Let's check the behavior of home appliances using API

You can check by hitting the API from the browser, but it is very inefficient, so we recommend using the API confirmation tool. I'm using IntelliJ's API tools. If you're not using JetBrains, use features similar to your IDE, and you'll probably have one. If you don't have one, the free software Postman is famous for its ease of use. Postman Download

The following is a description of the ʻIntelliJ: RESTful Web Service Test` tool. I think other API tools are similar, so please read them.

・ Command the air purifier to start in humidification mode スクリーンショット 2020-03-06 10.38.56.png

・ Get sensor information from the air purifier スクリーンショット 2020-03-06 10.37.14.png

-You can also check and execute the history retroactively. スクリーンショット 2020-03-06 10.35.50.png There is a message "This REST client is ~", but there is no problem and you can use it normally. I will explain briefly.

---- Method: GET / POST etc. --Host / Port: Target IP address (in this case, the IP address of the IOT home appliance) --Path: Since it is an API, this path content will differ depending on the processing content. Here is important. --Request parameters: Add the required parameters. Add with +, delete with -. You can change it by double-clicking. --Green in the upper left : Execution. The request will be sent. (IOT home appliances behave) --Request body: You can use the saved json file as a parameter in text, file contents, etc.

other, --Response: You can check the contents of the response. (The reply from IOT home appliances is displayed) --Left arrow: You can go back in history. --Import / export is also possible

If you code the control program while using the API tool, it will make a lot of progress, so please try it.

Control program

It's easy, but I wrote this code roughly.

daikinCleaner.py



import sys
import requests

args = sys.argv
if len(args) == 1:
    print('An argument is necessary.')
    exit()
arg = args[1]

#IP address of the air purifier
url = 'http://192.168.0.10'

#Basic information
basic_info = '/common/basic_info'
#Sensor acquisition
get_sensor = '/cleaner/get_sensor_info'

#Get control
get_control = '/cleaner/get_control_info'
#Control SET
set_control = '/cleaner/set_control_info'

#Set of parameters you want to execute
param_off = {'pow': 0}

param_max = {'pow': 1,
             'mode': 0,
             'humd': 3,
             'airvol': 5,
             }

param_hum = {'pow': 1,
             'mode': 4,
             'humd': 4,
             'airvol': 0,
             }


def control(params):
    return requests.get(url + set_control, params)


print('param: ', arg)

if arg == 'off':
    res = control(param_off)
    text = res.text
elif arg == 'max':
    res = control(param_max)
    text = res.text
elif arg == 'hum':
    res = control(param_hum)
    text = res.text
else:
    text = 'nothing'

print('res: ', text)

--The program itself has arguments. --ʻOff: Turn off the air purifier --max: Maximum humidity, maximum wind power! --hum: Start in humidification mode --/ common / basic_info: You can get basic information such as the model number of the air purifier. --/ cleaner / get_sensor_info: You can get sensor information such as humidity, temperature, and air pollution. Daikin's sensor is excellent, so it seems that we can make something else by acquiring this sensor information. (Periodically acquired and graphed for statistics, or the light turns on when the air becomes dirty w) --/ cleaner / set_control_info`: Use this path to control the air purifier.

Set the control program in HomeKit.

In order to operate with Siri, it is necessary to make Apple's HomeKit always accept program instructions, so make the Raspberry Pi a hub terminal of HomeKit and always stand by.

To register a program in HomeKit, you can easily set it by using homebridge of Node.js library. See previous articles for more information and settings on HomeKit and homebridge. [Philips_Hue is linked with API! ~ Make Raspberry Pi HomeKit](https://qiita.com/akinko/items/58c650f99f25fc7e3cb5#%E3%83%A9%E3%82%BA%E3%83%91%E3%82%A4%E3%82 % 92homekit% E5% 8C% 96% E3% 81% 99% E3% 82% 8B)

This time, I added the following to homebridge.

config.json



  "accessories": [
    {
      "accessory": "CMD",
      "name": "air cleaner",
      "on_cmd": "python3 /home/pi/daikinCleaner/daikinCleaner.py hum",
      "off_cmd": "python3 /home/pi/daikinCleaner/daikinCleaner.py off"
    }
  ]

This time, my skin is dry in winter, so I specified hum as an argument so that it can be turned ON / OFF in humidification mode. When the rainy season comes, I will change the argument and change it to refreshing air: thumbs up_tone2:

After setting up so far, restart homebridge and you're ready to go! I will use it immediately !!

Usability

"Turn on the air cleaner (on!)" → The air purifier operates in humidification mode "Turn off the air cleaner (off)" → The air purifier turns off.

It can also be operated with apple Watch (also with iPhone) Just tap on / off! IMG_7422.png I was so happy that I did it many times in front of the air purifier.

I also added it to the Home scene and enjoyed it as follows. "Good night" → The lights go out & PS4 is turned off & the air purifier is off "Good morning" → Some lights are on & the air purifier starts in humidification mode

My goal is to live a bedridden life, so I will do my best to do so!

in conclusion

This time, I explained with Daikin's air purifier, but I think that other IOT home appliances can also be controlled if authentication is not required using Wi-fi. We will investigate home appliances with stars one by one and incorporate them into the Siri family ... It's fun and exciting just to imagine. (And the respected gaze from my daughter is gathered.)

Thank you for reading to the end.

Recommended Posts

How to easily operate IOT home appliances from Siri by API hacking
Cospa's strongest IoT home appliances! Operate TPLink products from Raspberry Pi
How to operate Linux from the console
How to call Cloud API from GCP Cloud Functions
How to operate Linux from the outside Procedure
[Python] How to easily drop a child process started by multiprocess from another process
How to post a ticket from the Shogun API
How to operate Discord API with Python (bot registration)
How to operate NumPy
How to hide your Google Maps API key from HTML
How to get followers and followers from python using the Mastodon API