Control your Air Conditioning with a Raspi

In this short tutorial we are going to see how to configure a Raspberry Pi 4B (4GB of RAM) to control our air conditioning (any other element that is controlled by an Infrared) automatically to maintain a room -in our case a DPC- always at the right temperature and it will also allow us to turn on, turn off or vary the temperature remotely.

Stuff that we will need:

*Note that the IR Remote Shield hat needs to have jumper SJ1 soldered as shown in this tutorial. There are some IR Remote Shields that come with that soldered jumper but in my case, I had to attach it with a bit of tin. Having a soldering iron and tin is done in 20 seconds.

Once this is done we start with the programming. We will use LIRC, although the configuration is a bit tricky once the codes are read, it works without problems and in a stable way.

We update the Operating System, in our case Raspbian OS and install LIRC.

sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt-get install lirc -y

Bootloader Configuration

Now we have to update the linux bootloader so that it loads the IR modules. First we open the file that we have to modify:

sudo nano /boot/config.txt

And now we have to modify the following:

# Uncomment these lines to enable IR Communication 
# We change PIN 17 to 18
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

dtoverlay=gpio-ir,gpio_pin=18

#De momento el envio de señales lo comentamos para recibir las señales sin 
#problema. No es obligatorio pero si falla nos ahorraremos este paso
#dtoverlay=gpio-ir-tx,gpio_pin=17

Hardware LIRC Module Configuration

Let’s configure the LIRC module. To do this we first open the file:


sudo nano /etc/lirc/hardware.conf

And add the following lines:

LIRCD_ARGS="--uinput --listen"
LOAD_MODULES=true
DRIVER="default"
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

Next we are going to modify the LIRC configuration. For which we open:


sudo nano /etc/lirc/lirc_options.conf

And we comment and add the following lines:

#driver         = devinput
#device         = auto
driver          = default
device          = /dev/lirc0

Once updated we need to restart the operating system so that it loads the new module configuration:

sudo reboot

Once we have restarted, we check that the LIRC service is running correctly:

sudo /etc/init.d/lircd status

La salida de ese comando nos tendrá que indicar que está corriendo y sin errores:

Manual Recording of Air Conditioning IR Signals

We stop the service to be able to receive the signals that the remote control of the Air Conditioning will send us. The idea is to receive these signals and then reproduce them by sending them from our Raspi. To do this we must record as many signals as we want of the type: turn on, turn off, set the temperature to 24 degrees, etc.

We stop the LIRC service:

sudo /etc/init.d/lircd stop

The result has to be this:

Now we run the following command to check that we receive the signals correctly and save them to a file for later use:

sudo mode2 -m -d /dev/lirc0 > test_recepcion.lircd.conf

After sending several signals, if we do it in order and write down the sequence we will be able to create the file to send those signals. For example, I send the power on signal first, then lower the temperature, then raise the temperature, and finally turn off.

That will result in the test_reception.lircd.conf file something like this (this is just the first signal sent from the remote control to the Raspi IR:

Running as regular user peter
 16777215

     3163     1573      412      382      408     1177
      410      383      409      384      410     1175
      410      383      410     1176      411      381
      410      383      409     1176      409     1179
      410     1176      408      383      410     1176
      410      384      409     1177      409     1177
      410     1178      409      384      409      383
      409      383      410      382      410     1177
      410     1176      408      384      409     1177
      410     1179      410      382      409      381
      409     1177      412      380      408      384
      410     1176      410      383      411      382
      409     1176      409     1177      411      382
      410     1176      410     1176      411     1175
      409      385      408      384      409     1176
      411     1176      408     1177      409     1180
      407     1177      410      383      409     1178
      409     1176      409      384      410      383
      409      383      409      384      407      384
      408     1179      408     1176      411     1176
      411     1177      411      381      409     1177
      408     1180      408     1176      410      382
      410      384      409      383      410      383
      409     1176      410      385      408      383
      409      385      408      383      409     1179
      407     1176      411      383      409      383
      410     1178      408     1178      408      383
      410     1180      407      382      410      383
      409     1176      411     1176      410      382
      409      383      409     1178      409   130858-pulse  3206856-space

We will have as many blocks like this for each signal sent. Now we will create a new file called air-conditioner.lircd.conf:

nano air-conditioner.lircd.conf

We need it to have the following format (be very careful with the indentation or it won’t work) so that we can call each signal and execute it from the command line. As you can imagine, the ellipses (…) are to avoid showing a very long file here. I have only left the first block as an example:

begin remote

  name   mitsubishi
  flags RAW_CODES
  eps            25
  aeps          100

  ptrail          0
  repeat     0     0
  gap    20921

  begin raw_codes

    name SWITCH_ON
     3163     1573      412      382      408     1177
      410      383      409      384      410     1175
      410      383      410     1176      411      381
      410      383      409     1176      409     1179
      410     1176      408      383      410     1176
      410      384      409     1177      409     1177
      410     1178      409      384      409      383
      409      383      410      382      410     1177
      410     1176      408      384      409     1177
      410     1179      410      382      409      381
      409     1177      412      380      408      384
      410     1176      410      383      411      382
      409     1176      409     1177      411      382
      410     1176      410     1176      411     1175
      409      385      408      384      409     1176
      411     1176      408     1177      409     1180
      407     1177      410      383      409     1178
      409     1176      409      384      410      383
      409      383      409      384      407      384
      408     1179      408     1176      411     1176
      411     1177      411      381      409     1177
      408     1180      408     1176      410      382
      410      384      409      383      410      383
      409     1176      410      385      408      383
      409      385      408      383      409     1179
      407     1176      411      383      409      383
      410     1178      408     1178      408      383
      410     1180      407      382      410      383
      409     1176      411     1176      410      382
      409      383      409     1178      409

    name TEMP_25
        ...

    name TEMP_26
       ...

    name SWITCH_OFF
     ...

    name SWITCH_ON_24
     ...

  end raw_codes
end remote

Once all the signals have been saved, copy this file to the library folder that LIRC loads when starting the Operating System:

sudo cp ~/air-conditioner.lircd.conf /etc/lirc/lircd.conf.d/air-conditioner.lircd.conf

Now we can call each of these signals from the command line as follows:

# mostras los comandos que tenemos en nuestro archivo
irsend LIST mitsubishi ""

# para encender el aire acondicionado:
irsend SEND_ONCE mitsubishi SWITCH_ON

# para apagar el aire acondicionado:
apagar = 'irsend SEND_ONCE mitsubishi SWITCH_OFF

# poner la temperatura a 25 C
temp_25 = 'irsend SEND_ONCE mitsubishi TEMP_25'

Now with this we can create a simple script with shell, python or the language that you like the most and control the turning on and off of the air conditioning. We will use a simple Python script but first, in order to send these collected signals and once we have recorded all the signals we want, we will have to enable the sending of signals in the Bootloader LIRC module:

sudo nano /boot/config.txt

We uncomment this line:

dtoverlay=gpio-ir-tx,gpio_pin=17

and we restart the Raspi:

sudo reboot

Once the Raspi has been restarted, we will be able to send the signals that we want from the command line. For example with this command we can see all our signals:

irsend LIST mitsubishi ""

Leave a Reply

Your email address will not be published.