-- Read temperature from all DS18B20 sensors on the OneWireBus
-- Using NodeMCU 0.9.5 build 20150108
function prinTemp ()
local pin = 3 -- use GPIO-0
local tempL, tempH, temp = 0
local power, nopower = 1, 0
ow.setup(pin)
if ow.reset(pin) == 1 then
counter = counter + 1
print("Start conversion: " .. counter)
-- Select all devices on the owbus and start conversion on all devices
-- power the bus during conversion
ow.skip(pin)
ow.write(pin, 0x44, power)
tmr.wdclr() -- keep the watchdog happy
tmr.delay(750000) -- wait max 750 msec for conversion to complete
-- start searching DS18B20 (0x28) devices only
ow.reset_search(pin)
ow.target_search(pin,0x28)
-- search the first device
local addr = ow.search(pin)
-- and loop through all devices
while addr do
tmr.wdclr()
-- print(addr:byte(1,8))
ow.reset(pin)
ow.select(pin, addr) -- select the found sensor
ow.write(pin, 0xBE, nopower) -- READ_SCRATCHPAD
-- read temperature LSB + MSB bytes from sensor
temp = ( ow.read(pin) + ow.read(pin) * 256 ) * 625
-- stop reading rest of scratchpad ram after the first 2 bytes
ow.reset(pin)
-- floating point temperature with 1 decimal
tempH = temp / 10000
tempL = ( temp % 10000 ) / 1000
-- round up 1st decimal using second decimal
round = ( temp % 1000 ) / 100
if round >= 5 then
tempL = tempL + 1
end
-- print("T=" .. tempH .. "." .. tempL .. " C - " .. temp)
print("T=" .. tempH .. "." .. tempL)
-- search next device
addr = ow.search(pin)
end
else
print("No device present on the OneWireBus.")
end
end
-- Main progrom starts here
counter = 0
-- set timer to print temperature every 10 seconds
tmr.alarm(1, 10 * 1000, 1, prinTemp )
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
However, I should point out that this is not my code
I have executed the WiFi connect commands outside this code, so that the node connects on restart, and I confirm that it i by listing DHCP clients at my hub.
pin 4 is the RST pin - right? I've tried GPIO- and 1 and it makes no difference - nothing.
If I add debug print()s, I can confirm that the code is running but none of the print()s in the listen() function execute.
Any thoughts would be welcome.