Post your best Lua script examples here

User avatar
By iHaveESP
#6370 Thanks Pigs Fly!

I tested the DHT11 code and it works fine!

Temperature: 23.0
Humidity: 65.0
ChecksumReceived: 88
ChecksumTest: 88
> dofile('dht11.lua')

Since FP math can't be done, does anyone know a bit trick to convert C to F? I tried:

tF = (Temperature* 9/5) + 32
print ("TempF: "..tF)

TempC: 23.0
TempF: 73
Humidity: 67.0
ChecksumReceived: 90
ChecksumTest: 90


but the fraction gets cut off. I know the DHT11 only has 1C resolution, but it would be nice to display it correctly.

Many Thanks!
User avatar
By iHaveESP
#6475 For the DHT11, since it only has 1C resolution, the following celsius to fahrenheit conversion works:

With the help of the dallas module I cames up with this:

Code: Select alltF = (Temperature*9/5)+32
tFDec = ((Temperature*256)*1125)+320000
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF
print ("TempC: "..Temperature.."."..TemperatureDec)
print ("TempF: "..tF.."."..string.char(string.byte(tFDec,4)))
print ("Humidity: "..Humidity.."."..HumidityDec)
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)


Results:
Code: Select all> dofile('dht11.lua')
TempC: 28.0
TempF: 82.4
Humidity: 54.0
ChecksumReceived: 82
ChecksumTest: 82



This does NOT apply to DHT22 since it has tenths celsius resolution.
Hope this helps someone else!
User avatar
By Pigs Fly
#6508 IhaveESP: Thanks for your testing of the DHT11 and continued work on that with the C->F conversion.

I'm still hoping to speed up that acquisition loop for better resolution of the bit length which should give better reliability. As it stands we get from 0 to 4 with most 0-bits (20uS) showing up as a length of 1 and most 1 bits (70uS) having a length of 3. The code then later assumes that anything 3 or greater is a logic "1". That's too close for comfort if you ask me. A 4-bit length from 0 to 15 would be much better, bit nothing I've tried speeds it up enough to make a difference.
User avatar
By javiery
#6832 I made an adaptation for DHT22 from DHT11 code of Pigs Fly (thanks for you code Pig :) ).

The DHT22 code:

Code: Select allbitStream = {}
for j = 1, 40, 1 do
     bitStream[j]=0
end
bitlength=0

pin  = 4;
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read=gpio.read
gpio_write=gpio.write

gpio.mode(pin, gpio.INPUT)

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--acquisition loop
for j = 1, 40, 1 do
     while (gpio_read(pin)==1 and bitlength<10 ) do
          bitlength=bitlength+1
     end
     bitStream[j]=bitlength
     bitlength=0
     --bus will always let up eventually, don't bother with timeout
     while (gpio_read(pin)==0) do end
end

--DHT data acquired, process.

Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0

for i = 1, 16, 1 do
     if (bitStream[i+0] > 2) then
          Humidity = Humidity+2^(16-i)
     end
end
for i = 1, 16, 1 do
     if (bitStream[i+16] > 2) then
          Temperature = Temperature+2^(16-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+32] > 2) then
          Checksum = Checksum+2^(8-i)
     end
end
ChecksumTest=((Humidity / 256) + (Humidity % 256) + (Temperature / 256) + (Temperature % 256)) % 256

print ("Temperature: "..(Temperature / 10).."."..(Temperature % 10))
print ("Humidity: "..(Humidity / 10).."."..(Humidity % 10))
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)


Output:
Code: Select all> dofile('dht22_script.lua')
Temperature: 15.0
Humidity: 78.1
ChecksumReceived: 166
ChecksumTest: 166


In my opinion Pig code has an error. This line:
Code: Select allChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF


Should be so:
Code: Select allChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 256


I'll try to make a DHT22 module this weekend.