Post your best Lua script examples here

User avatar
By Mordack
#8729 Having just begun to *play* with this module (WI07c from Electrodragon), this was my first demo with a DHT11. As the LUA code stood, it would not read properly evident from the continuous check sum errors. Now, I'm not a programmer at all and I only heard of LUA a week days ago but I had to modify the minimum count for it to read correctly. I also removed a couple of lines that didn't appear to make sense to me. Again, not a programmer but my unit is working. Here is my modified dht.lua code in case it helps others.
Code: Select all-- ***************************************************************************
-- DHT22/11 module for ESP8266 with nodeMCU
--
-- Written by Javier Yanez
-- but based on a script of Pigs Fly from ESP8266.com forum
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************

local moduleName = ...
local M = {}
_G[moduleName] = M

local humidity
local temperature
local checksum
local checksumTest
local minCountForLow = 1

function M.read(pin)
  humidity = 0
  temperature = 0
  checksum = 0

  -- Use Markus Gritsch trick to speed up read/write on GPIO
  gpio_read = gpio.read
  gpio_write = gpio.write

  bitStream = {}
  for j = 1, 40, 1 do
    bitStream[j] = 0
  end
  bitlength = 0

  -- Step 1:  send out start signal to DHT
  gpio.mode(pin, gpio.OUTPUT)
  --MCU needs to pull data line down >18mS to signal DHT response
  gpio.write(pin, gpio.LOW)
  tmr.delay(20000)
  --MCU awaiting response
  gpio.mode(pin, gpio.INPUT)

  -- Step 2:  DHT send response signal
  -- MCU looks for DHT to respond with 80uS low on the data line
  while (gpio_read(pin) == 0 ) do end
  -- Followed by a 80uS high on the data line
  while (gpio_read(pin) == 1) do end
  -- MCU looks for DHT to indicate start bit by pulling data line down for 50uS
  while (gpio_read(pin) == 0 ) do end
 
  -- Step 3: DHT send data (40 bits)
  for j = 1, 40, 1 do
    while (gpio_read(pin) == 1 and bitlength < 3 ) do
       bitlength = bitlength + 1
    end
    bitStream[j] = bitlength
    bitlength = 0
    -- MCU looks for DHT to indicate start of next bit by pulling data line down for 50uS
    while (gpio_read(pin) == 0) do end
  end

  --DHT data acquired, process.
  for i = 1, 16, 1 do
    if (bitStream[i + 0] > minCountForLow) then
      -- Add up all the "high" bit postion values
      humidity = humidity + 2 ^ (16 - i)
    end
    if (bitStream[i + 16] > minCountForLow) then
      -- Add up all the "high" bit postion values
      temperature = temperature + 2 ^ (16 - i)
    end
  end
  for i = 1, 8, 1 do
    if (bitStream[i + 32] > minCountForLow) then
      -- Add up all the "high" bit postion values
      checksum = checksum + 2 ^ (8 - i)
    end
  end
  checksumTest=((humidity-humidity%256)/256+humidity%256+(temperature-temperature%256)/256+temperature%256)%256
  if temperature > 0x8000 then
    -- convert to negative format
    temperature = -(temperature - 0x8000)
  end

  if tostring(checksum) ~= tostring(checksumTest) then
  humidity = -1
  end
end

function M.getTemperature()
  return temperature
end

function M.getHumidity()
  return humidity
end

return M
User avatar
By thenecroscope
#9785 Hi,

This is my first post. I'm just starting to get my head around the ESP8266, but unfortunately I cannot get this to work I keep getting negative values. -0.1 for humidity and -3276.7 for temperature. Does anyone have a wiring diagram or a photo, video I can follow just to double check I've wired the device up correctly please. I'm using the latest nodemcu_20150212.bin. The webpage is working fine, its just the results I'm getting back from the DHT22 unit are completely wrong. Any suggestions anyone. Many thanks!!!!!
User avatar
By jankop
#11189 Test it!
1. Is your sensor really connected to GPIO2 ?
2. Is sensor properly connected to ground?
3. Is your terminals wiring okay?
Save test program wots.lua and modul testdht.lua
Load it to esp8266 and run wots.lua.
Bring result.
Then we can talk of an additional cases.

This is proper communication with DHT11. DHT22 is very similar.
Code: Select alldofile("wots.lua")
checksum    58
checksumTest    58
humidity - timing of bits      0 1 5 1 1 5 1 4 1 1 1 1 1 1 1 1
temperat - timing of bits      1 1 1 5 1 5 1 4 1 1 1 1 1 1 1 1
checksum - timing of bits      1 1 5 5 5 1 5 1
Humidity:    37%
Temperature: 21 deg C


And here the sensor is disconnected .
Code: Select alldofile("wots.lua")
checksum    255
checksumTest    252
humidity - timing of bits      10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
temperat - timing of bits      10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
checksum - timing of bits      10 10 10 10 10 10 10 10
Humidity:    0%
Temperature: -127.99609375 deg C