Post your best Lua script examples here

User avatar
By Theo
#6821 Just an other example that will find all DS18B20 and read the temperatures that will be printed to the serial port every 10 seconds.

Code: Select all-- 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 )

User avatar
By quantalume
#7213 I've been working with the ds18b20.lua script found in the latest nodeMCU github code. The first time I read the temperature, it always returns 85 degrees. It turns out that 85 is the default, power-up value of the sensor. In looking at the code below, there was at one point a delay which waited a sufficient amount of time to allow the temperature measurement to take place before reading the value. It appears that, by removing this delay, what happens now is readNumber() returns the temperature read during the previous call, or 85 if it's the first call. What was the thinking behind removing this delay? There is a procedure to monitor for temperature conversion completion, but it cannot be used in parasitic power mode.


Code: Select all        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
User avatar
By sancho
#7216 The 1 second delay is there to allow sufficient time for slow temperature sampling - the datasheet suggests 750ms for 12bit measurement, 1s is just a precaution (on my setup with 16 sensors on one wire with parasite power, the 750ms is usualy not enough).
However, I should point out that this is not my code ;)
User avatar
By ChrixXenon
#7436 I can't get this to do anything for me at all.

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.