Post your best Lua script examples here

User avatar
By Pigs Fly
#6165 Based on the reading of that waveform I figure the bit field reads:

0001 0101 0000 0000 0001 0001 0000 0000 0010 0110

I'm not far enough along yet to know how that should be read into the variables.
User avatar
By cdrwolfe
#6172
sancho wrote:
cdrwolfe wrote:I'm beginning to wonder if this is possible at all using lua.

For the life of me I can't get the chip to correctly control the gpio pins. With the DHT22 connected it is always set as high, calling gpio.write(pin, gpio.LOW) does nothing :(.

If anyone else has any luck feel free to post it :D.

Regards

Cdr

Which version of ESP do you have? The very first ESP-01 did not have GPIO pins connected to the output pins of the board - therefore doing anything with the code would result in absolutely nothing.


Wait so the ESP8266-01 with only GPIO0 and GPIO02 can't be used? I wish I had known that :). I can connect a LED to ground and GPIO02 and switch it on and off using the write commands, but i'm guessing this is just internal resistance?

Apologies for the spelling mistakes, never got that far in testing the script to eek them out.

Really the print codes did that? seems strange, I will remove them and see if it makes a difference, though i don't have an oscilloscope to view output. I'll also have a go at increasing tmr.delay. Though I suppose it is mute if i can't use the ESP8266-01 for this.

Thanks for the help it is really appreciated as this is all new to me.

Kind Regards

Cdr
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.