DS18B20 nodemcu lua library with negative celsius reading on ESP8266

Im tried everywhere on the net and dont find a usable library which can use on ESP8266 which can calculate the negative temperatures.

The basics is comming from the Nodemcu examples. The changes is basicly on the middle of the code.

About the changes:

The Lua by default not supporting the bitwise negation, have a C compiled module in the Nodemcu named bit which have bit.bnot function, if that was enabled in your firmware. ­čÖé The substraction from 65536 is a negation from┬áthe 16bit unsigned number which is comming from the DS18B20.

Hardware:

 

Demo code to use the module:

init.lua:

-- IOT4 Ltd 
-- LICENCE: http://opensource.org/licenses/MIT
-- Roboert Juhasz <robi@iot4.eu>
-- IOT4 Ltd www.iot4.eu

gpio0 = 1 --gpio5
t = require("ds18b20")
t.setup(gpio0)
addrs = t.addrs()


tmr.alarm(0,5000,1,function()
  temp= t.read(nil,0) --read in celsius, 1 fareheit,2 kelvin
  print(temp)
  collectgarbage()

end)

Just simply upload this 2 file into the Nodemcu ESP8266.

 

ds18b20.lua :

--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <vowstar@nodemcu.com>
-- Roboert Juhasz <robi@iot4.eu>
-- IOT4 Ltd www.iot4.eu
--------------------------------------------------------------------------------

-- 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 = 9
--------------------------------------------------------------------------------
-- 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 = 0
F = 1
K = 2
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)
    data = nil
    data = string.char(ow.read(pin))
    for i = 1, 8 do
        data = data .. string.char(ow.read(pin))
    end
	
    crc = ow.crc8(string.sub(data,1,8))
    if (crc == data:byte(9)) then
        if(unit == nil or unit == C) then
            x = (data:byte(1) + data:byte(2)* 256) 
            if (x<2048) then
                t = x * 625
             else 
                 t= 65536 - x -- no negate in lua and the bit.bnot is not working here
                 t= t*-625
             end
        elseif(unit == F) then
            x = (data:byte(1) + data:byte(2)* 256) 
            t = (x) * 1125 + 320000
        elseif(unit == K) then
	       x = (data:byte(1) + data:byte(2)* 256) 
		   if (x<2048) then
                t = x * 625 + 2731500
             else 
                 t= 65536 - x -- no negate in lua and the bit.bnot is not working here
                 t= t*-625
				 t = t + 2731500
             end
        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

 

DS18B20 nodemcu lua library with negative celsius reading on ESP8266
Tagged on:                     

Leave a Reply

Your email address will not be published. Required fields are marked *