new version of EspEasy change json of dht22

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
megamarco83
Normal user
Posts: 230
Joined: 13 Apr 2017, 23:09

new version of EspEasy change json of dht22

#1 Post by megamarco83 » 04 Oct 2018, 23:36

hi, on the old version of espeasy i had:

Code: Select all

pi@raspberrypi:~ $ curl -s http://192.168.0.198/json?tasknr=2
{
"TaskName": "dht22",
"Temperature": 24.10,
"Humidity": 55.30
now with the new one:

Code: Select all

pi@raspberrypi:~ $ curl -s http://192.168.0.198/json?tasknr=2
{
"TaskValues": [
{"ValueNumber":1,
"Name":"Temperature",
"NrDecimals":2,
"Value":24.10
},
{"ValueNumber":2,
"Name":"Humidity",
"NrDecimals":2,
"Value":55.30
}],
"TTL":60000,
"DataAcquisition": [
{"Controller":1,
"IDX":21,
"Enabled":"true"
},
{"Controller":2,
"IDX":0,
"Enabled":"false"
},
{"Controller":3,
"IDX":0,
"Enabled":"false"
}],
"TaskInterval":60,
"Type":"Environment - DHT11/12/22  SONOFF2301/7021",
"TaskName":"dht22",
"TaskEnabled":"true",
"TaskNumber":2
}
i need to take value of Temperature end Humidity using php
on previus version was quite easy:

Code: Select all

} elseif ($argv[1] == 'esp_dht22_temp_primo_piano') {
         $url = 'http://192.168.0.198/json?tasknr=2';
         $pagina = file_get_contents($url);
         $json_output = json_decode($pagina, true);
         $outstr = $json_output['Temperature'];
         $outstr = "18($outstr*°C)";        
     } elseif ($argv[1] == 'esp_dht22_umid_primo_piano') {
         $url = 'http://192.168.0.198/json?tasknr=2';
         $pagina = file_get_contents($url);
         $json_output = json_decode($pagina, true);
         $outstr = $json_output['Humidity'];
         $outstr = "19($outstr*%)";  
now that the value are "mixed" and has the same name ("value" for both temperature and humidity) how i can take only the value and biuld an output like this:

18(TEMP_VALUE*°C)
and
19(HUM_VALUE*%)
where i have to subtitute TEMP_VALUE with the numerical output
18(24.1*°C)
19(55.4*%)

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: new version of EspEasy change json of dht22

#2 Post by grovkillen » 05 Oct 2018, 07:00

Yes we are now using correct JSON path. A variable should never be used in a JSON path since a parser cannot parse through it generically. So this is how it's gonna look like from now on.

You should be able to use JSON for your php script but instead of hardcoded path names you should parse it. That way you can use the same script for all your nodes and changing a name will not break the script.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

megamarco83
Normal user
Posts: 230
Joined: 13 Apr 2017, 23:09

Re: new version of EspEasy change json of dht22

#3 Post by megamarco83 » 05 Oct 2018, 09:08

grovkillen wrote: 05 Oct 2018, 07:00 Yes we are now using correct JSON path. A variable should never be used in a JSON path since a parser cannot parse through it generically. So this is how it's gonna look like from now on.

You should be able to use JSON for your php script but instead of hardcoded path names you should parse it. That way you can use the same script for all your nodes and changing a name will not break the script.
hi grovekillen thanks so mutch as well
i'm beginner as program language, could you give me a php example where you show how to parse the temperature and humidity ?
thanks

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: new version of EspEasy change json of dht22

#4 Post by grovkillen » 05 Oct 2018, 11:16

I'm using JavaScript myself (but plan to learn php) so I have not the knowledge of how to do it in detail but here's a starting point: https://www.w3schools.com/js/js_json_php.asp
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 47 guests