您的位置:首页 > 大数据 > 物联网

物联网智能硬件解析之万能空调遥控器实现 Ardunio_通过红外线发射灯遥控空调

2015-09-07 23:12 645 查看
这其实就是空调万能遥控器的原理。

用arduion遥控空调。

首先我做了一下以下几点

(1)首先有一个红外线接收管 IR receiver

(2)红外线发射灯

(3)arduino uno的板子

(4)几条面包线 Jumper cables

(5)空调遥控器

首先自己家中空调是TCL的挂式空调。。。。这个首先说明。  arduino遥控空调和空调牌子有关

(1)硬件的链接如图所示。



(2)图中告诉你如何将红外线接收管和arduino通过面包线相连。

(3)安装好IRremote.h函数库(直接从github中搜irremote进行下载 你要找到如下三个文件 IRremote.ccp IRremote.h IRremoteInt.h ,在 arduino 安装目录下的libraries\arduino_iremote_master)
 注意将函数库放在正确的位置我将他们放到了\Arduino\libraries\RobotIRremote\src中了。。。这个只要能编译不报错就行

(4)因为本人的TCL空调的遥控器编码是228位,所以必须要修改IRremoteInt.h中的三行代码

第一行:

将#define RAWBUF 100

换成 #define
RAWBUF 255

这是因为我的空调遥控器编码是228位,100位太少了。

有人问要是大于255位怎么办?见这个帖子

编码大于255位的解决办法

第二行:

将#define _GAP 5000 

换成#define
_GAP 50000 

这是因为默认的数据是5000,因为遥控器会连续发两段代码,所以5000这个参数不适用,需要改到50000,否则你只能
接受到前一段代码仍然无法控制你家的空调 详情请见

第三行:

将uint8_t rawlen;  变成 int rawlen;

typedef struct {
uint8_t recvpin;           // pin for IR data from detector
uint8_t rcvstate;          // state machine
uint8_t blinkflag;         // TRUE to enable blinking of pin 13 on IR processing
unsigned int timer;     // state timer, counts 50uS ticks.
unsigned int rawbuf[RAWBUF]; // raw data
// uint8_t rawlen;         // counter of entries in rawbuf
int rawlen;
}
irparams_t;


改完之后按一下步骤

调用dump代码了,
1 连线  按照我贴的连接的教程连接好你的红外接收器 ,一般 最左侧的是数据线然后是gnd 和vcc
2 在arduino 中调出IRecvDump 的例程序,可以通过 FILE->EXAMPLES->Arduino_IRremote_master 下找到
3 编译和下载
4 打开com口监控 Tools->Serial Monitor
5 使用你家的空调遥控器对着接收头按一下开关键
6 这个时候串口会跳出你家空调开关的代码

IRecvDump的代码如下

/*
* IRremote: IRrecvDump - dump details of IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
* LG added by Darryl Smith (based on the JVC protocol)
*/

#include <IRremote.h>

/*
*  Default is Arduino pin D11.
*  You can change this to another available Arduino Pin.
*  Your IR receiver should be connected to the pin defined here
*/
int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void dump(decode_results *results) {
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");

}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->address, HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == LG) {
Serial.print("Decoded LG: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else if (results->decode_type == AIWA_RC_T501) {
Serial.print("Decoded AIWA RC T501: ");
}
else if (results->decode_type == WHYNTER) {
Serial.print("Decoded Whynter: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");

for (int i = 1; i < count; i++) {
if (i & 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.write('-');
Serial.print((unsigned long) results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println();
}

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
dump(&results);
irrecv.resume(); // Receive the next value
}
}


串口中获得的数据如下:

将其中的数字部分中的所有负数取绝对值。有些牌子的空调需要将第一个数移到最后一个数后。这个还是要根据空调的牌子的型号。

本人的TCL空调不用移位。

移位示例

去除示例

本人是没有动第一个数。原封不动。



选择其中的一行,将负数改成了正数。

快速修改方式



将硬件如图安装。

将一下代码烧录到arduion中

/*
* arduino遥控空调
*/

//By Yang Shuo
//http://blog.csdn.net/worldmakewayfordream
//Update: Pin 3 IR LED test function

#include <IRremote.h>                  // 引用 IRRemote 函式库
unsigned int rawCodes_ac_close[228]={3000,1600,450, 1100,450, 1100,450, 400,500, 350,450, 350,450, 1100,450, 400,400, 450,400, 1100,500, 1100,450, 400,400, 1100,500, 350,400, 400,450, 1100,450, 1150,450, 400,400, 1100,500, 1100,450, 400,400, 400,400, 1150,400, 450,450, 350,450, 1100,450, 400,400, 400,450, 400,400, 400,400, 400,450, 400,450, 350,450, 400,400, 400,450, 350,450, 400,450, 350,450, 350,450, 400,450, 350,400, 400,500, 350,450, 350,450, 400,450, 350,450, 1100,450, 400,450, 400,400, 1100,450, 1100,450, 1150,450, 400,450, 350,450, 350,450, 400,450, 350,400, 400,500, 350,450, 350,450, 400,450, 350,450, 400,400, 400,400, 450,400, 1100,450, 400,450, 1100,450, 400,400, 400,450, 400,400, 400,400, 450,400, 400,400, 400,450, 400,400, 400,400, 400,450, 400,400, 400,400, 450,400, 400,450, 350,450, 400,400, 400,450, 350,450, 400,450, 350,450, 350,500, 350,450, 350,400, 450,450, 350,450, 350,450, 400,450, 350,450, 400,400, 400,450, 400,400, 400,400, 400,450, 400,400, 400,400, 400,450, 400,400, 1100,500, 350,450, 400,450, 350,450, 400,400, 400,450, 1100,450, 400,400};

IRsend irsend;                          // 定义 IRsend 物件来发射红外线讯号

void setup()
{
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);   // 点亮LED测试(需通过手机摄像模式观察) 你可以开始插个led灯来观察led灯是否亮,如果亮就可以换成红外线发射灯了
delay(3000);             // 等待3秒
digitalWrite(3, LOW);    // 结束测试
}

void loop()
{
Serial.print("SendIR: ");
irsend.sendRaw(rawCodes_ac_close,228, 40);
delay(3000);             // 等待3秒
}


注意sendRaw函数第一个参数是空调的编码,就是arduion串口接收的一大堆数,你只要全部都取绝对值就可以了。

第二个参数是编码的个数。这个在接收时告诉你了。Raw(228):之后一大堆数.....   这样你就不用自己数了。

第三个参数是红外线频率。  主流的红外线频率是40,38,32.  这个你多试几次吧。   每个牌子的空调红外线频率不一样。TCL的是40.

这样你就可以遥控空调了。

下面附一张我的安装后可以调控空调的硬件图。



这样你就可以操纵空调,让空调感受到红外线的操控而出音了。。。

reference:

文章一 

文章二

文章三 

文章四

文章五 

文章六

文章七
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息