Post your best Lua script examples here

User avatar
By cdrwolfe
#6208 Fantastic Pig!

I've tried the DHT11 using the DHT22 and it runs but the numbers seems off or my setup is just giving checksum errors shown below:

Code: Select allTemperature: 0.207
Humidity: 2.69
ChecksumReceived: 22
ChecksumTest: 23
> dofile('DHT11.lua')

Temperature: 128.103
Humidity: 1.34
ChecksumReceived: 139
ChecksumTest: 11
> dofile('DHT11.lua')

Temperature: 0.103
Humidity: 1.34
ChecksumReceived: 138
ChecksumTest: 138
> dofile('DHT11.lua')

Temperature: 128.103
Humidity: 1.33
ChecksumReceived: 9
ChecksumTest: 10
> dofile('DHT11.lua')

Temperature: 0.206
Humidity: 2.68
ChecksumReceived: 20
ChecksumTest: 21
> dofile('DHT11.lua')

Temperature: 128.103
Humidity: 1.34
ChecksumReceived: 139
ChecksumTest: 11
> dofile('DHT11.lua')

Temperature: 0.207
Humidity: 2.69
ChecksumReceived: 22
ChecksumTest: 23


If you don't mind I will have a look through your code and try and see if I can replicate it in mine.

According to the Mbed C code example the author states that the DHT22 is shifting the bits 1 to the left which is not in the data sheet. I can't tell because all I get in terms of bits is generally '010111111111111111' so basically after a while it reads only high. I think this may be die to the tight timing but more investigation is required.

Edit* Ahh now I see what you did with the acquisition of data, rather then relying on length of delay you simply take any bit length longer then 2 to be high etc.

Kind Regards

Cdr
User avatar
By Pigs Fly
#6212 Damn. I was hoping that would just fall into place for the '22. At one point in troubleshooting I did a for-loop to print out those values after acquisition to set the threshold at 3+ for a logic "1". That might help in your case:

