使用Arduino IRremote库红外遥控家里的电器

1272

最近众筹网站上出现了不少所谓的智能家居控制器,其核心功能大概有二。
一是,控制电源通断,说白了就是arduino控制继电器,这个大家都会;
二是,收录家里各电器的遥控器的红外信号,然后控制电器。
本帖就是教大家做一个Arduino万能遥控器(希望卖万能遥控器、红外智能家居的朋友不要恨我…)

这里我们就讲讲这个红外信号收录的功能。
这里我们要用到Arduino的IRremote 库,其下载地址如下:


https://github.com/shirriff/Arduino-IRremote

使用到的硬件有:  Arduino、38K红外接收模块红外发射模块、屌丝空调、屌丝专用空调遥控器(也可以是电视或者其他的红外控制的电器)

 

下面正式开始了,我将以自己的屌丝空调为例。(补充下空调是长虹的1999元的空调,具体啥型号忘了,效果奇差)

和其他红外遥控项目一样,我们需要先解码遥控器,去年我用遥控器解码发送,老是不成功,一直没找到原因,不过最近在IRremote 库作者的blog上,我看到了这么一句话:
<ignore_js_op>

这位网友的意思,大致是说 大多数空调遥控器编码都是100位以上,甚至200位。

我们再来看看IRremote 库 能接收多少位,在IRremote.h中你可以找到如下语句:

#define RAWBUF 100 // Length of raw duration buffer

不给力啊,居然才100位,果断修改成

#define RAWBUF 255

现在我们再使用如下程序获取红外原始编码。

// 奈何哥哥教你推倒Arduino-红外遥控~
#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); 
}

void dump(decode_results *results) {
  int count = results->rawlen;
  Serial.print("Raw (");
  Serial.print(count, DEC);
  Serial.print("): ");

  for (int i = 0; i < count; i++) {
    if ((i % 2) == 1) {
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    } 
    else {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(" ");
  }
  Serial.println("");
}

void loop() {
  if (irrecv.decode(&results)) {
    dump(&results);
    irrecv.resume(); 
  }
}

 

下载该程序到Arduino后,我又用我的屌丝遥控按了按。
串口输出了如下信息:

Raw (244): -23424 8300 -4200 500 -550 550 -1600 550 -1600 550 -500 600 -1550 550 -600 500 -1600 550 -550 550 -1600 550 -1600 550 -1600 550 -500 550 -1650 500 -1650 500 -1650 500 -550 550 -550 550 -550 500 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -1600 550 -550 500 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -500 550 -1650 500 -550 550 -550 550 -550 550 -1600 550 -550 500 -550 550 -550 550 -550 500 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -550 500 -600 500 -550 550 -550 550 -550 500 -600 500 -550 550 -550 550 -550 500 -600 500 -550 550 -550 500 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -500 550 -600 500 -550 550 -550 550 -550 500 -550 550 -550 550 -550 550 -550 500 -600 500 -550 550 -550 500 -600 500 -550 550 -550 550 -500 550 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -550 500 -600 500 -550 550 -500 600 -550 500 -550 550 -550 550 -550 500 -600 500 -550 550 -550 550 -500 550 -550 550 -550 550 -550 550 -550 500 -550 550 -550 550 -550 500 -600 500 -550 550 -1600 550 -1600 550 -1600 550 -1550 600 -550 500 -550 550 -550 550

这一长串就是我的屌丝空调开关键的原始编码,有长度有244,如果你疯狂的按了很多次案件,你会发现,编码的第一位是一个不定值。这是一个无关紧要的值,可以直接去除,然后我们可以把这段编码复制到记事本,然后除去其中的减号,并把空格替换成逗号。最后用一个数组保存这段编码数据。如下:

unsigned int buf[243]=
    {8300,4150,550,600,500,1550,600,1600,500,600,500,1600,550,600,500,1550,600,500,550,1650,500,1600,550,1600,550,550,550,1550,550,1650,500,1650,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,600,500,550,500,1600,550,550,550,550,550,550,500,550,550,550,550,600,450,600,450,600,550,1600,550,550,500,600,500,550,550,1600,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,500,600,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,1600,550,1600,550,1600,550,1600,550,500,550,550,550,550,550
};

然后我们可以使用如下程序,发送这个编码:

//奈何哥哥和你一起逆袭Arduino~红外控制
#include <IRremote.h>

IRsend irsend;
unsigned int buf[243]=
    {8300,4150,550,600,500,1550,600,1600,500,600,500,1600,550,600,500,1550,600,500,550,1650,500,1600,550,1600,550,550,550,1550,550,1650,500,1650,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,600,500,550,500,1600,550,550,550,550,550,550,500,550,550,550,550,600,450,600,450,600,550,1600,550,550,500,600,500,550,550,1600,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,500,600,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,500,600,500,550,550,550,550,550,500,550,550,1600,550,1600,550,1600,550,1600,550,500,550,550,550,550,550
};

void setup()
{
}

void loop() {
  irsend.sendRaw(buf,243,38);
  delay(3000);
}

 

是的,发送就是使用sendRaw()这个函数。其中三个参数分别是 数组名,数组长度,红外信号频率
我们使用的大部分红外遥控信号频率都是38K,所以这个参数填个38就行~
编译并下载
现在把红外发射头对着关闭的空调,就可以打开空调啦。
在这个超级超级超级超级热的夏日~吹着空调写着程序真是享受。

最后,需要注意的是,虽然是开关键,但实际打开和关闭,是两个编码,这里的编码是开空调的编码。空调的调温按钮,通常每一个温度都对应了一个按键编码,因此如果你要调温,就要使用之前的程序,把各个温度对应的编码都记录下来。

现在你可以自己尝试做个万能遥控器了,先记录按键编码,再发送编码。更可以结合wifi、蓝牙等模块,做出此类家庭家居控制器系统。

留下一个答复

Please enter your comment!
Please enter your name here