|
本帖最后由 wsy0315 于 2016-2-18 19:59 编辑
随着社会的发展,智能家居这一概念越来越多人提起!
我当然也不例外,只懂点儿硬件的我实在是感觉力不从心,不少人弄了以后代码却大多不给人看(用来赚钱),所以可以直接利用的资源实在是太少,
我眼中的智能家居无非就是遥控加回传,方便使用者而已,成套的方案又贵得离谱,所以想自己弄点什么,我家里已经改造了一组墙壁开关,采用315MHz遥控,用着很方便,我就有了想要用手机控制的想法,就写了以下代码,笨代码可以实现手机发送ANSCII字符串实现控制315MHz,433MHz,红外以及模块本身IO口的数字以及模拟PWM控制!当然了,为了安全起见,还加入了简单的字符串效验,可以用很少的成本,完成对家里电器的控制!适合个人DIY
字符串效验 | 文件头 | 机器码 | 机器反码 | 类别码 | 类别反码 | 数值码 | 数值反码 | 效验 | 文件尾 | | WSY | A-Z | Z-A | A-Z | Z-A | A-Z | Z-A | A-Z | YSW |
代码如下:
[mw_shl_code=c,true]//缺少传感器,解码(红外,315MHz,433MHz),状态回传
unsigned int ir_zs[] = {
0xFFFFFFFF, //empty0
0x33B8609F, //Z CH-
0x33B800FF, //Z POWER
};
//#define ppp(xxx) Serial.print("xxx");Serial.print(" = ");Serial.println(xxx);//错误代码
//#define ppp(name) Serial.print((#name));Serial.print(" = ");Serial.println(name);//出自ArduinoCN 一块奔跑的五花肉
unsigned int TCL1[51] = {4150, 3750, 750, 1750, 750, 1750, 750, 1750, 700, 1800, 700, 800, 700, 800, 700, 1800, 700, 800, 700, 1800, 700, 800, 700, 1800, 700, 800, 700, 800, 650, 850, 650, 850, 650, 900, 600, 1900, 550, 1950, 550, 950, 550, 1950, 550, 950, 550, 1950, 500, 1000, 500, 2000, 450}; // 1 // 开关
#include <IRremote.h>
int RECV_PIN = 11;//定义红外接收器的引脚为11
IRsend irsend;// 定义 IRsend 物件来发射红外线讯号
IRrecv irrecv(RECV_PIN);
decode_results results;
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
String cd = "";
int DPIN[6] = {0, 0, 0, 0, 0, 0};
int APIN[6] = {0, 0, 0, 0, 0, 0};
void setup()
{
irrecv.enableIRIn(); // 初始化红外接收器
mySwitch.enableTransmit(5);
mySwitch.setPulseLength(270);
Serial.begin(9600);
while (!Serial)
{
;
}
Serial.println("test:");
for (int j = 0; j < 6; j++) pinMode(DPIN[j], OUTPUT);
for (int j = 0; j < 6; j++) pinMode(APIN[j], OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
int is = (Serial.read() - 64);
cd += (char)is;
delay(2);
}
if (cd.length() > 0)
{
if ((cd[9] == ((cd[3] + cd[5] + cd[7]) % 26) + 1) &&
(cd[0] == 23) && (cd[12] == 23) && (cd[1] == 19) && (cd[11] == 19) && (cd[2] == 25) && (cd[10] == 25) &&
(cd[3] + cd[4] == 27) && (cd[5] + cd[6] == 27) && (cd[7] + cd[8] == 27))
{
Serial.println("");
ppp(cd[3], "cd[3]");
/*
int cd3 = (cd[3] + 2) / 3; //机器码
ppp(cd3, "CD3");
int cd33 = cd[3] % 3; //"A"=1,余1;"B"=2,余2;"C"=3,余0;
ppp(cd33, "CD33"); //分支3:余1 AD;余2 红外;余0 315MHz,433MHz
*/
if (cd[3] == 1) { //本机 A AD
int pin = cd[5];
int val = cd[7];
if (pin < 12) {
int dval = (val % 2);
digitalWrite(DPIN[(pin - 1)], dval); //取值范围0-10
ppp(pin, "DP(-1)");
ppp(dval, "DV");
}
else if (pin > 11) {
int aval = map(val, 1, 25, 0, 255);
analogWrite(APIN[pin - 12], aval); // 取值范围0-14
ppp(pin, "AP(-12)");
ppp(aval, "AV");
}
}
else if (cd[3] == 2) { //本机 B 红外
int valir = (cd[5] * cd[7]);
ppp(valir, "IR");
switch (valir) {
case 1:
irsend.sendNEC(ir_zs[2], 32);
break; //开关
case 2:
irsend.sendRaw(TCL1, 51, 38);
break; //开关
}
}
else if (cd[3] == 3) { //本机 C 315MHz,433MHz
int valrc = (cd[5] * cd[7]);
ppp(valrc, "RC");
switch (valrc) {
case 11:
mySwitch.sendTriState("010FFF1F1000");//A Z
Serial.println("A");
break;
case 12:
mySwitch.sendTriState("010FFF1F0100");//B Z
Serial.println("B");
break;
case 13:
mySwitch.sendTriState("010FFF1F0010");//C Z
Serial.println("C");
break;
case 14:
mySwitch.sendTriState("010FFF1F0011");//D Z
Serial.println("D");
break;
case 15:
mySwitch.sendTriState("010FFF1F1100");//ON/OFF Z
Serial.println("E");
case 21:
mySwitch.sendTriState("000F01010100");//A D
Serial.println("a");
break;
case 22:
mySwitch.sendTriState("000F01010010");//B D
Serial.println("b");
break;
case 23:
mySwitch.sendTriState("000F01011100");//C D
Serial.println("c");
break;
case 24:
mySwitch.sendTriState("000F01010011");//D D
Serial.println("d");
break;
case 25:
mySwitch.sendTriState("000F01010001");//ON/OFF D
Serial.println("e");
break;
case 26:
mySwitch.sendTriState("000F01011000");//SLEEP D
Serial.println("f");
break;
}
}
else {//非本机 透传,待编译。。。。。
//pr(cd);
//透传,待编译。。。。。
}
}
cd = "";
Serial.flush();
}
}
void pr(String c)
{
int i;
for (i = 0; i < 13; i++)
{
Serial.print("STR[");
Serial.print(i);
Serial.print("] is ");
Serial.print(c);
Serial.print(", HEX is ");
Serial.println(c, HEX);
}
Serial.print("cd is ");
Serial.print(c);
Serial.print(", HEX is ");
Serial.println(c);
}
void ppp(int val, char *name) {
Serial.print(name);
Serial.print("=");
Serial.println(val);
}[/mw_shl_code]
测试字符串
AD样例: | | | 1 | | |
| | 26 | | | | A0 | | D0 | WSYAZ | AZ | AZ | D | YSW | WSYAZ | AZ | ZA | C | YSW | | AZ | O | D1 | WSYAZ | BY | AZ | E | YSW | WSYAZ | BY | ZA | D | YSW | | BY | P | D2 | WSYAZ | CX | AZ | F | YSW | WSYAZ | CX | ZA | E | YSW | | CX | Q | D3 | WSYAZ | DW | AZ | G | YSW | WSYAZ | DW | ZA | F | YSW | | DW | R | D4 | WSYAZ | EV | AZ | H | YSW | WSYAZ | EV | ZA | G | YSW | | EV | S | D5 | WSYAZ | FU | AZ | I | YSW | WSYAZ | FU | ZA | H | YSW | | FU | T | D6 | WSYAZ | GT | AZ | J | YSW | WSYAZ | GT | ZA | I | YSW | | GT | U | D7 | WSYAZ | HS | AZ | K | YSW | WSYAZ | HS | ZA | J | YSW | | HS | V | D8 | WSYAZ | IR | AZ | L | YSW | WSYAZ | IR | ZA | K | YSW | | IR | W | D9 | WSYAZ | JQ | AZ | M | YSW | WSYAZ | JQ | ZA | L | YSW | | JQ | X | D10 | WSYAZ | KP | AZ | N | YSW | WSYAZ | KP | ZA | M | YSW | | KP | Y | A0 | WSYAZ | LO | AZ | O | YSW | WSYAZ | LO | ZA | N | YSW | | LO | Z | A1 | WSYAZ | MN | AZ | P | YSW | WSYAZ | MN | ZA | O | YSW | | MN | A | A2 | WSYAZ | NM | AZ | Q | YSW | WSYAZ | NM | ZA | P | YSW | | NM | B | A3 | WSYAZ | OL | AZ | R | YSW | WSYAZ | OL | ZA | Q | YSW | | OL | C | A4 | WSYAZ | PK | AZ | S | YSW | WSYAZ | PK | ZA | R | YSW | | PK | D | A5 | WSYAZ | QJ | AZ | T | YSW | WSYAZ | QJ | ZA | S | YSW | | QJ | E | A6 | WSYAZ | RI | AZ | U | YSW | WSYAZ | RI | ZA | T | YSW | | RI | F | A7 | WSYAZ | SH | AZ | V | YSW | WSYAZ | SH | ZA | U | YSW | | SH | G | A8 | WSYAZ | TG | AZ | W | YSW | WSYAZ | TG | ZA | V | YSW | | TG | H | A9 | WSYAZ | UF | AZ | X | YSW | WSYAZ | UF | ZA | W | YSW | | UF | I | A10 | WSYAZ | VE | AZ | Y | YSW | WSYAZ | VE | ZA | X | YSW | | VE | J | A11 | WSYAZ | WD | AZ | Z | YSW | WSYAZ | WD | ZA | Y | YSW | | WD | K | A12 | WSYAZ | XC | AZ | A | YSW | WSYAZ | XC | ZA | Z | YSW | | XC | L | A13 | WSYAZ | YB | AZ | B | YSW | WSYAZ | YB | ZA | A | YSW | | YB | M | A14 | WSYAZ | ZA | AZ | C | YSW | WSYAZ | ZA | ZA | B | YSW | | ZA | N | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | IR样例: | | | | RC样例: | | | | | | | | | 1 | WSYBYAZAZEYSW | | 11 | WSYCXAZLOQYSW | | | | | | | | 2 | WSYBYAZBYFYSW | | 25 | WSYCXEVEVNYSW | | | | | | | |
发送样例测试字符串,就可以返回数值,发送不符合规律的字符串,则不动作,例如发送WSYCXAZLOQYSW,就会控制315MHz的发送模块,发送控制信号,当然了,发送的是我的开关的编码,只要换成你自己的编码,就可以实现控制自己的电器了,采用这个模块的原因主要是便宜,哈哈!只是添加遥控代码不是很方便,清晰,能力有限,希望大家能有更好的作品,让我们大家享受下科技带来的好处!
|
|