- Thu Oct 01, 2015 9:07 pm
#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)