Temperatur measurement via DS18B20 added
This commit is contained in:
parent
c73478d86e
commit
999c8ae3ac
@ -36,12 +36,14 @@ Mandatory:
|
|||||||
* GPIO0 factory reset (long during operation)
|
* GPIO0 factory reset (long during operation)
|
||||||
Optinal:
|
Optinal:
|
||||||
* ADC VT93N2, 48k light resistor
|
* ADC VT93N2, 48k light resistor
|
||||||
|
* GPIO4 DS18B20 Temperatur sensor
|
||||||
|
|
||||||
## MQTT Interface
|
## MQTT Interface
|
||||||
### Status
|
### Status
|
||||||
* **basetopic**/brightness **Current brightness in percent**
|
* **basetopic**/brightness **Current brightness in percent**
|
||||||
* **basetopic**/background **Current background color**
|
* **basetopic**/background **Current background color**
|
||||||
* **basetopic**/row1 **Current background color**
|
* **basetopic**/row1 **Current background color**
|
||||||
|
* **basetopic**/temp **Temperatur**
|
||||||
|
|
||||||
### Commands
|
### Commands
|
||||||
* **basetopic**/cmd/single
|
* **basetopic**/cmd/single
|
||||||
|
138
ds18b20.lua
Normal file
138
ds18b20.lua
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- DS18B20 one wire module for NODEMCU
|
||||||
|
-- NODEMCU TEAM
|
||||||
|
-- LICENCE: http://opensource.org/licenses/MIT
|
||||||
|
-- Vowstar <vowstar@nodemcu.com>
|
||||||
|
-- 2015/02/14 sza2 <sza2trash@gmail.com> 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 = 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)
|
||||||
|
-- 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(unit == nil or unit == C) then
|
||||||
|
t = t * 625
|
||||||
|
elseif(unit == F) then
|
||||||
|
t = t * 1125 + 320000
|
||||||
|
elseif(unit == K) then
|
||||||
|
t = t * 625 + 2731500
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
t = t / 100
|
||||||
|
-- print("Temperature="..t1.."."..t2.." Centigrade")
|
||||||
|
-- result = t1.."."..t2
|
||||||
|
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
|
2
init.lua
2
init.lua
@ -51,6 +51,8 @@ tmr.alarm(1, 5000, 0, function()
|
|||||||
(file.open("timecore.lua")) or
|
(file.open("timecore.lua")) or
|
||||||
(file.open("wordclock.lua")) or
|
(file.open("wordclock.lua")) or
|
||||||
(file.open("displayword.lua")) or
|
(file.open("displayword.lua")) or
|
||||||
|
(file.open("mqtt.lua")) or
|
||||||
|
(file.open("ds18b20.lua")) or
|
||||||
(file.open("telnet.lua"))
|
(file.open("telnet.lua"))
|
||||||
) then
|
) then
|
||||||
c = string.char(0,128,0)
|
c = string.char(0,128,0)
|
||||||
|
27
mqtt.lua
27
mqtt.lua
@ -1,6 +1,8 @@
|
|||||||
-- Global variable
|
-- Global variable
|
||||||
m=nil
|
m=nil
|
||||||
mqttConnected = false
|
mqttConnected = false
|
||||||
|
-- Temp:
|
||||||
|
t=nil
|
||||||
|
|
||||||
function handleSingleCommand(client, topic, data)
|
function handleSingleCommand(client, topic, data)
|
||||||
if (data == "ON") then
|
if (data == "ON") then
|
||||||
@ -59,6 +61,21 @@ function parseBgColor(data, row)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function readTemp()
|
||||||
|
if (t ~= nil) then
|
||||||
|
addrs=t.addrs()
|
||||||
|
-- Total DS18B20 numbers
|
||||||
|
sensors=table.getn(addrs)
|
||||||
|
local temp1=0
|
||||||
|
if (sensors >= 1) then
|
||||||
|
temp1=t.read(addrs[0])
|
||||||
|
end
|
||||||
|
return temp1
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- MQTT extension
|
-- MQTT extension
|
||||||
function registerMqtt()
|
function registerMqtt()
|
||||||
m = mqtt.Client("wordclock", 120)
|
m = mqtt.Client("wordclock", 120)
|
||||||
@ -113,6 +130,12 @@ function startMqttClient()
|
|||||||
if (mqttServer ~= nil and mqttPrefix ~= nil) then
|
if (mqttServer ~= nil and mqttPrefix ~= nil) then
|
||||||
registerMqtt()
|
registerMqtt()
|
||||||
print "Started MQTT client"
|
print "Started MQTT client"
|
||||||
|
if (file.open("ds18b20.lc")) then
|
||||||
|
t=require("ds18b20")
|
||||||
|
t.setup(2) -- GPIO4
|
||||||
|
readTemp() -- read once, to setup chip
|
||||||
|
print "Setup temperature"
|
||||||
|
end
|
||||||
oldBrightness=0
|
oldBrightness=0
|
||||||
oldTemp=0
|
oldTemp=0
|
||||||
tmr.alarm(5, 5001, 1 ,function()
|
tmr.alarm(5, 5001, 1 ,function()
|
||||||
@ -120,9 +143,13 @@ function startMqttClient()
|
|||||||
local temp = nil
|
local temp = nil
|
||||||
if (t ~= nil) then
|
if (t ~= nil) then
|
||||||
temp=readTemp()
|
temp=readTemp()
|
||||||
|
print(tostring(temp) .. "°C")
|
||||||
end
|
end
|
||||||
if (oldBrightness ~= briPercent) then
|
if (oldBrightness ~= briPercent) then
|
||||||
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
|
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
|
||||||
|
elseif (temp ~= nil and temp ~= oldTemp) then
|
||||||
|
oldTemp = temp
|
||||||
|
m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0)
|
||||||
else
|
else
|
||||||
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
|
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user