Uploaded from my local repository
[433Utils_NP] / xPi_utils / 433rx.cpp
1 /*
2         433mHz module reader
3
4         Created by Dmitry Shalnov (c) 2019 for Interplay Mediumâ„¢ project.
5
6         Based on RFSniffer.cpp http://code.google.com/p/rc-switch/ by @justy
7         License: MIT
8 */
9
10 #include "../rc-switch/RCSwitch.h"
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <unistd.h>
14         
15 RCSwitch mySwitch;
16
17 void usage(void)
18 {
19         printf("433mHz module reader. Created by Dmitry Shalnoff [interplaymedium.org] Â© 2019, Ver 0.0.2.\n");
20         printf("Usage:\n");
21         printf(" -l     pulse length (optional)\n");
22         printf(" -p     GPIO pin\n");
23         exit (8);
24 }
25
26
27 int main(int argc, char *argv[]) {
28
29         int pulseLength = 0;
30         int PIN = 0; // default
31
32         // commandline parser 
33
34         int argCnt = argc;
35
36         if ( argCnt > 2 && argv[1][0] == '-' && argv[ argCnt - 1 ][0] != '-' ){
37
38                 argCnt--;
39
40                 while (argCnt > 0) {
41
42                         if ( argv[ argCnt ][0] == '-' && argv[ argCnt + 1 ][0] != '-' ) {
43
44                                 switch (argv[ argCnt ][1])
45                                 {
46                                         case 'l':
47                                                 pulseLength = atoi( argv[ argCnt + 1 ] );
48                                                 printf("Pulse length: %d\n",pulseLength);
49                                                 break;
50                 
51                                         case 'p':
52                                                 PIN = atoi( argv[ argCnt + 1 ] );
53                                                 printf("GPIO: %d\n", PIN);;
54                                                 break;
55                 
56                                         default:
57                                                 printf("Wrong Argument: %s\n", argv[ argCnt ]);
58                                                 usage();
59                                 }
60                         }
61         
62                         argCnt--;
63                 }
64         } else {
65                 usage();
66         }
67
68         // ---------------------
69         
70         if(wiringPiSetup() == -1) {
71                 printf("wiringPiSetup failed, exiting...");
72                 return 0;
73         }
74
75         pinMode (PIN, INPUT);
76         mySwitch = RCSwitch();
77         if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
78         mySwitch.enableReceive( PIN );
79
80         while(1) {
81         
82 #ifdef NANOPI
83                 mySwitch.handleNoInterrupt( PIN ); // polling handler, nanoPi WiringNP (no interrupts supported)
84 #endif
85
86                 if (mySwitch.available()) {
87                         
88                         int value = mySwitch.getReceivedValue();
89                         
90                         if (value == 0) {
91                                 printf("Unknown encoding\n");
92                         } else {                
93                                 printf("Received %i\n", mySwitch.getReceivedValue() );
94                         }
95                         
96                         fflush(stdout);
97                         mySwitch.resetAvailable();
98                 }
99 #ifndef NANOPI
100                 usleep(100); 
101 #endif
102
103         }
104
105         exit(0);
106
107
108 }
109
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717