meter

Jednostavno rešenje za praćenje potrošnje preko LED impulsne diode na samom strujomeru.
Fotoćelija prati impulse i pomoću skripte generiše stanje.

Meter2Pi

file: electric.py

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import subprocess
import smbus
bus = smbus.SMBus(0)
from datetime import timedelta

meterIps = [12]

def _setup():
  GPIO.setmode(GPIO.BOARD)
  for meterIp in meterIps:
    GPIO.setup(meterIp, GPIO.IN, GPIO.PUD_UP)

def _storeUpCountDelta():
  with open('/proc/uptime', 'r') as f:
    up = float(f.readline().split()[0])
  with open('/home/pi/inetpub/upCountDelta.txt', 'a+') as f:
    f.seek(0)
    arrLine1 = f.readline().split()
    if len(arrLine1) > 1:
      delta = up - float(arrLine1[0])
      count = 1 + int(arrLine1[1])
      f.seek(0)
      f.truncate()
      f.write ('{} {} {:.2f} //up count interval'.format(up, count, delta))

if __name__ == "__main__":
  _setup()
  while 1:
    val = GPIO.input(meterIps[0])
    if val:
      state = 1
    else:
      if state == 1:
        _storeUpCountDelta()
      state = 0
    time.sleep(0.001)

 

file: meter.php

<?php
$ip = file_get_contents('_ip.txt');

$file = file_get_contents('http://' . $ip . '/upCountDelta.txt');
$fileArr = explode("\n", $file);
$lineArr = preg_split('/\s+/', $fileArr[0]);

$counter = 0;
$counter = $lineArr[1];
$counter = $counter / 1000;

$meter = 0;
$meterFile = "meter.txt";
if (file_exists($meterFile) && is_readable($meterFile)) {
	$fh = fopen($meterFile, "r");
	while (!feof($fh)) {
		$meterArray = split("\|", trim(fgets($fh)));
		if (count($meterArray) == 3) {
			$meter = intval($meterArray[2]);
			$meterCount = intval($meterArray[1])/1000;
		}
	}
	fclose($fh);
}

$meterReading = number_format($meter + $counter - $meterCount, 3, '.', '');

$pPerKW = 0.14931;
$secsPerWatt = floatval($lineArr[2]);
if ($secsPerWatt > 0) {
	$kWh = round(60 * 60 * 0.001 / $secsPerWatt, 3);
	$cost = round($kWh * $pPerKW, 3);
	echo "$meterReading $cost $kWh $pPerKW //reading &pound;/h kWh tariff";
} else {
	echo $file;
}
?>