May 8, 2015

Power Meter pulse logger with ESP8266 running NodeMCU


This device is installed in my home to monitor the usage of electricity. It counts the pulses from the meter and produces a log file with number of pulses and a time stamp that can later be analyzed. The hardware is quite simple. The NodeMCU development kit board with an ESP8266 running the NodeMCU firmware is connected to a phototransistor with an pull-down resistor. The firmware and my lua script seems stable since it has been running for more than 10 days without problems.




The phototransistor used is PT204-6C. It is aimed at the pulse windows on the power meter.

I also working on a php script that produces a diagram.
Below is the NodeMCU lua code. Pin 1 is set to generate an interrupt on the rising edge. I had some issues with pulses counted multiple times probably due to bounce in the input signal. I have solved this by only inrement the counter if there is a gap of 20 ms or more between interrupts since the pulse is about 10 ms wide. This is done with the method described in my previous post. Every 60 seconds it also loads a webpage with a php script that writes to a log file.
elog.lua
pin = 1
led = 0
min_pw_ms = 20
upload_rate_ms = 60000

pulse_detected = 0
timestamp = 0
counter = 0
conn = nil

gpio.mode(led, gpio.OUTPUT)
gpio.mode(pin, gpio.INT)

gpio.write(led, gpio.LOW)

if not wifi.sta.getip() then
print("Connecting to wifi")
wifi.setmode(wifi.STATION)
    wifi.sta.config("net_name","net_pwd")
ip = wifi.sta.getip()
print(ip)
end

function upload() 
conn = net.createConnection(net.TCP, 0) 
conn:on("receive", 
function(conn, payload) 
success = true
print(payload)
end)
conn:on("disconnection", 
function(conn, payload)
print('\nDisconnected')
end)
conn:on("connection", 
function(conn, payload) 
print('\nConnected') 
conn:send("GET/ logdata.php?"
.."timestamp="..timestamp
.."&key=your-key"
.."&counter="..counter
.." HTTP/1.1\r\n" 
.."Host: your_host.com\r\n" 
.."Connection: keep-alive\r\n"
.."Accept: */*\r\n" 
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 
.."\r\n")
end)
print("Opening port")
conn:connect(80,'your_host.com') 
end

function pin1up(level)
pulse_detected = 1
end

function maintask()
        print("Counter is:"..counter)
        if not wifi.sta.getip() then
            print("Connecting to AP, Waiting...") 
        else  
            gpio.write(0, gpio.HIGH)
            print("Uploading to server...")
            upload()
       end
end

function pulsetask()
timestamp = timestamp + 1
if pulse_detected == 1 then
counter = counter + 1
pulse_detected = 0
end
end

gpio.trig(pin, "up", pin1up)
tmr.alarm(0, upload_rate_ms, 1, maintask);
tmr.alarm(1, min_pw_ms,      1, pulsetask);

maintask();

This is the php script on the server. It has a simple security feature with a secret key to avoid bots bloating the log. The firmaware loads the url:
http://your-host.com/logdata.php?timestamp=1111&key=your-key&counter=8888
The script extracts the parameters from the url and creates an entry in a file. There is a new file create every 24 hour.
logdata.php
<?php
$delim = ", ";
$referer = getenv('HTTP_REFERER');
$timestamp = $_GET['timestamp'];
$counter = $_GET['counter'];
$key = $_GET['key'];

$secret = "your-key";

date_default_timezone_set("Europe/Stockholm");

$entry = date("Y-m-d") . $delim . date("H:i:s") . $delim . $timestamp . $delim . $counter . "\n";
         
echo $entry;

$file = "datalog_". date("Y-m-d") . ".txt";

if ($key === $secret)
{
  echo "Valid key\n";
  file_put_contents($file, $entry, FILE_APPEND);
}
else
{
  echo "Invalid key\n";
  echo $key;
}
?>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.