极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12500|回复: 2

发个自己写的蓝牙串口控制红外,315以及IO,PWM

[复制链接]
发表于 2016-2-18 20:03:38 | 显示全部楼层 |阅读模式
本帖最后由 wsy0315 于 2016-2-18 20:06 编辑

随着社会的发展,智能家居这一概念越来越多人提起!
我当然也不例外,只懂点儿硬件的我实在是感觉力不从心,不少人弄了以后代码却大多不给人看(用来赚钱),所以可以直接利用的资源实在是太少,
我眼中的智能家居无非就是遥控加回传,方便使用者而已,成套的方案又贵得离谱,所以想自己弄点什么,我家里已经改造了一组墙壁开关,采用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]


测试字符串

IR样例:                                                                         
1       
WSYBYAZAZEYSW
2       
WSYBYAZBYFYSW
RC样例:       
11       
WSYCXAZLOQYSW                
25       
WSYCXEVEVNYSW

发送样例测试字符串,就可以返回数值,发送不符合规律的字符串,则不动作,例如发送WSYCXAZLOQYSW,就会控制315MHz的发送模块,发送控制信号,当然了,发送的是我的开关的编码,只要换成你自己的编码,就可以实现控制自己的电器了,采用这个模块的原因主要是便宜,哈哈!只是添加遥控代码不是很方便,清晰,能力有限,希望大家能有更好的作品,让我们大家享受下科技带来的好处!
回复

使用道具 举报

发表于 2016-2-19 12:43:58 | 显示全部楼层
谢谢楼主分享资源,不过很多BUG,怎样搞:
Arduino:1.7.2 (Windows XP), 板:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_feb19a.ino:1:1: error: expected unqualified-id before '[' token

sketch_feb19a.ino: In function 'void loop()':

sketch_feb19a.ino:97:28: error: 'ir_zs' was not declared in this scope

sketch_feb19a.ino: In function 'void pr(String)':

sketch_feb19a.ino:173:26: error: no matching function for call to 'HardwareSerial::println(String&, int)'

sketch_feb19a.ino:173:26: note: candidates are:

In file included from D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

                 from D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

                 from D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Arduino.h:223,

                 from sketch_feb19a.ino:1:

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:70:12: note: size_t Print::println(const __FlashStringHelper*)

     size_t println(const __FlashStringHelper *);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:70:12: note:   candidate expects 1 argument, 2 provided

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:71:12: note: size_t Print::println(const String&)

     size_t println(const String &s);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:71:12: note:   candidate expects 1 argument, 2 provided

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:72:12: note: size_t Print::println(const char*)

     size_t println(const char[]);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:72:12: note:   candidate expects 1 argument, 2 provided

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:73:12: note: size_t Print::println(char)

     size_t println(char);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:73:12: note:   candidate expects 1 argument, 2 provided

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:74:12: note: size_t Print::println(unsigned char, int)

     size_t println(unsigned char, int = DEC);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:74:12: note:   no known conversion for argument 1 from 'String' to 'unsigned char'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:75:12: note: size_t Print::println(int, int)

     size_t println(int, int = DEC);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:75:12: note:   no known conversion for argument 1 from 'String' to 'int'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:76:12: note: size_t Print::println(unsigned int, int)

     size_t println(unsigned int, int = DEC);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:76:12: note:   no known conversion for argument 1 from 'String' to 'unsigned int'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:77:12: note: size_t Print::println(long int, int)

     size_t println(long, int = DEC);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:77:12: note:   no known conversion for argument 1 from 'String' to 'long int'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:78:12: note: size_t Print::println(long unsigned int, int)

     size_t println(unsigned long, int = DEC);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:78:12: note:   no known conversion for argument 1 from 'String' to 'long unsigned int'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:79:12: note: size_t Print::println(double, int)

     size_t println(double, int = 2);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:79:12: note:   no known conversion for argument 1 from 'String' to 'double'

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:80:12: note: size_t Print::println(const Printable&)

     size_t println(const Printable&);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:80:12: note:   candidate expects 1 argument, 2 provided

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:81:12: note: size_t Print::println()

     size_t println(void);

            ^

D:\Arduino-1.7.2\hardware\arduino\avr\cores\arduino/Print.h:81:12: note:   candidate expects 0 arguments, 2 provided

sketch_feb19a.ino: At global scope:

sketch_feb19a.ino:185:2: error: expected unqualified-id before '[' token

回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-19 15:13:15 | 显示全部楼层
yuqingshan 发表于 2016-2-19 12:43
谢谢楼主分享资源,不过很多BUG,怎样搞:
Arduino:1.7.2 (Windows XP), 板:"Arduino Mega or Mega 2560, ...

我也是小白一个,只是想用代码表示我的一个想法,既然编辑完了,就贡献出来,免得大家都在重复的编辑,这样也有利于改进!
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 21:32 , Processed in 0.039975 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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