极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12949|回复: 6

arduino通过串口控制空调

[复制链接]
发表于 2014-9-8 22:00:51 | 显示全部楼层 |阅读模式
  1. #include <SoftwareSerial.h>

  2. //Infrared communation infomation
  3. byte InfrInitStart[]={0xAA,0xAA,0x08,0x08,0x00};
  4. byte InfrInitEnd[]={0xCC,0xCC,0x08,0x08,0x00};
  5. byte AirConVerion[]={0x02,0x00,0x2C,0x08,0x26};
  6. //fit the air condition type code 0x2C is 044 (meidi KFR-72LW)
  7. //if you want to control another air condition , you should change 0x2c
  8. byte AirConTrunOn[]={0x04,0xff,0x08,0x08,0xfb};
  9. byte AirConTurnOff[]={0x04,0x00,0x08,0x08,0x04};

  10. //use pin 6 as receivepin and use pin  as transmitpin
  11. SoftwareSerial serial(6,7);

  12. void setup() {
  13.   // put your setup code here, to run once:
  14.   serial.begin(9600);
  15.   Serial.begin(9600);
  16.   Serial.println("use softserial");
  17.   
  18.    delay(1000);  
  19.   //Initialize the infrared mode start
  20.   serial.write(InfrInitStart,5);
  21.   if(checkRespondeCode())
  22.   {
  23.   //Initialize the infread mode end
  24.     serial.write(InfrInitEnd,5);
  25.   }
  26.   else
  27.   {
  28.     exit(0);
  29.   }
  30.   if(checkRespondeCode())
  31.   {
  32.    //set the air condition mode version
  33.      serial.write(AirConVerion,5);
  34.   }  
  35.   else
  36.   {
  37.     exit(1);
  38.   }
  39.   if(checkRespondeCode()==false)
  40.   {
  41.     exit(2);
  42.   }
  43.   
  44.   
  45. }

  46. void loop() {
  47.   // put your main code here, to run repeatedly:
  48. delay(5000);
  49.   //turn on the air condition
  50.   serial.write(AirConTrunOn,5);
  51.   Serial.println("0x04,0xff,0x08,0x08,0xfb");
  52.   //turn off the air condition  
  53.   delay(5000);
  54.   serial.write(AirConTurnOff,5);
  55.   Serial.println("0x04,0x00,0x08,0x08,0x04");
  56.   while(true)
  57.   {}
  58. }

  59. boolean checkRespondeCode()
  60. {
  61.   byte RXData;
  62.   delay(1000);
  63.   //Serial.println("check response code");
  64.   if(serial.available())
  65.   {
  66.     delay(200);
  67.     RXData=serial.read();
  68.     //Serial.println(RXData,HEX);
  69.     if(RXData==0x89)
  70.     {
  71.       //serial.println();
  72.       Serial.println("xiang ying 89");
  73.       return true;
  74.     }
  75.     else
  76.     {
  77.       Serial.println(RXData,HEX);
  78.     }
  79.   }
  80.   Serial.println("no xiang ying");
  81.   return false;
  82. }
复制代码

以上是代码,实验成功。有问题的可以询问
回复

使用道具 举报

发表于 2014-9-9 09:19:55 | 显示全部楼层
空调上有串口吗?配个图吧。
回复 支持 反对

使用道具 举报

发表于 2014-9-9 10:18:16 | 显示全部楼层
这是用软件串口来发射红外信号
回复 支持 反对

使用道具 举报

发表于 2014-9-9 10:18:34 | 显示全部楼层
载波用串口10变化来实现的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-9 20:32:35 | 显示全部楼层
瘦网虫 发表于 2014-9-9 09:19
空调上有串口吗?配个图吧。

串口与红外模块通信 发送二进制数据
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-9 20:33:06 | 显示全部楼层
wxws 发表于 2014-9-9 10:18
这是用软件串口来发射红外信号

是的 避免与0 1端口的冲突
回复 支持 反对

使用道具 举报

发表于 2014-9-18 08:52:02 | 显示全部楼层
发的是NEC码吗?

//fit the air condition type code 0x2C is 044 (meidi KFR-72LW)

这句里面的type是怎么得到的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 01:00 , Processed in 0.040359 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表