Post your best Lua script examples here

User avatar
By GIlbert Marchal
#10918 Hi!

I've just tried your code, sending to thingspeak is ok but the values (read and sent) are wrong :cry:
Code: Select allHumidity:    70.4%
Temperature: 45.2 deg C
Temperature: 113.36 deg F


I've checked the sensor by reading it with an arduino and I got 21.4°C and 44% humidity (checked with another Oregon sensor).
I've also tried your code without sensor attached and I got the following:
Code: Select allHumidity:    -0.1%
Temperature: -3276.7 deg C
Temperature: -5866.06 deg F

So it seems to read something but not in the right way, would you have any idea of the problem?
I'm running build 20150213.

Thank you in advance for your help!
Kind regards.
User avatar
By jankop
#10973 Yesterday I run into the same problem. But it is quite complicated. I think that it causes WiFi signal from ESP8266 module. The largest deviation is from the moment of intense communication on WiFi network. Oddly, the checksum is correct. So far I can not solve.
This program might work better, do not forget to reset after uploading !!! You can also try to compile files using node.compile (), it can be. But it does not solve the problem of hardware.

Code: Select all-- name BPclient03.lua
--    Demo thingspeak.com client with sensor DHT11/22
--    Tested with Lua NodeMCU 0.9.5 build 20150213
-- 1. Flash Lua NodeMCU to ESP module.
-- 2. Set in program BPclient03.lua humidity sensor type. This is parameter typeSensor="dht11" or "dht22".
-- 3. Set in program BPclient03.lua your thingspeak.com write API key
-- 4. You can rename the program BPclient03.lua to init.lua
-- 5. Load program BPclient03.lua and dht.lua to ESP8266 with LuaLoader
-- 6. HW reset module !!!
-- 7. Login module to your AP - wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD") from console !!!
-- 8. Run program BPclient03.lua - dofile(BPclient03.lua)
-- 9. The sensor is repeatedly read and data are send to api.thingspeak.com every 15s,
--    It is minimal period for data send to api.thingspeak.com
--    The author of the program module dht.lua for reading DHT sensor is Javier Yanez
--    The author of the http client part is Peter Jennings

sensorType="dht11"          -- set sensor type dht11 or dht22
WRITEKEY="EMZHRBDWJBZI2MV0" -- set your thingspeak.com key

timup=tmr.now()
   PIN = 4 --  data pin, GPIO2
   humi=0
   temp=0
   fare=0
   chck=0
   gslp=0
   --load DHT module for read sensor
function ReadDHT()
   dht=require("dht")
   dht.read(PIN)
   chck=1
   h=dht.getHumidity()
   t=dht.getTemperature()
   if h==nil then h=0 chck=0 end
   if sensorType=="dht11"then
      humi=h/256
      temp=t/256
   else
      humi=h/10
      temp=t/10
   end
   fare=(temp*9/5+32)
   print("Humidity:    "..humi.."%")
   print("Temperature: "..temp.." deg C")
   print("Temperature: "..fare.." deg F")
   -- release module
   dht=nil
   package.loaded["dht"]=nil
end

-- send to https://api.thingspeak.com
function SendTS()
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print(payload)end)
conn:on("connection",
   function(conn, payload)
      print("Connected")
      conn:send('GET /update?key='..WRITEKEY..'&field1='..humi..'&field2='..temp..'&field3='..chck..'HTTP/1.1\r\n'..
      'Host: api.thingspeak.com\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
   conn:on("disconnection",
   function(conn, payload)
      print('Disconnected')
   end)
conn:connect(80,'184.106.153.149')
end

tmr.alarm(1,15000, 1,
   function()
      if wifi.sta.getip()~=nil
      then ReadDHT()SendTS()
      end
   end)


Code: Select all-- ***************************************************************************
-- name dht.lua
-- DHT module for ESP8266 with nodeMCU floating point
-- 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

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

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

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

  -- Step 1:  send out start signal to DHT22
  gpio.mode(pin, gpio.OUTPUT)
  gpio.write(pin, gpio.HIGH)
  tmr.delay(100)
  gpio.write(pin, gpio.LOW)
  tmr.delay(20000)
  gpio.write(pin, gpio.HIGH)
  gpio.mode(pin, gpio.INPUT)

  -- Step 2:  DHT22 send response signal
  -- bus will always let up eventually, don't bother with timeout
  while (gpio_read(pin) == 0 ) do end
  local c=0
  while (gpio_read(pin) == 1 and c < 500) 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 < 500) do c = c + 1 end

  -- Step 3: DHT22 send data
  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.
  for i = 1, 16, 1 do
   if (bitStream[i] > 3) then
      humidity = humidity + 2 ^ (16 - i)
    end
    if (bitStream[i + 16] > 3) then
      temperature = temperature + 2 ^ (16 - i)
    end
  end
  for i = 1, 8, 1 do
   if (bitStream[i + 32] > 3) then
      checksum = checksum + 2 ^ (8 - i)
    end
  end

  checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8))
  checksumTest = bit.band(checksumTest, 0xFF)

  if temperature > 0x8000 then
    -- convert to negative format
    temperature = -(temperature - 0x8000)
  end

  -- conditions compatible con float point and integer
  if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
    humidity = nil
  end
end

function M.getTemperature()
  return temperature
end

function M.getHumidity()
  return humidity
end

return M


Here you can see how erroneous values are read from the sensor DHT11. It is an interference problem, which I do not currently suppress. In reality, temperature is stabile about 23 grades. ESP8266 is periodic awakening from deep sleep and loged in.

Image