arduino通过串口控制空调
#include <SoftwareSerial.h>//Infrared communation infomation
byte InfrInitStart[]={0xAA,0xAA,0x08,0x08,0x00};
byte InfrInitEnd[]={0xCC,0xCC,0x08,0x08,0x00};
byte AirConVerion[]={0x02,0x00,0x2C,0x08,0x26};
//fit the air condition type code 0x2C is 044 (meidi KFR-72LW)
//if you want to control another air condition , you should change 0x2c
byte AirConTrunOn[]={0x04,0xff,0x08,0x08,0xfb};
byte AirConTurnOff[]={0x04,0x00,0x08,0x08,0x04};
//use pin 6 as receivepin and use pinas transmitpin
SoftwareSerial serial(6,7);
void setup() {
// put your setup code here, to run once:
serial.begin(9600);
Serial.begin(9600);
Serial.println("use softserial");
delay(1000);
//Initialize the infrared mode start
serial.write(InfrInitStart,5);
if(checkRespondeCode())
{
//Initialize the infread mode end
serial.write(InfrInitEnd,5);
}
else
{
exit(0);
}
if(checkRespondeCode())
{
//set the air condition mode version
serial.write(AirConVerion,5);
}
else
{
exit(1);
}
if(checkRespondeCode()==false)
{
exit(2);
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(5000);
//turn on the air condition
serial.write(AirConTrunOn,5);
Serial.println("0x04,0xff,0x08,0x08,0xfb");
//turn off the air condition
delay(5000);
serial.write(AirConTurnOff,5);
Serial.println("0x04,0x00,0x08,0x08,0x04");
while(true)
{}
}
boolean checkRespondeCode()
{
byte RXData;
delay(1000);
//Serial.println("check response code");
if(serial.available())
{
delay(200);
RXData=serial.read();
//Serial.println(RXData,HEX);
if(RXData==0x89)
{
//serial.println();
Serial.println("xiang ying 89");
return true;
}
else
{
Serial.println(RXData,HEX);
}
}
Serial.println("no xiang ying");
return false;
}
以上是代码,实验成功。有问题的可以询问 空调上有串口吗?配个图吧。 这是用软件串口来发射红外信号 载波用串口10变化来实现的。 瘦网虫 发表于 2014-9-9 09:19 static/image/common/back.gif
空调上有串口吗?配个图吧。
串口与红外模块通信 发送二进制数据 wxws 发表于 2014-9-9 10:18 static/image/common/back.gif
这是用软件串口来发射红外信号
是的 避免与0 1端口的冲突 发的是NEC码吗?
//fit the air condition type code 0x2C is 044 (meidi KFR-72LW)
这句里面的type是怎么得到的?
页:
[1]