rtl_433 command output will not pipe to php script

119 Views Asked by At

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"}
1

There are 1 best solutions below

0
On

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;
}