Advertisement
Guest User

rfreceive.cpp

a guest
Sep 17th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. /*
  2.   rfreceive.cpp
  3.   by Sarrailh Remi
  4.   Description : This code receives RF codes and trigger actions
  5.   Modificado por Juanmol para http://rsppi.blogspot.com
  6. */
  7.  
  8. #include "RCSwitch.h"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12.   RCSwitch mySwitch;
  13.   int pin; //Pin of the RF receiver
  14.   int codereceived; // Code received
  15.   char buffer[200]; // Buffer for the command
  16.  
  17.   int main(int argc, char *argv[]) {
  18.  
  19.         if(argc == 2) { //Verify if there is an argument
  20.       pin = atoi(argv[1]); //Convert first argument to INT
  21.       printf("PIN SELECTED :%i\n",pin);
  22.     }
  23.   else {
  24.     printf("No PIN Selected\n");
  25.     printf("Usage: rfreceive PIN\n");
  26.     printf("Example: rfreceive 0\n");
  27.     exit(1);
  28.   }
  29.  
  30.     if(wiringPiSetup() == -1) { //Initialize WiringPI
  31.       printf("Wiring PI is not installed\n"); //If WiringPI is not installed give indications
  32.       printf("You can install it with theses command:\n");
  33.       printf("apt-get install git-core\n");
  34.       printf("git clone git://git.drogon.net/wiringPi\n");
  35.       printf("cd wiringPi\n");
  36.       printf("git pull origin\n");
  37.       printf("./build\n");
  38.       exit(1);
  39.     }
  40.  
  41.    mySwitch = RCSwitch(); //Settings RCSwitch (with first argument as pin)
  42.    mySwitch.enableReceive(pin);
  43.  
  44.    while(1) { //Infinite loop
  45.     if (mySwitch.available()) { //If Data is detected.
  46.          codereceived = mySwitch.getReceivedValue(); //Get data in decimal
  47.           printf("Received %i\n", mySwitch.getReceivedValue() );
  48.           sprintf(buffer, "sh /usr/local/bin/recibe_rf.sh %i", mySwitch.getReceivedValue());
  49.           system(buffer);
  50.  
  51.           //Want to execute something when a code is received ?
  52.           //When 12345 is received this will execute program_to_execute for exemple)
  53.           /*
  54.           if (codereceived == 12345)
  55.           {
  56.             system("program_to_execute");
  57.           }
  58.           */
  59.     }
  60.   mySwitch.resetAvailable();
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement