利用arduino红外库遥控美的家用空调

news/2025/1/7 22:48:50/

由于美的空调使用的不是简单的红外编码信号,因此在使用arduino红外库的时候不能直接调用其已有的编码规则,需要先了解美的空调的编码规则,再使用IRsend::sendRaw这个函数直接发送原始数据。本文建立在已经对红外发送有了了解并且搜到了美的空调的编码规则的基础上(编码规则在百度即可搜到https://wenku.baidu.com/view/c46594141ed9ad51f01df2c3.html)

一、编码规则

根据美的的编码规则,L A A' B B' C C' S L A A' B B' C C' O为最普通的开机码,其中L为引导码,A、B、C为决定温度,风速,模式的码,A',B',C'分别为A,B,C按位取反后的码,S为分隔码,O为终止码(均可在上面的链接上查到)

关机码与开机码稍有不同,如果想直接关机,可以直接复制下面的数组buf[300]

{4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,8000};

二、解码函数

如果要把每一个码都直接存起来就有点浪费内存了,其实可变的码也就A,B,C这三个字节,下面的函数实现了输入三个字节A,B,C,就把全局数组buf[200]用持续时间填满,之后直接使用sendRaw(buf,200,38)就可以开机并设置温度,风速,模式了。

void getcode(unsigned char A,unsigned char B,unsigned char C){unsigned char mask=0x80;unsigned int cnt=2;buf[0]=4400;buf[1]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=5220;buf[cnt++]=4400;buf[cnt++]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=8800;}

三、完整代码

需要的硬件电路:

  • 一个按钮,一端接arduino的2号IO口,一端接GND;
  • 一个红外发射头,串上限流电阻,一端接arduino3号IO口,一端接GND

功能:

  • 按钮每次按下,轮流发射开机码(对应某个温度、风速)与关机码
#include <IRremote.h>unsigned char flag=1;
const unsigned char LED = 13;
const unsigned char Btn =2;
const unsigned char IR = 3;
unsigned int buf[]={4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,8000};IRsend irsend;
unsigned char state=LOW;
void setup() {// put your setup code here, to run once:pinMode(jieshou, INPUT);pinMode(LED, OUTPUT);pinMode(Btn,INPUT);Serial.begin(9600);pinMode(IR, OUTPUT); 
}void loop() {if(digitalRead(Btn)==HIGH && state==LOW){//消抖delay(15);if(digitalRead(Btn)==HIGH){state=HIGH;if(flag & 1){sendMeidi_OPEN(0xB2,0x5F,0xD0);       digitalWrite(LED,HIGH);flag++;//Serial.println(flag);}else {sendMeidi_CLOSE();digitalWrite(LED,LOW);flag++;//Serial.println(flag);}  }  } if(digitalRead(Btn)==LOW && state==HIGH){//消抖delay(15);if(digitalRead(Btn)==LOW){state=LOW;}}
}void sendMeidi_OPEN(unsigned char A,unsigned char B,unsigned char C){getcode(A,B,C); irsend.sendRaw(buf,200,38);
}
void getcode(unsigned char A,unsigned char B,unsigned char C){unsigned char mask=0x80;unsigned int cnt=2;buf[0]=4400;buf[1]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=5220;buf[cnt++]=4400;buf[cnt++]=4400;//发Afor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & A)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~A))buf[cnt++]=1660;else buf[cnt++]=550;}//发Bfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & B)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~B))buf[cnt++]=1660;else buf[cnt++]=550;}//发Cfor(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & C)buf[cnt++]=1660;else buf[cnt++]=550;}for(mask=0x80;mask!=0;mask>>=1){buf[cnt++]=550;if(mask & (~C))buf[cnt++]=1660;else buf[cnt++]=550;}buf[cnt++]=550;buf[cnt++]=8800;Serial.println(cnt);
}
void sendMeidi_CLOSE(){unsigned int buf_close[]={4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,1660,550,1660,550,1660,550,1660,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,5220,4400,4400,550,1660,550,550,550,1660,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,550,550,550,550,1660,550,1660,550,550,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,1660,550,8000};irsend.sendRaw(buf_close,300,38);
}

 


http://www.ppmy.cn/news/128796.html

相关文章

创意设计让美的电扇更美

本来以为电视的蓬勃发展可能会让广播受到致命冲击&#xff1b;本来以为网络的快速发达可能把传统媒体彻底抛弃&#xff0c;本来以为有空调了&#xff0c;谁还用电扇&#xff1f; 可是&#xff0c;现实中广播以节目创新和脱口秀的方式硬是在于电视的竞争中再次崛起了。网络以快见…

中央空调“国进外退”,格力、美的、海尔三大白如何角逐新市场?

文/螳螂财经&#xff08;ID:TanglangFin&#xff09; 作者/易牟 “国进外退&#xff01;” 只要稍微关注中央空调行业的人&#xff0c;都会发现市场格局的变动&#xff0c;那就是“国进外退”趋势明显&#xff0c;国内品牌的市场份额越来越高&#xff0c;国外品牌的份额逐年…

商用中央空调领域国牌混战,海尔、美的、格力谁更有机会?

文/螳螂财经&#xff08;ID:TanglangFin&#xff09; 作者/图霖 在中央空调领域&#xff0c;国产品牌的声量越来越大。 据《机电信息》中央空调的市场数据&#xff0c;2020年1-9月&#xff0c;中国中央空调行业“十强”品牌分别为&#xff1a;美的、格力、大金、日立、海尔、…

网站服务器控制空调开关,手机遥控美的空调怎么用?手机遥控开关的应用范围?...

用手机去代替遥控器进行使用&#xff0c;对于手机遥控美的空调怎么用&#xff0c;大家是应该要知道具体的方法才行&#xff0c;另外对于手机遥控开关的应用范围也是应该有着正确的认识。 我们平时生活中在开空调的时候&#xff0c;来来回回的拿遥控器&#xff0c;这让很多人们都…

ESP8266美的空调红外控制库(Arduino)

有关美的空调的ESP8266的红外控制库&#xff0c;适用于Arduino平台&#xff0c;需配合IRremoteESP8266库使用。 文件基于IRremoteESP8266库文件编写。 个人网站上的文章www.songzx.top/esp8266 具体代码请访问https://github.com/GYSS1204/IRsendMeidi_ESP8266-RN02S-Midea 说…

【STM32】R05D电控红外协议的美的空调遥控器

目录 一、设计思路一、R05D红外协议原理1.协议手册理解2.验证时序&#xff08;重点&#xff09; 二、硬件实现1.需要的材料2.对发射模块电路进行修改3.STM32 GPIO选择 三、代码实现1.载波38kHZ实现2.R05D时序实现3.调用函数并验证 一、设计思路 通常红外遥控采用NEC传输协议&a…

stc8单片机控制美的空调

stc8单片机通过红外模拟成空调遥控器 文章分为红外遥控原理和单片机实现两个部分 1 红外遥控原理 通常红外遥控为了提高抗干扰性能和降低电源消耗&#xff0c;红外遥控器常用载波的方式传送二进制编码&#xff0c;常用的载波频率为38kHz&#xff0c;这是由发射端所使用的455kH…

小爱同学控制美的美居中的家电热水器,空调等

背景 家里大多数家电都是支持接入米家App的&#xff0c;美的家电不能接入小米&#xff0c;电脑安装Home Assistant成功实现小爱语音控制美的燃气热水器。 实现步骤&#xff1a; 1. 安装docker 我的电脑是windows的&#xff0c;那就直接安装docker desktop https://desktop.…