使用RF接收器433 MHz解码RF远程信号值

时间:2016-09-01 11:47:10

标签: arduino iot arduino-uno home-automation

我需要有关RF接收器433 MHz的帮助。我有频率为433 MHz的RF遥控器。 我想要实现的是我想使用RF接收器解码RF遥控器的信号值。我跟着RC Switch Library,但我无法得到解决方案。

以下教程链接: - http://jubitoblog.blogspot.in/2014/06/arduino-radio-remote-control-rf.html

我的收件人代码: -

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
int txPin = 10;
String readString;

void setup(void) {
  Serial.begin(9600);

  mySwitch.enableTransmit(txPin);
  mySwitch.enableReceive(0);    
}

void loop(void) {

  if (mySwitch.available()) {
      Serial.print("Received ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );

    output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
    mySwitch.resetAvailable();
  }

  while (Serial.available())
  {
    delay(1); //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
    readString.toLowerCase();
    readString.trim();
  }

  if (readString.length() >= 24)
  {
    //mySwitch.disableReceive();
    char inBin[25];
    readString.toCharArray(inBin,25);
    mySwitch.send(inBin);
    //mySwitch.enableReceive(0, output);
  }
  readString = "";
}

void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
  if (decimal == 0) {
    Serial.print("Unknown encoding.");
  } else {
    char* b = dec2binWzerofill(decimal, length);
    Serial.println( b );

      char* c = bin2tristate(b);
      Serial.println( c );
  }
}

static char* bin2tristate(char* bin) {
  char returnValue[50];
  int pos = 0;
  int pos2 = 0;
  while (bin[pos]!='\0' && bin[pos+1]!='\0') {
    if (bin[pos]=='0' && bin[pos+1]=='0') {
      returnValue[pos2] = '0';
    } else if (bin[pos]=='1' && bin[pos+1]=='1') {
      returnValue[pos2] = '1';
    } else if (bin[pos]=='0' && bin[pos+1]=='1') {
      returnValue[pos2] = 'F';
    } else {
      return "not applicable";
    }
    pos = pos+2;
    pos2++;
  }
  returnValue[pos2] = '\0';
  return returnValue;
}

static char* dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
  static char bin[64]; 
  unsigned int i=0;

  while (Dec > 0) {
    bin[32+i++] = (Dec & 1 > 0) ? '1' : '0';
    Dec = Dec >> 1;
  }

  for (unsigned int j = 0; j< bitLength; j++) {
    if (j >= bitLength - i) {
      bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
    }else {
      bin[j] = '0';
    }
  }
  bin[bitLength] = '\0';

  return bin;
}

请帮助我摆脱这个问题。

0 个答案:

没有答案