[PYTHON] Remotely control the air conditioner from outside (by MQTT)

Caution

Sango used in this article is no longer available because the service has ended. Instead, you need to use something like AWS IoT.

Purpose

Make it possible to operate the air conditioner on the go with as little effort as possible. It costs more than if you make your own with infrared light receiving element + infrared LED because of the omission. Since the line is a condominium type network that cannot be accessed directly from the outside, use MQTT.

What to prepare

  1. Raspberry Pi with raspbian installed (I used 2)
  2. Infrared Remote Control Advance I will price a little. However, it is the key to omission.
  3. USB Mini cable to connect 1 and 2
  4. Smartphone app for MQTT Publish ↑ I used the Android MQTT Dash app. It's pretty easy to use.
  5. Register in sango

Infrared remote control Advance A Windows desktop app made by Bit Trade One, the manufacturer. I think it's a good idea to check the operation and usability once. http://bit-trade-one.co.jp/product/module/adir01p/

procedure

Infrared remote control advanced operation

First of all, let's operate the infrared remote control advance from Raspberry Pi.

1. clone at hand

Create a suitable directory and use git clone as a tool to operate the infrared remote control advance on the command line.

$ mkdir /home/pi/aircon
$ cd /home/pi/aircon
$ git clone https://github.com/Drunkar/bto_ir_advanced_cmd.git

2. Install the required tools

Install libusb-1.0.0. Required to build the tool. [Addition] It seems that libusb-1.0.0-dev is also required. https://qiita.com/Kaz-su/items/93ec120b4bb90de7da2b#comment-2ff5f5b73f0ff8ecea44

$ sudo apt-get install libusb-1.0.0
$ sudo apt-get install libusb-1.0-0-dev
  1. make make. You can now use Infrared Remote Control Advance from the command line.
$ cd bto_ir_advanced_cmd
$ make
$ sudo make install

4. Data conversion of infrared remote control signals

Connect the infrared remote control advance to the Raspberry Pi and file the remote control signal of the air conditioner. Create a heating ON signal, air conditioner stop signal file, etc.

** [Start receiving] **

$ bto_advanced_USBIR_cmd -r

(Press the button on the remote control toward the infrared remote control advance.)

** [End of reception] **

$ bto_advanced_USBIR_cmd -s

** [Write signal to file] **

$ bto_advanced_USBIR_cmd -g | tee data.txt

5. Send script creation

Create a script for sending. (I borrowed it from someone's page, but I forgot ... I'm sorry)

$ vi send_ir

send_ir


#!/usr/bin/python3
import os
import sys

def send(code):
    os.system('/usr/local/bin/bto_advanced_USBIR_cmd -d ' + code)

if __name__ == '__main__':
    argv = sys.argv
    try:
        filename = argv[1]
        if os.path.isfile(filename) is True:
            f = open(filename, 'r')
            code = f.read()
            send(code)
            sys.exit(0)
        else:
           sys.stderr.write('File not found.\n')
           sys.exit(-1)

    except (OSError, IndexError):
        sys.stderr.write('File not found.\n')
        sys.exit(-1)

6. Infrared remote control signal transmission

Infrared remote control signal transmission with the created script

$ ./send_ir data.txt

Did the air conditioner work? In other words, if you can execute " ./send_ir data.txt "remotely, you can operate the air conditioner from outside.

Remote control

Use MQTT to enable remote control over the Internet. (The explanation of MQTT is omitted ...)

1. Check connection information

Enter the sango console and make a note of the MQTT connection information. image.png

2. Install the required libraries

Install the python mqtt library.

$ pip install paho-mqtt

3. Subscribe script creation

Create a script to subscribe to MQTT based on the MQTT connection information. ʻAircon_heat_on message to signal heating on It is a sample that sends a signal to stop the air conditioner with the message ʻaircon_off.

$ vi subscribe.py

subscribe.py


# coding:utf-8
import paho.mqtt.client as mqtt
import commands

def on_connect(client, userdata, flags, rc):
        print('Connected with result code '+str(rc))
        client.subscribe("[topic]")

def on_message(client, userdata, msg):
        print(msg.topic + ' ' + str(msg.payload))
        if msg.payload == "aircon_heat_on":
                print "aircon heat ON!"
                print commands.getoutput("./usbir_send ./aircon_heat_on.txt")
        if msg.payload == "aircon_off":
                print "aircon OFF!"
                print commands.getoutput("./usbir_send ./aircon_off.txt")

if __name__ == '__main__':

        username = '[username]'
        password = '[password]'
        host = 'lite.mqtt.shiguredo.jp'
        port = 1883

        client = mqtt.Client()
        client.on_connect = on_connect
        client.on_message = on_message

        client.username_pw_set(username, password=password)
        client.connect(host, port=port, keepalive=60)
        client.loop_forever()

4. Run subscribe script in the background

Keep subscribe.py running Even if ssh is cut with $ nohup [hogehoge] &, hogehoge will continue to work in the background. It is amazing.

$ nohup python subscribe.py &

5. Send air conditioner control command

From MQTT Dash, connect to sango and send a message to [topic] " aircon_heat_on " or " aircon_off "

Did the words "aircon heat ON!" Or "aircon OFF!" Appear on the Raspberry Pi console and the air conditioner worked? Now you can turn on the air conditioner before you go home. Is it the best? I think it can be applied to various things such as TV, so please try making various things.

in conclusion

――If you are interested in the mechanism of the infrared remote controller, you should prepare an infrared receiver, infrared LED, and breadboard and experiment. --Please note that the non-advanced infrared remote control probably has a different command line tool for sending and receiving. --If the infrared code is long, it seems that it cannot be sent by the method of $ bto_advanced_USBIR_cmd -d'cat data.txt'. Fujitsu's air conditioner control signal could not be sent. Therefore, there is a section that uses the script of Infrared Remote Control Advanced Operation-Step 5.

Recommended Posts

Remotely control the air conditioner from outside (by MQTT)
Control the Matrix LED panel from ROS
How to access the Datastore from the outside
How to operate Linux from the outside Procedure