-1

I'm trying to get the output from the rtl_433 command into a php script.

If I run echo TEST | php sensors.php I get

STARTING PHP SCRIPT
DATA FROM PHP SCRIPT IS: TEST

sensors.php is just this

<?php

echo "STARTING PHP SCRIPT" . PHP_EOL;

$data = stream_get_contents(STDIN);

echo "DATA FROM PHP SCRIPT IS: " . $data . PHP_EOL;

If I run this command rtl_433 -f 344975000 -F json -M utc | php sensors.php

This is the command line output I get

rtl_433 version 21.12 (2021-12-14) inputs file rtl_tcp RTL-SDR SoapySDR
Use -h for usage help and see https://triq.org/ for documentation.
Trying conf file at "rtl_433.conf"...
Trying conf file at "/home/tehjrow/.config/rtl_433/rtl_433.conf"...
Trying conf file at "/usr/local/etc/rtl_433/rtl_433.conf"...
Trying conf file at "/etc/rtl_433/rtl_433.conf"...
Registered 176 out of 207 device decoding protocols [ 1-4 8 11-12 15-17 19-23 25-26 29-36 38-60 63 67-71 73-100 102-105 108-116 119 121 124-128 130-149 151-161 163-168 170-175 177-197 199 201-207 ]
STARTING PHP SCRIPT
Found Rafael Micro R820T tuner
Exact sample rate is: 250000.000414 Hz
[R82XX] PLL not locked!
Sample rate set to 250000 S/s.
Tuner gain set to Auto.
Tuned to 344.975MHz.
Allocating 15 zero-copy buffers
baseband_demod_FM: low pass filter for 250000 Hz at cutoff 25000 Hz, 40.0 us

If I run the command without the | php sensors.php I get this output (I'm trying to feed the json into a php script).

rtl_433 version 21.12 (2021-12-14) inputs file rtl_tcp RTL-SDR SoapySDR
Use -h for usage help and see https://triq.org/ for documentation.
Trying conf file at "rtl_433.conf"...
Trying conf file at "/home/tehjrow/.config/rtl_433/rtl_433.conf"...
Trying conf file at "/usr/local/etc/rtl_433/rtl_433.conf"...
Trying conf file at "/etc/rtl_433/rtl_433.conf"...
Registered 176 out of 207 device decoding protocols [ 1-4 8 11-12 15-17 19-23 25-26 29-36 38-60 63 67-71 73-100 102-105 108-116 119 121 124-128 130-149 151-161 163-168 170-175 177-197 199 201-207 ]
Found Rafael Micro R820T tuner
Exact sample rate is: 250000.000414 Hz
[R82XX] PLL not locked!
Sample rate set to 250000 S/s.
Tuner gain set to Auto.
Tuned to 344.975MHz.
Allocating 15 zero-copy buffers
baseband_demod_FM: low pass filter for 250000 Hz at cutoff 25000 Hz, 40.0 us
{"time" : "2023-10-21 18:02:07", "model" : "Honeywell-Security", "id" : 37656, "channel" : 10, "event" : 160, "state" : "open", "contact_open" : 1, "reed_open" : 1, "alarm" : 0, "tamper" : 0, "battery_ok" : 1, "heartbeat" : 0, "mic" : "CRC"}
{"time" : "2023-10-21 18:02:07", "model" : "Honeywell-Security", "id" : 37656, "channel" : 10, "event" : 160, "state" : "open", "contact_open" : 1, "reed_open" : 1, "alarm" : 0, "tamper" : 0, "battery_ok" : 1, "heartbeat" : 0, "mic" : "CRC"}
{"time" : "2023-10-21 18:02:07", "model" : "Honeywell-Security", "id" : 37656, "channel" : 10, "event" : 160, "state" : "open", "contact_open" : 1, "reed_open" : 1, "alarm" : 0, "tamper" : 0, "battery_ok" : 1, "heartbeat" : 0, "mic" : "CRC"}
2
  • Are you viewing the sensors.php output in a browser? Perhaps modify php to write $data to some file instead and inspect contents of that file to see if JSON is there. It might also be failing because the output of the rtl_433 command lacks a trailing newline/carriage return char? There's a comment in the docs that says "The command line interface data in STDIN is not made available until return is pressed." Lastly, does rtl_433 keep until you halt it or does it run and halt immediately? Commented Oct 21, 2023 at 18:31
  • Looks like exactly what you say you want to happen is happening - the JSON output from rtl_433 is going to stdout and piping into php sensors.php as desired while the rest of the rtl_433 output is apparently going to stderr and so is being displayed on your screen. What problem do you have with that? Commented Oct 21, 2023 at 20:25

1 Answer 1

0

edit your sensors.php script to read from stdin and parse the JSON data, so first of all try to use a while loop to read each line from stdin until there are no more lines, because the lines are then concatenated into the $data variable and after reading all the lines, you can use json_decode to parse the JSON data into an associative array!

let me show you how :

echo "STARTING PHP SCRIPT" . PHP_EOL;

//here read from stdin
$data = '';
while (($line = fgets(STDIN)) !== false) {
    $data .= $line;
}

//parse JSON data
$jsonData = json_decode($data, true);

//check if JSON parsing was successful
if ($jsonData !== null) {
    //process the JSON data
    foreach ($jsonData as $item) {
        //access the individual data fields
        $time = $item['time'];
        $model = $item['model'];
        // ...
        
        //performing your desired actions with the data
        echo "Received data: Time=$time, Model=$model" . PHP_EOL;
    }
} else {
    //JSON parsing failed
    echo "Failed to parse JSON data" . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.