Firing some code to run on my Pi, when an update is received

Hi,

Spent about a week of long evenings with an emoxTX, Pi and interface and learning fast!

I want to fire my RRDtool graph with the incoming values when they are received by the RFM12Pi

I just need to run on my Pi:-

rrdtool update meter.rrd N:1:2:3:4

where 1:2:3:4 are the incoming values....

How can I add this to be run whenever emoncms is updated?  

 

Thanks in advance.

funa's picture

Re: Firing some code to run on my Pi, when an update is received

Run the command using exec. Add this line somewhere in the raspberrypi_run.php file.

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

Thanks, I cant see where the incoming values are in raspberrypr_run.php for say Ct1, ct2, ct3

They must be there somewhere? where can I find them and parse them into my command?

Sorry, really newbie here!

Jérôme's picture

Re: Firing some code to run on my Pi, when an update is received

I see at least three ways :

1/ Modify the piece of code that redirects RF data into inputs (I refer to that as gateway), that is either raspberry_run.php or rfm2pigateway.py. This is what funa suggests.

2/ Modify the piece of code that receives inputs (php) and store them into DB and does the feeding.

3/ Log those values to a feed and do something like the event module (https://github.com/emoncms/event). It is meant to send emails, you could modify it to send a command. Perhaps not the most simple.

Perhaps if you made 1/ or 2/ versatile enough, such a feature could be integrated. Why not a user-defined command with the input values as argument ?

 

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

can someone help me with my syntax please (new to PHP)

I have added this, under the "// Forward data to remote emoncms" line :-

 

        // Forward data to remote emoncms
   
      }

  // run RRDtool logging
$pieces = explode(" ", $remotedata)
// piece1 echo $pieces[0];
// piece2 echo $pieces[1];

 exec('rrdtool update powertemp.rrd N:$pieces[0]:$pieces[1]')

 

 

but it doesnt seem to work...I guess the syntax in the update statement is wrong to pass the variables?

what tools can I get to set myself up to be able to debug the PHP?

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

is this right? I have added the below, nothing happens.

It all works fine from command line, any help appreciated.

$result = file_get_contents("http://".$remotedomain."/time/local.json?apikey=".$remoteapikey);
          if ($result[0]=='t') {echo "Remote upload enabled - details correct \n"; $sent_to_remote = true; }
        }

        raspberrypi_running();

        // Forward data to remote emoncms
  
      }

  // run RRDtool logging
$pieces = explode(" ", $remotedata);
  exec("rrdtool update powertemp.rrd N:" & $pieces[0] & ":" & $pieces[1])
;

 
 
 
      if (time()-$remotetimer>30 && $sent_to_remote == true)

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

ok, so it looks like I am barking up the wrong tree :-)

Can someone confirm where are the incoming values are in the raspberry_run.php file? 

funa's picture

Re: Firing some code to run on my Pi, when an update is received

Not sure about the data variables but PHP string concatenation character is . (dot):

// run RRDtool logging
$pieces = explode(" ", $remotedata);
  exec("rrdtool update powertemp.rrd N:" . $pieces[0] . ":" . $pieces[1])
;

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

thanks.

can anyone confirm where the incoming values are and in what format?
really hard to see and code isnt commented.

Jérôme's picture

Re: Firing some code to run on my Pi, when an update is received

If unsure, add print statements in the php code to see what the variables contain before trying to execute anything. Otherwise, you're shooting in the dark.

If you feel more comfortable with python, you can try to modify the .py gateway. If you do so, you'd rather use the dev branch version, it is much more up to date. Don't try this if you think moving to the dev branch is likely to add more complexity that it will help you.

Edit: I just realized that Trystan merged dev into master, and he probably will more regularly than I thought. Master branch is fine, then.

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

so when I add print statements to raspberry_run.php, where do i see the output exactly?

Jérôme's picture

Re: Firing some code to run on my Pi, when an update is received

Run the script manually :

- comment or remove the crontab line

- kill the running process or reboot

- launch command "php raspberrypi_run.php"

You should see the output in the terminal. Even if you don't modify anything, there are prints already.

I peeked quickly into the python script. By design, it is not meant to do what you intend. Wouldn't be impossible, but could be a bit tricky for someone with no knowledge of the code. You may want stick to the php script.

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

Thanks for all the help.

I have it all working, all you need to do is just need add a few lines of code into the raspberrypi_run.php file, install rrdtools, change the crontab and it is all working.  simple, logging without worrying about a SQL database filling up, some tasty graphs ( http://oss.oetiker.ch/rrdtool/gallery/ ) and a full write up to follow.

 

 

 

 

Jérôme's picture

Re: Firing some code to run on my Pi, when an update is received

You're welcome.

You may want to share your changes so that anyone can do the same.

mattnj's picture

Re: Firing some code to run on my Pi, when an update is received

i will do. once its finished up

Jérôme's picture

Re: Firing some code to run on my Pi, when an update is received

Thanks.

Perhaps a page here would be appropriate :

http://wiki.openenergymonitor.org/

or a paragraph on the Pi page.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.