1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
eroak
  • 1,017
  • 10
  • 17

5 Answers5

2

You are writing on stdout which is buffered by default, and as I cannot see any break, return or exit in your loop, I assume that you quit your program with Ctrl-C.

And all the values that were buffered until there are simply discarded. You should simply fflush stdout after each write to make sure that what is received will end in your file :

 while(1) {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();
    if (value == 0) {
      printf("Unknown encoding");
    } else {    
      printf("Received %i\n", mySwitch.getReceivedValue() );
    }
    fflush(stdout);   // force immediate output
    mySwitch.resetAvailable();
  }

And anyway, having to Ctrl-C to exit a program is not really nice ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thank you very much ! It works now with `fflush(stdout)` ! And i need to call `sudo ./RFsniffer | sudo tee codes.txt` **_OR_** `sudo sh -c './RFSniffer >> codes.txt'` – eroak Jun 14 '15 at 20:38
1

I believe your problem is "sudo". Are you sure you know where codes.txt is? Give it an absolute path (e.g. >> /tmp/codes.txt).

Auspex
  • 2,175
  • 15
  • 35
  • I confess, I don't remember exactly how redirection works when `sudo` is used, but I know I've had problems just trying to do `echo "text" >file`. – Auspex Jun 14 '15 at 15:51
  • If i don't run with `sudo`, my program doesn't have the permission to read the raspberry's GPIO. i'm running this command in my home directory. `/home/me/rfsniffer/`. It doesn't work with an absolute path :( If i try `echo 'hello' >> codes.txt` or `sudo echo 'hello' >> codes.txt` i can see the hello string in my file :( – eroak Jun 14 '15 at 15:54
  • Absolutely you need to use `sudo`, but _when_ you use sudo, your working directory is not necessarily where you think it is. Anyway, two other commenters gave essentially the same advice, and that's not your issue. – Auspex Jun 15 '15 at 15:50
1

Either file codes.txt is not provided the write permission.

Check ls -l codes.txt and confirm file has write permission.

OR path of the file is not right, so provide absolute path of the file:

sudo ./RFSniffer >> codes.txt

replace it with

sudo sh -c `RFSniffer >> <absolute_path>/codes.txt`

OR

sudo ./RFSniffer | sudo tee <absolute_path>/codes.txt

This link give more details on file redirection using sudo : How do I use sudo to redirect output to a location I don't have permission to write to?

Community
  • 1
  • 1
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • If there was a problem writing `code.txt`, the shell will give an error message. – P.P Jun 14 '15 at 16:00
  • Thank you for your answer but it doesn't work :( The file is still empty. And no error in my console – eroak Jun 14 '15 at 16:03
  • @Fieldset I slightly changed the command by removing `./`, can you try like that – Steephen Jun 14 '15 at 16:06
  • And added additional back tick, it should work. Please note it is not back quote http://unix.stackexchange.com/questions/27428/what-does-backquote-backtick-mean-in-bash – Steephen Jun 14 '15 at 16:07
  • I get the `sh: 1: RFSniffer: not found` error :s. I also tried `sudo sh -c /RFSniffer >> /codes.txt`, but the file is still empty – eroak Jun 14 '15 at 16:09
  • @Fieldset you are missing the back tick in your command. Back tick is the same key use to designate code snippet in SO comments – Steephen Jun 14 '15 at 16:21
  • I just forget back tick in my comment. Thank you very much for your help ! With the Serge Ballesta answer and yours, it works very well !! – eroak Jun 14 '15 at 20:40
1

I suspect that sudo might be executing your command with a different working directory. What does sudo pwd print? Have you considered looking for codes.txt in that directory?

edit: Alternatively, could it be that your OS is temporarily storing stdout somewhere until the program closes, at which point it writes to your codes.txt file? What happens if you inject an exit(0); immediately after each call to printf in this code (or terminate the loop, whatever...)

autistic
  • 1
  • 3
  • 35
  • 80
  • `sudo pwd` display the same directory. But if sudo writing codes.txt in a different directory, why i see the codes.txt file in my current directory ? – eroak Jun 14 '15 at 16:08
  • That's was the problem ! Thank you very much ! Just fflush(stdout) after each printf :) – eroak Jun 14 '15 at 20:42
1

You can use tee command, that will store your stream data in file

You can use it as ./RFSniffer | tee codes.txt

Amol Saindane
  • 1,568
  • 10
  • 19