Post your best Lua script examples here

User avatar
By kniazio
#34967 I have a problem with the sensor DHT11.
Which showed the lowest temperature is 1 degree
Now is -2 degrees, and DHT shows 1 degree
init.lua
Code: Select all--     thingspeak.com client with sensor DHT11,DHT22
--    Tested with Lua NodeMCU 0.9.6 (nodemcu_float_0.9.6-dev_20150704.bin)
--    Minimal period for data send to api.thingspeak.com is 15s
--
   DEBUGPRINT = true -- true: enable debugg print, false: disable debugg print
--****************************************************************
   WRITEKEY="xxxxxxxxxxxxxxx" -- set your thingspeak.com key
--****************************************************************
   ssid="xxxxxx"    -- your router SSID
    pass="xxxxxxxxxx"   -- your router password
--****************************************************************
   pin=4   -- number of pin (GPIO0), where is DHTXX connected
   prs=60  -- period reading and sending sensors [s]
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid,pass,1)

function debugprint(...)
   if DEBUGPRINT then
      print(...)
   end
end

--Read DHTXX sensor
function ReadDHT()
     status,temp,humi,temp_decimial,humi_decimial = dht.read(pin)
   if( status == dht.OK ) then
   debugprint("DHT Temperature:"..temp..";".."Humidity:"..humi)
   elseif( status == dht.ERROR_CHECKSUM ) then
   debugprint( "DHT Checksum error." );
   elseif( status == dht.ERROR_TIMEOUT ) then
   debugprint( "DHT Time out." );
   end
end

-- send data to https://api.thingspeak.com
function SendTS()
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'api.thingspeak.com')   -- This row is good, but only for newer firmware
--conn:connect(80,'184.106.153.149')    -- This is worse, but it also works well with the older firmware.
conn:on("connection",
   function(conn) debugprint("Connection!")
   conn:send('GET /update?key='..WRITEKEY..
   '&headers=false'..
   '&field2='..humi..
   '&field3='..temp..
   ' 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("sent",
   function(conn)
   debugprint("Sent!")
      if DEBUGPRINT == false
      then
      conn:close()
      end
   end)
conn:on("receive",
   function(conn, payload)
   debugprint("Receive!")
   debugprint(payload)
   conn:close()
   end)
conn:on("disconnection",
   function(conn)
   debugprint("Disconnection!")
   end)
end

-- first reading sensors
  ReadDHT()
  SendTS()
-- Periodic reading of the sensor
tmr.alarm(0,prs*1000,1,
 function()
 if wifi.sta.getip()==nil
   then
   print("IP refresh")
   print("!!!!!!!!!!!!!!")
   node.restart()
   end
  ReadDHT()
  SendTS()
end)

dht11.lua
Code: Select all-- ***************************************************************************
-- DHT11 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 checko1
local checko2

function M.read(pin)
  humidity = 0
  temperature = 0
  checksum = 0
  checko1=0
  checko2=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 DHT11
  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:  DHT11 send response signal
  -- 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

  -- Step 3: DHT11 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, 8, 1 do
    if(bitStream[i+0]>2)then
      humidity=humidity+2^(8-i)
    end
   if(bitStream[i+8]>2)then
      checko1=checko1+2^(8-i)
    end
    if(bitStream[i+16]>2)then
      temperature=temperature+2^(8-i)
    end
    if(bitStream[i+24]>2)then
      checko2=checko2+2^(8-i)
   end
    if (bitStream[i+32]>2)then
      checksum=checksum+2^(8-i)
    end
  end

  checksumTest=(humidity+checko1+temperature+checko2)%256

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

  if checksum ~= checksumTest then
    humidity = -1
  end
end

function M.getTemperature()
  return temperature
end

function M.getHumidity()
  return humidity
end

return M

My chanel https://thingspeak.com/channels/56111 black charts Temperatura