The ADC on ESP8266 is poorly documented. It is note even mentioned in the datasheet. I did some measurements with a variable power supply to understand how it works. The experiments are done with the NodeMCU firmware 0.95. File: nodemcu_20150213.bin (link)
What pin is the ADC input?
TOUT on an ESP8266 module.
A0 on a NodeMCU Devkit.
What is the range of the NodeMCU ADC input?
Measured range: 0 - Vdd. (About 3.1 V in this case)
Number of bits: 10 bit (Codes: 0-1024)
Note that the NodeMCU board has a voltage divider on the ADC input. The ESP8266 module it self has 0 - 1.0 V input range.
Readings are done with the command: print(adc.read(0))
0.5 V => 167
1.0 V => 330
1.5 V => 498
2.0 V => 666
2.5 V => 828
3.0 V => 995
3.1 V => 1024 (saturated)
I did a plot of the values and it looks linear.
Do I need to power down WiFi before using the ADC?
There seems that it at least has been a problem in earlier versions of the SDK but I did not have any problems with interference when using WiFi and the ADC at the same time. If you see any issues try to power down the radio with:
wifi.sleeptype(wifi.MODEM_SLEEP)
Can I measure the supply voltage?
It is possible to read the supply voltage. It is done internaly in the chip so thre is no need to connect it to the ADC input. Use the NodeMCU command:
print(node.readvdd33())
for later versions og NodeMCU firmware use:
print(adc.readvdd33())
It returns a value in mV. I got 3123. Be careful to use this call since it is buggy and will cause reboots quite often when called!
May 13, 2015
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();
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;
}
?>
$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;
}
?>
April 28, 2015
NodeMCU tmr.time() and tmr.now() bugs
The NodeMCU firmware has some nasty bugs in the tmr.time() and tmr.now() functions so don't use them!
From what I have observed in v0.95 and v0.9.6-dev_20150406 the following happens.
tmr.time(), that returns system time in seconds, makes a jump after 25430 seconds (about 7 hours).
25427
25428
25429
25430
27043
27044
27045
27046
27047
tmr.now(), that returns system time in us, does at some point in time freeze and returns the same value for every call.
Theses bugs makes the functions unusable. The bugs seems to originate from the ESP8266 SDK used for the build rather that the implementation of NodeMCU it self. This will probably make it hard to fix. Luckily there is is a workaround since tmr.alarm() works fine. With tmr.alarm() you can easily create your own time measuring function. At least if you are fine with ms resolution. Hers thee code:
perid_ms = 100
timestamp = 0
tmr.alarm(0, period_ms, 1, function()
timestamp = timestamp + 1
end )
Then you can use the variable timestamp in you code to make time measurements.
Subscribe to:
Posts (Atom)