Pro tip: the “shield” on ESP8266MOD (like ESP12 etc) is grounded, and is easy to solder to if you're lazy.
Pro tip 2 / Warning: If you are new to NodeMCU, you need to get the Float version. NOT the integer version ;)
Data to GPIO4, VCC to 3.3v, GND to GND.
Connect a 4.7k resistor between 3.3v and GPIO4.
Connect 7.5k (R1) between ADC and Battery VCC.
Connect 1k (R2) between ADC and GND.
If you need more accuracy, please use different resistors for the voltage divider. As I wanted it to be able to measure up to 8.3v
Convert 10bit ADC back to original voltage: adc.read(0)/1024/R2*(R1+R2)
Example: adc.read(0)/1024/1000*(7500+1000)
Connect GPIO16 to RST with a wire or something is all that is needed :)
There are some people on the internet, that claims you need to conenct both GPIO0 and GPIO2 with a resistor to VCC for it being able to wake from deep sleep. I tried this, and it has the opposite effect.
t = require("ds18b20")
t.setup(2)
addrs = t.addrs()
tmr.alarm(1,1000, 1, function()
if (wifi.sta.getip()==nil or t.read() == 85) then
print("Waiting for a WiFi connection or the ds18b20.")
else
print("New IP address is "..wifi.sta.getip())
print("Temperature: "..t.read().."'C")
vbatt = 0
i=0
while (i<10) do
vbatt=vbatt+adc.read(0)/1024/1000*(7500+1000)
i=i+1
end
tmr.stop(1)
conn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, pl) node.dsleep(300000000) end) -- 5 minutes
conn:connect(8888,"ws.mathias.local")
conn:send(GET /sensor?type=temp&id="..node.chipid().."&value="..t.read().."&vbatt="..(vbatt/10).." HTTP/1.1\r\nHost: ws.mathias.local\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end
end)
--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <[email protected]>
-- 2015/02/14 sza2 <[email protected]> Fix for negative values
--------------------------------------------------------------------------------
-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 2
--------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------
C = 'C'
F = 'F'
K = 'K'
function setup(dq)
pin = dq
if(pin == nil) then
pin = defaultPin
end
ow.setup(pin)
end
function addrs()
setup(pin)
tbl = {}
ow.reset_search(pin)
repeat
addr = ow.search(pin)
if(addr ~= nil) then
table.insert(tbl, addr)
end
tmr.wdclr()
until (addr == nil)
ow.reset_search(pin)
return tbl
end
function readNumber(addr, unit)
result = nil
setup(pin)
flag = false
if(addr == nil) then
ow.reset_search(pin)
count = 0
repeat
count = count + 1
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
ow.reset_search(pin)
end
if(addr == nil) then
return result
end
crc = ow.crc8(string.sub(addr,1,7))
if (crc == addr:byte(8)) then
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
-- print("Device is a DS18S20 family device.")
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
-- tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
-- print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
-- print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
-- print("CRC="..crc)
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32767) then
t = t - 65536
end
if (addr:byte(1) == 0x28) then
t = t * 625 -- DS18B20, 4 fractional bits
else
t = t * 5000 -- DS18S20, 1 fractional bit
end
if(unit == nil or unit == 'C') then
-- do nothing
elseif(unit == 'F') then
t = t * 1.8 + 320000
elseif(unit == 'K') then
t = t + 2731500
else
return nil
end
t = t / 10000
return t
end
tmr.wdclr()
else
-- print("Device family is not recognized.")
end
else
-- print("CRC is not valid!")
end
return result
end
function read(addr, unit)
t = readNumber(addr, unit)
if (t == nil) then
return nil
else
return t
end
end
-- Return module table
return M