I'm using a C program on my raspberry pi2 with a 433mhz receiver to read codes that are transmitted. This program sniffing 433mhz codes.
To run it, I use the following command: sudo ./RFSniffer
and if a code is found, the program displays in the console something like :
Received 5204
But, I would like to be able to get these codes in a file, so I tried this:
sudo ./RFSniffer >> codes.txt
But nothing is appended to my codes.txt file...and I don't know why. What's wrong with my code? The file is always empty.
Here is my code :
#include "RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
RCSwitch mySwitch;
int main(int argc, char *argv[]) {
int PIN = 2;
if(wiringPiSetup() == -1)
return 0;
mySwitch = RCSwitch();
mySwitch.enableReceive(PIN);
while(1) {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
printf("Unknown encoding");
} else {
printf("Received %i\n", mySwitch.getReceivedValue() );
}
mySwitch.resetAvailable();
}
}
exit(0);
}
Could the problem be exit(0)
or printf()
instead of anything else?
EDIT:
The program is compiled with WiringPI lib so there is a flag '-lwiringPi'
The tool is available here: https://github.com/ninjablocks/433Utils/tree/master/RPi_utils
EDIT2:
I changed the code to:
int main(int argc, char *argv[]) {
printf("yeah\n");
exit(0);
}
And it works only with:
sudo sh -c './RFSniffer >> /home/pi/433Utils/RPi_utils/codes.txt'
So the problem is maybe while(1) { printf... }
? Or the file is only written when exit(0)
is called?