Post your best Lua script examples here

User avatar
By MikeVanDyke
#39718 Hi folks!

I've currently been playing around with my ESP-01 and an DS18B20 and I go to say, this combo is really nice - though it is really slow.
When I was browsing the DS18B20's datasheet i noticed that it take 750ms for it to make one temperature conversion.
I was trying to get one measurement per second out of it, so this is kinda slow.
But reading further i noticed that the DS18B20 can be set to measure at 9/10/11 or 12bit resolution - whereby the lower resolutions work way faster in my understanding.
So I tried to get the DS18B20 to work with a lower resolution but wasn't able to do it so far.
I cant figure out what byte code to send to the device to set it's config to a lower resolution - can someone pls help me here!

What I tried so far is what I learned by studying the examples, the ds18b20.lua library and the API wiki.

This function works like a charm and reads tthe scratchpad of the DS18B20:
Code: Select allfunction read_scratchpad(pin)
   ow.setup(pin)
   ow.reset(pin)
   ow.skip(pin)
   ow.write(pin,0xBE,1)
   data = string.char(ow.read(pin))
   for i = 1, 8 do
   data = data .. string.char(ow.read(pin))
   end
   
   local temp = "0x"..string.format("%#X", string.byte(data, 1)).."   "
   for i = 2, 9 do
      temp = temp.."0x"..string.format("%#X", data:byte(i)).."   "
   end
   print("data in hex:")
   print(temp)
end



Unfortunately this code does not what I intended
Code: Select allfunction write_scratchpad(pin, msg)
   ow.setup(pin)
   ow.reset(pin)
   addr = ow.search(pin)
   ow.reset_search(pin)
   ow.reset(pin)
   ow.select(pin, addr)
   ow.write(pin, 0x4E)
   debg = ow.write_bytes(pin, msg, 1)
   print(debg)
   ow.write(pin, 0x48)
end

s=0x4B..0x46..0x5F
write_scratchpad(4, s)


I was trying to write 4Bh and 46h (values of TH and TL that I got by read_scratchpad) and 5Fh for Tconv resolution of 11bit.
Checking the sensor with read_scratchpad it shows the same values as before.

What am I doing wrong? Hope someone can help.

Cheers
MVD