Code: Select allfor j = 1, 40, 1 do
     print("Bitstream["..j.."]="..bitStream[j]
end
User avatar
By cdrwolfe
#6235
Pigs Fly wrote:Damn. I was hoping that would just fall into place for the '22. At one point in troubleshooting I did a for-loop to print out those values after acquisition to set the threshold at 3+ for a logic "1". That might help in your case:

Code: Select allfor j = 1, 40, 1 do
     print("Bitstream["..j.."]="..bitStream[j]
end


I will try that, the data sheet for DHT22 has 16 bits for a decimal number rather then 8 bits integral and 8 bits decimal. Once you have the 16bit string you convert to decimal and divide by 10 to get your value.

I didn't make any changes to take this into account so this may be why, I will try later, thanks Pig.

Regards

Cdr
User avatar
By cdrwolfe
#6259 Right I have included all your bits Pig and now I believe it works with regards to temperature value and humidity.

Only problems are that checksum isn't working correctly as I don't know how to correctly perform bitwise addition / sum, of the checksum parts.

Also it seems to only give whole integer numbers though I know it should be able to give at least a single decimal value as well.

Code: Select all-- Retrieve data from DHT22 sensor
-- Set pin
pin  = 4;
-- Initiliase bit data array
bitData = {}
--Pre-allocate vars used in loop.
for i = 0, 39, 1 do
     bitData[i]=0
end
-- Set bitlength variable
bitlength=0
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read=gpio.read
gpio_write=gpio.write
-- Step 1 - Host send start signal and wait 1ms
-- Set pin to output data
gpio.mode(pin, gpio.OUTPUT)
-- Send out start signal to DHT22, pull low
gpio.write(pin, gpio.LOW)
-- Wait specified time '1ms+'
tmr.delay(20000)
-- Host pull up output pin voltage and wait for sensors response
gpio.write(pin, gpio.HIGH)
-- Set pin to recieve data
gpio.mode(pin, gpio.INPUT)
-- Loop and wait for ACK pulse
-- Bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do
     -- Loop until pin pulls high
end
-- Reset count
retryCount = 0;
while (gpio_read(pin)==1 and retryCount < 100) do
     -- Loop until pin pulls low
     -- Increment count
     retryCount = retryCount + 1
end
--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do
     -- Loop until pin pulls high
end
-- Reset count
retryCount = 0;
while (gpio_read(pin)==1 and retryCount < 100) do
     -- Loop until pin pulls low
     -- Increment count
     retryCount = retryCount + 1
end
-- Step 2 - Read data stream
-- Data recieved as 40 bits of data, 16 bits for humidity, 16 bits for temperature and 6 bits checksum
-- Need to convert from binary to decimal and divide by 10
-- When highest bit of temperature is '1' it means that temperature is below '0'
for i = 0, 39, 1 do
     -- Wait for DHT22 pin to pull high and set whether 0 or 1 bit value based upon length
     while (gpio_read(pin) == 1 and bitlength < 10 ) do
          bitlength=bitlength+1
     end
     bitData[i]=bitlength
     bitlength=0
     --bus will always let up eventually, don't bother with timeout
     while (gpio_read(pin)==0) do end
end
-- Re-initialise DHT22 pin
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
-- Using bit data calculate values for humidity, temperature and checksum
-- Initialise variables
currentHumidity = ""
currentTemperature = ""
checksum = ""
-- Initialise variables
checksum1 = ""
checksum2 = ""
checksum3 = ""
checksum4 = ""
-- First 8 bits of humidity
for i = 0, 7, 1 do
     -- Check value
     if bitData[i] > 2 then
          -- Set bit value as high '1'
          currentHumidity = currentHumidity .. "1"
          -- Parse checksum data
          checksum1 = checksum1 .. "1"
     else
          -- Set bit value as low '0'
          currentHumidity = currentHumidity .. "0"
          -- Parse checksum data
          checksum1 = checksum1 .. "0"
     end 
end
-- Second 8 bits of humidity
for i = 8, 15, 1 do
     -- Check value
     if bitData[i] > 2 then
          -- Set bit value as high '1'
          currentHumidity = currentHumidity .. "1"
          -- Parse checksum data
          checksum2 = checksum2 .. "1"
     else
          -- Set bit value as low '0'
          currentHumidity = currentHumidity .. "0"
          -- Parse checksum data
          checksum2 = checksum2 .. "0"
     end
end
-- First 8 bits of temperature
for i = 16, 23, 1 do
     -- Check value
     if bitData[i] > 2 then
          -- Set bit value as high '1'
          currentTemperature = currentTemperature .. "1"
          -- Parse checksum data
          checksum3 = checksum3 .. "0"
     else
          -- Set bit value as low '0'
          currentTemperature = currentTemperature .. "0"
          -- Parse checksum data
          checksum3 = checksum3 .. "0"
     end
end
-- Second 8 bits of temperature
for i = 24, 31, 1 do
     -- Check value
     if bitData[i] > 2 then
          -- Set bit value as high '1'
          currentTemperature = currentTemperature .. "1"
          -- Parse checksum data
          checksum4 = checksum4 .. "0"
     else
          -- Set bit value as low '0'
          currentTemperature = currentTemperature .. "0"
          -- Parse checksum data
          checksum4 = checksum4 .. "0"
     end
end
-- Last 8 bits are checksum
for i = 32, 39, 1 do
     -- Check value
     if bitData[i] > 2 then
          -- Set bit value as high '1'
          checksum = checksum .. "1"
     else
          -- Set bit value as low '0'
          checksum = checksum .. "0"
     end
end
-- Convert values for humidity and temperature
lastHumidity = tonumber(currentHumidity, 2) / 10
-- Check temperature is negative or not, check bitstirng length in bit data signifies a '0' bit
if (bitData[16] > 2) then
     lastTemperature = (tonumber(currentTemperature, 2) / 10) * -1
else
     lastTemperature = tonumber(currentTemperature, 2) / 10
end
print "Temperature and Humidity"
print (lastTemperature)
print (lastHumidity)
checksumTest = bit.band((checksum1 + checksum2 + checksum3 + checksum4), 0xFF)
print ("ChecksumReceived: "..tonumber(checksum, 2))
print ("ChecksumTest: "..checksumTest)
-- Calculate checksum
if (checksum == checksumTest) then
     -- Checksum successful
     print "Checksum OK"
else
     -- Checksum failed
     print "DHT22 checksum error"
end
 -- Function end

function transmitData()
-- Transmit sensor data
end


And output:

Code: Select allTemperature and Humidity
20
51
ChecksumReceived: 212
ChecksumTest: 120
DHT22 checksum error
> dofile('DHT22.lua')

Temperature and Humidity
20
52
ChecksumReceived: 221
ChecksumTest: 97
DHT22 checksum error
> dofile('DHT22.lua')

Temperature and Humidity
20
52
ChecksumReceived: 220
ChecksumTest: 96
DHT22 checksum error


Kind Regards

Cdr