extract value from json reponse's espeasy

Talks about metering
Post Reply
s_p2003
Posts: 2
Joined: Thu Dec 19, 2019 12:30 pm

extract value from json reponse's espeasy

Post by s_p2003 » Sun Apr 26, 2020 12:01 pm

Hi

I would like to extract the temperature value from the json of an espeasy device

this is the link

Code: Select all

http://192.168.0.151/json?tasknr=2
and this is the answer

Code: Select all

{
"TaskName":"temperaturaboiler",
"temperaturaboiler": 65.25
}
this is the code I used, but I'm not sure

Code: Select all

$lett_temp=exec('curl -s http://192.168.0.151/json?tasknr=2 | egrep \'"temperaturaboiler":\' | egrep -o \'[0-9]*(\.)?[0-9]*\' ');
it's right?

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: extract value from json reponse's espeasy

Post by jeanmarc » Sun Apr 26, 2020 1:00 pm

Hi,
why don't your use json_decode ?
check req_espeasy.php example in misc of comapps_examples

Code: Select all

...
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $espjson = array();
    if (($espjson = curl_exec($ch)) === false) {
        die(curl_error($ch) . "\n");
    }
    curl_close($ch);
...
   $espjson   = json_decode($espjson, true);  
   // print_r($espjson); // uncomment 
   $val = $espjson['temperaturaboiler']; 
   //  or maybe
    $val = $espjson['TaskValues'][1]['temperaturaboiler'];
...

s_p2003
Posts: 2
Joined: Thu Dec 19, 2019 12:30 pm

Re: extract value from json reponse's espeasy

Post by s_p2003 » Sun Apr 26, 2020 5:12 pm

Thank you so much!!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests