Post your best Lua script examples here

User avatar
By alonewolfx2
#5980
sancho wrote:My DHT22 and DHT11 will arive probably next week, I'll port it than (seems really easy as the library is not using interrupts).

it seems already ported in nodelua. maybe you can take a quick look.
User avatar
By cdrwolfe
#6089
alonewolfx2 wrote:
sancho wrote:My DHT22 and DHT11 will arive probably next week, I'll port it than (seems really easy as the library is not using interrupts).

it seems already ported in nodelua. maybe you can take a quick look.


Do you happen to have a link? I couldn't find it anywhere.

I've been trying to port over one of the MBED examples into lua for the DHT22, but haven't had much luck in testing it. My power supply is weak and annoying at best which makes it frustrating and I'm not sure if I have currectly wired the DHT22.

I can't seem to get either the script or direct calls to correctly change the GPIO02 pin to high or low. It seems with the DHT22 connected to the GPIO02 pin, VCC and GND the pin is always pulled high.

Anyway someone else might get some use out of it until I can figure out what is wrong. Not sure about the bitwise operations as well.

Code: Select all--retrieveData()
-- Retrieve data from DHT22 sensor
-- Set pin
pin  = 4;
-- Initiliase bit data array
bitData = {}
-- 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
retryCount = 0;
-- Loop
while gpio.read(pin) == 1 do
     -- Check retry count
     if retryCount > 40 then
          print "DHT22 not responding"
     end
     -- Increment count
     retryCount = retryCount + 1
     -- Wait 1us
     tmr.delay(1)
end
-- Once confirmed wait a specified time '80us+'
tmr.delay(80)
-- 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, 5, 1 do
     for j = 0, 8, 1 do
          -- Reset count
          retryCount = 0;
          -- Wait for DHT22 pin to pull low
          while gpio.read(pin) == 0 do
                -- Check retry count
               if retryCount > 75 then
                    print "DHT22 timeout waiting for data"
               end
               -- Increment count
               retryCount = retryCount + 1
               -- Wait 1us
               tmr.delay(1)
          end
          -- Once confirmed wait a specified time '40us+'
          tmr.delay(40)
          -- Check data bit value
          if (gpio.read(pin) == 1) then
               -- DHT22 pin is high, bit value is a 1
               bitData[i*8+j] = 1
          else
               -- DHT22 pin is low, bit value is a 0
               bitData[i*8+j] = 0
          end
          -- Wait for 1us and pin is high
          count = 0
          -- Loop
          while gpio.read(pin) == 1 and count < 100 do
               tmr.delay(1)
               -- Increment
               count = count + 1
          end
     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 = 0
currentTemperature = 0
checksum = 0
-- First 16 bits is humidity
for i = 0, 16, 1 do
     -- Check value
     if (bitData[i+1] > 0) then
          currentHumidity = bit.bor(currentHumidity, (bit.lshift(1, (15 - i))))
     end
end
-- Second 16 bits is temperature
for i = 0, 16, 1 do
     -- Check value
     if (bitData[i+17] > 0) then
          currentTemperature = bit.bor(currentTemperature, (bit.lshift(1, (15 - i))))
     end
end
-- Last 8 bits are checksum
for i = 0, 8, 1 do
     -- Check value
     if (bitData[i+33] > 0) then
          checksum = bit.bor(checksum, (bit.lshift(1, (7 - i))))
     end
end
-- Convert values for humidity and temperature
lastHumidity = currentHumidity / 10
-- Check temperature is negative or not
if (bit.band(currentTemperature, 0x8000) == 0x8000) then
     lastTemperature = (bit.band(currentTemperature, 0x7FFF) / 10) * -1
else
     lastTemperature = currentTemperature / 10
end
-- Calculate checksum
-- Initialise variables
checksum1 = bit.rshift(currentHumidity, 8)
checksum2 = bit.band(currentHumidity, 0xFF)
checksum3 = bit.rshift(currentTemperature, 8)
checksum4 = bit.band(currentTemperature, 0xFF)
-- Check checksum
if (checksum == bit.band((checksum1 + checksum2 + checksum3 + checksum4), 0xFF)) then
     -- Checksum successful
     print "Checksum OK"
else
     -- Checksum failed
     print "DHT22 checksum error"
end
 -- Function end

function transmitData()
-- Transmit sensor data
end


EDIT* Fixed spelling errors in humidity and bit right shift call

Regards

Wolfe
Last edited by cdrwolfe on Sat Jan 03, 2015 8:00 am, edited 1 time in total.