OpenWRT offers event driven handling of scripts execution. GPIOs can be configured to trigger events, and all that needs to be configured is adding a patch to enable it for a required GPIO - in my case it was two output pins used for controlling LEDs repurposed as TTL inputs. Because the pulse signals produced by power meter are very short, I've added T flip-flop in between and wrote measurement script to ignore level, but measure time between edges. My house power meter produces 1000 pulses per 1kWh. Dividing 3600000 by the period time gives the momentary power consumption. For more details regarding hotplug in OpenWRT see howto
Hotplug generates log entries:
root@igor:~/bin# logread -f
Jan 29 11:58:41 igor user.notice root: 810.811W - 18.1776C/h (4440ms) *
Jan 29 11:58:46 igor user.notice root: 810.628W - 18.1735C/h (4441ms) *
Jan 29 11:58:50 igor user.notice root: 814.48W - 18.2598C/h (4420ms) *
Jan 29 11:58:54 igor user.notice root: 810.628W - 18.1735C/h (4441ms) *
Jan 29 11:58:59 igor user.notice root: 810.811W - 18.1776C/h (4440ms) *
Jan 29 11:59:03 igor user.notice root: 814.295W - 18.2557C/h (4421ms) *
Jan 29 11:59:08 igor user.notice root: 810.628W - 18.1735C/h (4441ms) *
Jan 29 11:59:12 igor user.notice root: 810.811W - 18.1776C/h (4440ms) *
To display current power consumption I've added I2C bus by repurposing two of GPIOs, that originally were driving LEDs indicating ethernet ports activity and speed. To enable I2C add module, and configure it with /etc/modules.d/58-i2c-gpio-custom file:
i2c-gpio-custom bus0=0,20,21
where 20 and 21 refers to GPIOs indexes for SDA and SCL lines. See OpenWRT documentation. After connecting SAA chip the display can be used to show any 4 digit figure. I cut rectangular hole in the plastic casing, leaving the semi-transparent cover untouched. This way the display is dimmed, and any imperfections in cutting around the display module are not visible anymore. Display is mounted upside down since the router is hanged this way on the wall just next to the power meter.
As a bonus, rrd is called every other time the hotplug is triggered, to gather data in round-robin database. The database is stored in ramdisk to minimize flash-rom activity, and its contents covers
- last 3 hours - with 5 minutes averaging,
- last 24 hours - with 20 minutes averaging,
- last 3 days - with 20 minutes averaging,
- last 7 weeks - with 1 hour averaging,
- last 3 months - with 1 day averaging,
- one last year - with 1 day averaging
To finalize the httpd daemon is used to provide lightweight means to access graphics via network.
As a bonus, the router still acts as a network sharing device.