Post your best Lua script examples here

User avatar
By kniazio
#30292
jankop wrote:Where is connected your sensor? On GPIO0?
What is version your NODEMCU LUA?

float_0.9.6-dev_20150704
Something moved.
Yes gpio = 0
There are frequent timeout
I tried with resistor 4k7 and 10k
Same
Show me your file dht.lua
Code: Select allDHT Temperature:24;Humidity:21
Connection!
Sent!
Receive!
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
X-Cnection: close
Status: 200 OK
ETag: "7c1373b8fec010dba9b2cf06e7b232a7"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 3e6d66bb-819a-4361-966a-89e8633fad49
X-Runtime: 0.022954
X-Powered-By: Phusion Passenger 4.0.57
Date: Thu, 01 Oct 2015 20:23:39 GMT
Server: nginx/1.9.3 + Phusion Passenger 4.0.57

5
18968


Code: Select allDHT Time out.
Connection!
Sent!
Receive!
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
X-Cnection: close
Status: 200 OK
ETag: "01bebc8e971d09cb90cf5756b340f24c"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 925fb081-aa83-4be6-84e3-50c7a08a05dc
X-Runtime: 0.020830
X-Powered-By: Phusion Passenger 4.0.57
Date: Thu, 01 Oct 2015 20:25:39 GMT
Server: nginx/1.9.3 + Phusion Passenger 4.0.57

5
18970

Please help
https://thingspeak.com/channels/56111
User avatar
By godfish
#30317 I can't get the new functional sketch to work at all? I get " NodeMCU 0.9.6 build 20150216 powered by Lua 5.1.4
lua: init.lua:25: attempt to index global 'dht' (a nil value).. I renamed it to init.lua because the chip wouldn't restart without it. did I do something wrong? I've very new to this.

jankop wrote:Here's new functional sketch, tested with DHT11:

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="yourAPIkey" -- set your thingspeak.com key
--****************************************************************
   ssid="yourSSID"    -- your router SSID
    pass="yourPass"   -- your router password
--****************************************************************
   pin=3   -- number of pin (GPIO0), where is DHTXX connected
   prs=15  -- 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'..
   '&field1='..humi..
   '&field2='..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)
User avatar
By jankop
#30327 You have to record a new version of Lua, as stated in the program description - (nodemcu_float_0.9.6-dev_20150704.bin). It has internal support for DHT sensors.
Edit: Set variable prs =15 to prs=60 and rename program from init to something for testing. Under normal conditions, the module will not restart. For real traffic rename program to init.lua.

https://github.com/nodemcu/nodemcu-firmware/releases
Last edited by jankop on Fri Oct 02, 2015 3:33 pm, edited 1 time in total.
User avatar
By kniazio
#30341 The sensor shows a low humidity and is stable. (dht11)
Indications range from 40 to 60%.
The temperature was a little too high.
I have a 4k7 resistor and cable 25cm
Voltage 3.3V
Here is the link:
https://thingspeak.com/channels/56111
How do I calibrate the sensor?