Post your best Lua script examples here

User avatar
By gwizz
#7464 Hiya ChrixXenon

The reference to 4 needs to be understood as an internal reference number. They are mapped using the GPIO mapping table here https://github.com/nodemcu/nodemcu-firmware/#gpio-old-table-before-build-20141212.

It means that you should wire the DS18B20 signal wire (often yellow in pre-wired waterproof sensors) to the GPIO2 pin.

I made a little one-liner you can include in your scripts if you like that means you can use the normal GPIO numbers.

Code: Select allgpio= {[0]=3,[2]=4,[4]=2,[5]=1,[12]=6,[13]=7,[14]=5,[15]=8,[16]=0}


After this line you can refer to pins like this:

Code: Select allpin=gpio[2] -- connect the signal wire to pin GPIO2


hth

G
User avatar
By Jeckert
#10297 Here is how I solved the number conversion on the esp8266 with a DS18B20. It works with both positive and negative numbers.
Code: Select allfunction trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end
rt1=("0x"..trim(string.format('%02X ', data:byte(2)))..trim(string.format('%02X ', data:byte(1))).."" )
s=""
rt2=rt1
   if (rt1+0) > 2001 then
     s="-"
     rt2=(bit.bxor(rt1, 0xffff)+1)
   end
td=bit.band(rt2,15)
td=(td*.0625)
tn=(bit.rshift(rt2, 4))
print(s..(tn+td))