import value from ESP8266 - ESPeasy

Talks about metering
Post Reply
megamarco83
Posts: 29
Joined: Tue Apr 14, 2015 9:25 am

import value from ESP8266 - ESPeasy

Post by megamarco83 » Tue Feb 21, 2017 10:12 am

hi i have an ESP8266 and i connected to it two sensors: ds18b20 and dht22
now i would have the temperature values on metern
i do in this way; if i use the command:

Code: Select all

pi@raspberrypi ~ $ curl http://192.168.0.193/json?tasknr=2
{
"TaskName": "ds_gpio12_28_193",
"Temperature": 18.69
} 
192.168.0.193 = IP if esp
2 = task for ds18b20

now i clean the result:

Code: Select all

pi@raspberrypi ~ $ curl http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 57 100 57 0 0 1801 0 --:--:-- --:--:-- --:--:-- 2714
18.69
18.69 = temperature (value cleaned)

now i crete a format value for metern (meter number 20)

Code: Select all

pi@raspberrypi ~ $ echo "20($(curl http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*')*°C)"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 57 100 57 0 0 1177 0 --:--:-- --:--:-- --:--:-- 1500
20(18.69*°C)
20(18.69*°C) = should be the value passed to metern, meter number 20

but if i go in admin page, create meter 20, put type = senor
and in command i paste:
echo "20($(curl http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*')*°C)"
and i run the test button i receive:
is not valid, the correct format is 20(1234.5*°C)
what is wrong?
my metern version is 0.8.2.7

thanks so much

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

Re: import value from ESP8266 - ESPeasy

Post by jeanmarc » Tue Feb 21, 2017 11:35 am

Hi,
I think you need to remove the curl progress output (using silent option -s).

You also need to avoid requesting commands with quotes directly from mN. Rather use a "parser" like reqsdm example :

Code: Select all

#!/usr/bin/php
<?php
// chmod +x then ln -s /srv/http/comapps/reqESP8266.php /usr/bin/reqESP8266

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

