Live Electricity Usage
1 minute read •
So, following on from my recent post on updating my Currentcost code I now have every update coming from the Currentcost unit popping directly into the database, every 6 seconds.
I had become aware of the Highcharts charts javascript and when reading through their Live Charts Demo decided that this was just waiting to be played with.
I now have essentially the same graph that they have in that demo, and the page HTML is very similar. The difference is obviously to call my code instead, and also a change to refresh every 6 seconds:
My AJAX call is also to some PHP, but mine looks up the latest data from the database with a simple SQL statement:
select unix_timestamp(time) as time,data_value from data order by time desc limit 1
I quickly realised that this would slap the database, and more so if more than one person was viewing the page. That would be unnecessary, and so I installed memcached and the php5 memcached module.
Edit: Slight tweak to the code, as there was some obvious lazyness that I tidied up.
Enjoy.