JSON

How do I read out the "local JSON" data - I would like to read these values to enter into my own DB.
Is there a URL that shows the data in some json format?
thanks

icarus75's picture

Have a look at section 3.6 of the Fluksometer Manual [1]. You can access the local API through either the ethernet or wifi interface on port 8080.

[1]: http://www.flukso.net/files/flm02/manual.pdf

johan's picture

right, RTFM :-)
Another Q about this:
The manual sais that 60 datapoints (1 minute) are available real-time. Will this stay the same if the communications to the Fluksoserver is halted (haven't tried - just asking).
Related Q: How long does the fluksometer can hold measurements without internet access? And as a result, are these measurements somehow made available through the local API then?
An ultimate goal would e.g. be to have 1 datapoint every minute for 1 hour - I would then have to read the data only every hour to store it a local DB.
Is this possible? If not, can this be made available in a next firmware release - as an option?

icarus75's picture

Hi Johan,

The local API on the Fluksometer ia a real-time API. This is complementary to the server API, which allows you to fetch historical data.

When the Fluksometer is not able to report its readings to the Flukso server, it will buffer these readings in RAM. The daemon will apply aging to the data, so the older the data gets, the lower the granularity. Once it can connect to the server again, it will send out the buffered readings in a single burst.

For reading the minute values, just use the server api once every hour. See section 3.4 of the manual. Try out this curl command:

  1. curl -k -v -X GET -H "Accept:application/json" -H "X-Version: 1.0" -H "X-Token:d8a8ab8893ea73f768b66b45234b5c3a" "https://api.flukso.net/sensor/c1411c6b4f9910bbbab09f145f8533b9?interval=hour&unit=watt"

Then change the sensor and token values.

HTH,
Bart.

johan's picture

How long are the minute-values kept in the DB on the server?
This would ideally return 1440 minute-values for Oct,1st:

  1. &start=1317427200&end=1317513600&resolution=minute&unit=watt

Now, this doens't work...
Is there any way I can do this, or is the only way to get all the minute-values to poll every hour?

icarus75's picture

Hi Johan,

This is what we store for each sensor in the rrd:
resolution -> #datapoints
1min -> 1440 (= 1day)
15min -> 672 (= 7days)
day -> 365 (= 1year)
week -> 520 (= 5years)

So yes, Flukso can give you a day's worth of data in minute resolution. However, if the start parameter falls just outside of the day interval, this can cause an error. What did your API call return?

Instead of start/stop, you could query the API by setting interval=day and resolution=minute, e.g.:

  1. curl -k -v -X GET -H "Accept:application/json" -H "X-Version: 1.0" -H "X-Token:d8a8ab8893ea73f768b66b45234b5c3a" "https://api.flukso.net/sensor/c1411c6b4f9910bbbab09f145f8533b9?interval=day&resolution=minute&unit=watt"

Cheers,
Bart.

johan's picture

cool - OK, I'll cron a curl every few hours - this way, I can have all data into my own mysql.
Goal is to incorporate this data into my solar-panel-website: hofmans.be/pv - feel free to take a look and comment.

icarus75's picture

Nice work! Did you put your code in a repository somewhere?

johan's picture

Thanks!
Nope, never worked with repositories before - I'll check and see where I can put it - tbc.

dijka007's picture

just so I could put my code somewhere:

This script retrieves some values from flukso.net and inserts them in a mysql table. No temporary files, all done in memory.

I have some difficulty translating the timestamp to my local timezone, so in this example I just substracted 5 hours to get the right date, the 5 hours just to be sure.

Retrieved the value for one day, in kwh/year, so to get the value for that day just divide by 365 or 366.

  1. #!/usr/bin/php -q
  2. <?php
  3. /*
  4. #
  5. # retreive from flukso.net the records of one sensor
  6. # and insert them in a mysql table
  7. #
  8. # table contains two columns
  9. # ts    - datetime not null (primary key)
  10. # value - smallint not null
  11. #
  12. # Using PHP FLukso API class from Geert Van Bommel
  13. #
  14. */
  15.  
  16. # your Flukso settings
  17.  
  18. $sensorid='sensor-id-here';
  19. $token='token-here';
  20.  
  21. $interval='month';      # Should be one of {hour, day, month, year, night}
  22. $unit='kwhperyear';             # Should be one of {watt, kwhperyear, eurperyear, audperyear, lpermin, lperday, m3peryear}
  23. $resolution='day';   # Should be one of {minute, 15min, hour, day, week, month, year, decade, night}
  24.  
  25. # change these values to adjust for your database
  26. $db_user = "database-user";
  27. $db_pass = "database-password";
  28. $db_host = "localhost";
  29. $db_name = "database-name";
  30. $table   = "table-name";
  31.  
  32.  
  33.  
  34.  
  35. class Flukso {
  36.         private $sensorid, $token, $interval, $unit, $resolution;
  37.         private $url='https://api.flukso.net/sensor/';
  38.        
  39.         public function __construct($u_sensorid, $u_token, $u_interval, $u_unit, $u_resolution) {
  40.                 $this->sensorid = $u_sensorid;
  41.                 $this->token = $u_token;
  42.                 if (empty($u_interval)) {
  43.                         $this->interval = 'hour';
  44.                 } else {
  45.                         $this->interval = $u_interval;
  46.                 }      
  47.                 if (empty($u_unit)) {
  48.                         $this->unit = 'watt';
  49.                 } else {
  50.                         $this->unit = $u_unit;
  51.                 }
  52.                 $this->resolution = $u_resolution;
  53.                 $this->getdata();
  54.         }
  55.  
  56.         private function getdata() {
  57.                 // headers according to <a href="http://www.flukso.net/content/jsonrest-api-released<br />
  58. " title="http://www.flukso.net/content/jsonrest-api-released<br />
  59. ">http://www.flukso.net/content/jsonrest-api-released<br />
  60. </a>            $header=array();
  61.                 $header[]="Accept: application/json";
  62.                 $header[]="X-Version: 1.0";
  63.                 $header[]='X-Token: '.$this->token;
  64.  
  65.                 $request=$this->url.$this->sensorid.'?interval='.$this->interval.'&unit='.$this->unit.'&resolution='.$this->resolution;
  66.                
  67.                 $ch=curl_init();
  68.                 curl_setopt($ch,CURLOPT_URL,$request);
  69.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  70.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  71.                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  72.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  73.                 $this->data=curl_exec($ch);
  74.                 curl_close($ch);
  75.                
  76.         }
  77.         public function __toString() {
  78.                 return $this->data;
  79.         }
  80.  
  81. }
  82.  
  83.  
  84.  
  85. $fluksodata = new Flukso($sensorid, $token, $interval, $unit, $resolution);
  86.  
  87. $fluksodata=json_decode($fluksodata);
  88.  
  89. # Open the database connection
  90.  
  91. $link = mysql_connect($db_host, $db_user, $db_pass)
  92.        or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to MySQL.\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  93. mysql_select_db ($db_name)
  94.        or die ("Error ".date('Y-m-d H:i:s')."Failed to connect to the database ".$db_name.".\n<b>A fatal MySQL error occured</b>.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  95.  
  96. #
  97. # Process all values
  98. #
  99. foreach($fluksodata as $value){
  100.  
  101.         #
  102.         # If the value is "nan", skip this row
  103.         #
  104.         if ( "$value[1]" != "nan" )
  105.         {
  106.                 # subtract 5 hours, to make sure we get the right date
  107.                 $corrected_ts = $value[0] - 18000;
  108.                 $formatdatetime = date('Y-m-d H:i:s', $corrected_ts);
  109.                 $year = date('Y', $corrected_ts);
  110.                 # divide value bij 365 or 366
  111.                 $number_days_this_year =  date("z", mktime(0,0,0,12,31,$year)) + 1;
  112.                 $corrected_value = $value[1] / $number_days_this_year;
  113.                 #
  114.                 # Insert row into table, ignore errors if row
  115.                 # already exists
  116.                 #
  117.                 $query = "insert ignore into $table (ts, value) values ('$formatdatetime', $corrected_value)";
  118.                 $result = mysql_query ($query)
  119.                         or die ("Error ".date('Y-m-d H:i:s')."Failed to insert ".$table." record.\nA fatal MySQL error occured.\nQuery: " . $query . "\nError: (" . mysql_errno($link) . ") " . mysql_error($link). " Affected rows is ".mysql_affected_rows($link));
  120.  
  121.         }      
  122. }
  123.  
  124.  
  125. # close database connection
  126. mysql_close($link);
  127.  
  128. ?>

Fluc's picture

Nice work, i wondering how this is showing in the database.
Do you have a example of it ?

dijka007's picture

In the comment I made a mistake, the table-column named value is a float.

I only have the device three weeks, so I'm still tinkering.

In the database I get:

ts value
2014-04-13 16.526
2014-04-14 16.1151
2014-04-15 11.8137
2014-04-16 13.2411
2014-04-17 16.1507
2014-04-18 18.2082
2014-04-19 12.4767
2014-04-20 12.5973

and these I use to calculate a value per month, by just adding them.