if(!isset($argv[1])) {
die("Abording: no valid argument given.\n");
}
if ($argv[1] == '-temp') {
    $outstr =  exec('curl http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*'');
}
// and so on..

echo "$outstr";
?>
you may then use "reqESP8266 -temp" from mN.

PS: please update to latest, it have more detailled error output.

megamarco83
Posts: 29
Joined: Tue Apr 14, 2015 9:25 am

Re: import value from ESP8266 - ESPeasy

Post by megamarco83 » Tue Feb 21, 2017 12:33 pm

hi Jeanmark and thanks for support
i have mine reqsdm

Code: Select all

#!/usr/bin/php
<?php
// This script will output a meterN compatible format for the main or live command
// You'll need to setup correct permission chmod +x
// then ln -s /var/www/comapps/reqsdm.php /usr/bin/reqsdm
// Request command with 'reqsdm tensione' or 'reqsdm corrente' or ......

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (!isset($argv[1])) {
   die("Abording: no valid argument given.\n");
      } elseif ($argv[1] == 'tensione') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_1\(" | grep "*V)"');
      } elseif ($argv[1] == 'corrente') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_2\(" | grep "*A)"');
      } elseif ($argv[1] == 'cospi') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_4\(" | grep "*F)"');
      } elseif ($argv[1] == '-temp') {
         $outstr =  exec('curl http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*'');
     // and so on ....
   } else {
    die("Usage: reqsdm (tensione|corrente|cospi|-temp)\n");
   }
echo "$outstr";
?>
i added the -temp

but when i lunch it from ssh i receive:

Code: Select all

pi@raspberrypi ~ $ reqsdm temp
PHP Parse error:  syntax error, unexpected '"Temperature"' (T_CONSTANT_ENCAPSED_STRING) in /var/www/metern/comapps/reqsdm.php on line 20
could you help me again?

p.s. if i use:

Code: Select all

pi@raspberrypi ~ $ curl -s http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*'
18.06
the output is 18.06 i have not to modify the numberd adding echo command to have the output like that:

Code: Select all

pi@raspberrypi ~ $ echo "20($(curl -s http://192.168.0.193/json?tasknr=2 | egrep '"Temperature":' | egrep -o '[0-9]*(\.)?[0-9]*')*°C)"
20(18.12*°C)
where the 20 = meter number 20 (where i add the temperature from ESP8266
thanks

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

Re: import value from ESP8266 - ESPeasy

Post by jeanmarc » Tue Feb 21, 2017 1:00 pm

That's not really requesting sdm.. but anyway ;)
You need to escape single quotes in the command :

Code: Select all

if ($argv[1] == '-temp') {
	$outstr =  exec('curl -s http://192.168.0.193/json?tasknr=2 | egrep \'"Temperature":\' | egrep -o \'[0-9]*(\.)?[0-9]*\'');
	$outstr = "20($outstr*°C)";
}
ps: 20 is the meter ID

megamarco83
Posts: 29
Joined: Tue Apr 14, 2015 9:25 am

Re: import value from ESP8266 - ESPeasy

Post by megamarco83 » Tue Feb 21, 2017 2:12 pm

Hi Jeanmarc you are amazing another time!
thanks!!!!
now i modify the reqsdm.php adding also the other meter DS18B20 that is used with webiopi, and DS18b20 + DHT22 used by ESP8266
and all works great
i post the php, is it ok for you?
thanks again so much!

Code: Select all

#!/usr/bin/php
<?php
// This script will output a meterN compatible format for the main or live command
// You'll need to setup correct permission chmod +x
// then ln -s /var/www/comapps/reqsdm.php /usr/bin/reqsdm
// Request command with 'reqsdm tensione' or 'reqsdm corrente' or ......

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (!isset($argv[1])) {
   die("Abording: no valid argument given.\n");
      } elseif ($argv[1] == 'tensione') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_1\(" | grep "*V)"');
      } elseif ($argv[1] == 'corrente') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_2\(" | grep "*A)"');
      } elseif ($argv[1] == 'cospi') {
         $outstr =  exec('cat /dev/shm/metern1.txt | egrep "^1_4\(" | grep "*F)"');
      } elseif ($argv[1] == 'esptemp') {
         $outstr =  exec('curl -s http://192.168.0.193/json?tasknr=2 | egrep \'"Temperature":\' | egrep -o \'[0-9]*(\.)?[0-9]*\'');
         $outstr = "20($outstr*°C)";
      } elseif ($argv[1] == 'espdht22t') {
         $outstr =  exec('curl -s http://192.168.0.193/json?tasknr=3 | egrep \'"Temperature":\' | egrep -o \'[0-9]*(\.)?[0-9]*\'');
         $outstr = "21($outstr*°C)";
      } elseif ($argv[1] == 'espdht22u') {
         $outstr =  exec('curl -s http://192.168.0.193/json?tasknr=3 | egrep \'"Humidity":\' | egrep -o \'[0-9]*(\.)?[0-9]*\'');
         $outstr = "22($outstr*%)";
      } elseif ($argv[1] == 'DStemp9') { //temp solaio meter9
         $outstr =  exec('curl -s http://127.0.0.1:8000/devices/temp1/sensor/temperature/c');
         $outstr = "9($outstr*°C)";
      } elseif ($argv[1] == 'DStemp10') { //temp mandata riscaldamento meter10
         $outstr =  exec('curl -s http://127.0.0.1:8000/devices/temp2/sensor/temperature/c');
         $outstr = "10($outstr*°C)";
   } else {
    die("Usage: reqsdm (tensione|corrente|cospi|esptemp)\n");
   }
echo "$outstr";
?>
Last edited by megamarco83 on Tue Feb 21, 2017 2:38 pm, edited 1 time in total.

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

Re: import value from ESP8266 - ESPeasy

Post by jeanmarc » Tue Feb 21, 2017 2:24 pm

you're welcome ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest