May 13, 2015

Using the ADC on NodeMCU (ESP8266)

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(
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:

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:
for later versions og NodeMCU firmware use:

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 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.
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")
ip = wifi.sta.getip()

function upload() 
conn = net.createConnection(net.TCP, 0) 
function(conn, payload) 
success = true
function(conn, payload)
function(conn, payload) 
conn:send("GET/ logdata.php?"
.." HTTP/1.1\r\n" 
.."Connection: keep-alive\r\n"
.."Accept: */*\r\n" 
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 
print("Opening port")

function pin1up(level)
pulse_detected = 1

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

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

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


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:
The script extracts the parameters from the url and creates an entry in a file. There is a new file create every 24 hour.
$delim = ", ";
$referer = getenv('HTTP_REFERER');
$timestamp = $_GET['timestamp'];
$counter = $_GET['counter'];
$key = $_GET['key'];

$secret = "your-key";


$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);
  echo "Invalid key\n";
  echo $key;