Post your best Lua script examples here

User avatar
By cdrwolfe
#6178 Right, removing the print calls got it to at least start the process, thanks Pig :).

The code was a port from http://developer.mbed.org/cookbook/RHT03, and there is a little confusion on my part about how to read the data. The script below waits for it to drop low and then waits a specified time before reading again and checking whether it is high or low to get the bit data. The data sheet indicates to wait for low and the decide on bit value depending on length of high value, so it may be a timing issue. Guess i'll have to experiment.

https://www.adafruit.com/datasheets/Dig ... AM2302.pdf

Code: Select all-- 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
User avatar
By Pigs Fly
#6179 If you can toggle an LED then it's a good port and should work fine for this. It's the timing that's going to be difficult to troubleshoot without some kind of logic analyzer/DSO. The DS18B20 code going around should verify that, assuming you have a DS18B20 to hook up. This is not the first time I've seen the print function wreck otherwise perfectly good code. It's not even a matter of slowing it down - it just quits working for some reason. 0.9.4 firmware is also troublesome for me. I backed down to 0.9.2 (20141219) and things work much better all around. Haven't tried the 20141230 version.

Your code is on the right track, I just haven't spent enough time going through it to figure out where things might be going wrong, and there's the difference between your DHT22 it was designed for and the DHT11 I have here. The Adafruit arduino code does make that distinction even if it might only be in the way it parses the returned data.

The analyzers are not too expensive. I'm using an older version of the Saele Logic 8 (https://www.saleae.com) which is no longer sold. They have a 4-line for $99. I've also used the clones (http://www.aliexpress.com/wholesale?SearchText=saleae+logic+analyzer) which is (ideally) the same thing internally and uses the Saele software. Quality control is nowhere the same, but two of the three ordered actually worked.
User avatar
By sancho
#6190 No, this is just a missunderstanding.
I have 1 of the very first ESP-01 modules and this one has actualy 2 unconnected pins - GPIO0 and GPIO2 are not connected.
I just wanted to make sure you are not one of the unlucky first explorers with such a module ;)
Should your LED work, you have the "correct" ESP-01.
User avatar
By Pigs Fly
#6195 Got it working. Timing was so tight it almost doesn't work. Good luck, and I hope someone can clean it up a bit. Just be cautious of modifying the acquisition loop.

Code: Select all--Works for DHT11 on ESP-07 (version w/16pins)
--Only 20141219 firmware tested.

--Data stream acquisition timing is critical. There's
--barely enough speed to work with to make this happen.
--Pre-allocate vars used in loop.
bitStream = {}
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, 8, 1 do
     if (bitStream[i+0] > 2) then
          Humidity = Humidity+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+8] > 2) then
          HumidityDec = HumidityDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+16] > 2) then
          Temperature = Temperature+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+24] > 2) then
          TemperatureDec = TemperatureDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+32] > 2) then
          Checksum = Checksum+2^(8-i)
     end
end
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF

print ("Temperature: "..Temperature.."."..TemperatureDec)
print ("Humidity: "..Humidity.."."..HumidityDec)
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)


screenshot.38.png
screenshot.38.png (1.56 KiB) Viewed 4072 times
Last edited by Pigs Fly on Sat Jan 03, 2015 4:21 pm, edited 1 time in total.