Hi all,
This weekend, I managed to work a little on measuring gas, electricity and water usage. Based on the rugged example from Tinkerforge I wrote a little explorative python script. It is on Github. It is not well documented or properly written, but it works.
The setup is pretty simple. I have a master brick with a connected Ethernet brick and three sensors (line, hall and ambient light). The line bricklet is used for measuring water consumption, the hall for gas and the ambient light for electricity.
The hall bricklet was the easiest. Every time it sees a magnet pass by, it does a callback to the computer running the python scrip. I know that every rotation corresponds to 10 liters of gas (or 0.01m3).
The ambient light bricklet was a little more difficult. As I said in an earlier post which matthiku also referred to, just getting a callback when the ambient light crosses some threshold won't work. I won't get into too much detail, but you will either get multiple callbacks due to the duration of the light flash or when you set the threshold too high, you will miss some flashes.
I solved this by assuming that two separate flashes will be at least 400 ms apart (the debounce period). This solves the problem as you can see in the table below.
2014-06-01 12:52:00.114766 - 1 Wh
2014-06-01 12:52:09.664464 - 1 Wh
2014-06-01 12:52:19.216056 - 1 Wh
2014-06-01 12:52:28.765721 - 1 Wh
2014-06-01 12:52:38.366049 - 1 Wh
2014-06-01 12:52:47.916696 - 1 Wh
2014-06-01 12:52:57.467214 - 1 Wh
2014-06-01 12:53:07.117634 - 1 Wh
The line bricklet was more complicated. If you look at the graph of the reflectivity, you can see that there are two different minimums.
What happens in the code is that we wait until we reach a maximum or minimum. Then we change the callback for the opposite. If the callback for the maximum is followed by the callback for the minimum, then we know that 100-mL (0.0001m3) is used (we wait for the falling flank).
def cb_line(self, *args, **kwargs):
state = self.line.get_reflectivity_callback_threshold()
if state[0] == Line.THRESHOLD_OPTION_SMALLER:
print('{} - 0.0001m3 water'.format(datetime.now()))
self.line.set_reflectivity_callback_threshold('>', 3900, 0)
elif state[0] == Line.THRESHOLD_OPTION_GREATER:
self.line.set_reflectivity_callback_threshold('<', 3880, 0)
When I turn on the hot water, then I get something like this:
2014-06-01 13:08:06.897621 - 0.0001m3 water
2014-06-01 13:08:10.665532 - 1 Wh
2014-06-01 13:08:15.774149 - 0,01m3 gas
2014-06-01 13:08:18.315497 - 1 Wh
2014-06-01 13:08:18.429275 - 0,01m3 gas
2014-06-01 13:08:21.208779 - 0.0001m3 water
2014-06-01 13:08:25.915624 - 1 Wh
2014-06-01 13:08:28.496903 - 0,01m3 gas
2014-06-01 13:08:31.145113 - 0,01m3 gas
2014-06-01 13:08:33.566193 - 1 Wh
2014-06-01 13:08:35.652654 - 0.0001m3