Post your best Lua script examples here
User avatar
By bbx10node
#29745 184.106.153.149 might be an old or wrong IP address. Connect to "api.thingspeak.com" instead.

If this fails, try "ping api.thingspeak.com" and use the IP address it returns. This is less desirable since IP addresses frequently change so it may work today but fail in the future.
User avatar
By jankop
#29765 It's the wrong time sequence of commands in your program. First, you have to wait for a connection, send, and you can not close the connection before everything is sent.
There is a functional sketch.

Code: Select all--    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
--****************************************************************
   ssid="yourSSID"    -- your router SSID
        pass="yourPassword"   -- your router password
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid,pass,1)

   apiKey="yourAPIwriteKey" -- set your thingspeak.com key
   tempin=21.3   
   hum=52.5   
   tempout=18.7

-- send data to https://api.thingspeak.com
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)
   print("Connection!")
   conn:send('GET /update?key='..apiKey..
   '&headers=false'..
   '&field1='..tempin..
   '&field2='..hum..
   '&field3='..tempout..
   ' HTTP/1.1\r\n'..
   'Host: api.thingspeak.com\r\nAccept: */*\r\n'..
   'User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
conn:on("sent",
   function(conn)
   print("Sent!")
   --conn:close() -- You can enable this row for skip thingspeak.com answer
   end)
conn:on("receive",
   function(conn, payload)
   print("Receive!")
   print(payload)
   conn:close()
   end)
conn:on("disconnection",
   function(conn)
   print("Disconnection!")
   end)

User avatar
By M0J01
#42877 Hey There!

I was getting the negative temperature reading as well. I solved it by

1. going to http://nodemcu-build.com/, getting a custom build (up to date firmware as well). I added all of the modules, just to be sure. You might only need the OW ("One Wire") and a few others.
2. Using the following code

Code: Select all--init.lua
print("Connecting to wifi")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("YOUR ROUTERr","PASSWORD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("waiting for ip")
else
tmr.stop(1)
print("Got ip "..wifi.sta.getip())
dofile("ds1820.lua")
end
end)


Code: Select all-- ds1820.lua
-- Measure temperature and post data to thingspeak.com
-- 2014 OK1CDJ
--- Tem sensor DS18B20 is conntected to GPIO0
--- 2015.01.21 sza2 temperature value concatenation bug correction
--- 2016.01.06 ahanktcd edits
print("P4/GPIO2\n")
pin = 4
ow.setup(pin)
function bxor(a,b)
   local r = 0
   for i = 0, 31 do
      if ( a % 2 + b % 2 == 1 ) then
         r = r + 2^i
      end
      a = a / 2
      b = b / 2
   end
   return r
end
--- Get temp and send data to thingspeak.com
function sendData()
   ow.reset(pin)
   ow.write(pin,0xCC, 1)
   ow.write(pin, 0x44, 1)
   tmr.delay(1000000)
   ow.reset(pin)
   ow.write(pin,0xCC, 1)
   ow.write(pin,0xBE, 1)
   data = nil
   data = string.char(ow.read(pin))
   for i = 1, 8 do
     data = data .. string.char(ow.read(pin))
   end
   t = (data:byte(1) + data:byte(2) * 256)
   data = nil
   if (t > 32768) then
      t = (bxor(t, 0xffff)) + 1
      t = (-1) * t
   end
   t = t * 625
   t1 = t / 10000
   t2 = (t >= 0 and t % 10000) or (10000 - t % 10000)
   t = nil
   print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
   -- conection to thingspeak.com
   print("Sending to thingspeak")
   conn=net.createConnection(net.TCP, 0)
   conn:on("receive", function(conn, payload) print(payload) end)
   -- api.thingspeak.com 184.106.153.149
   conn:connect(80,'184.106.153.149')
   conn:send("GET /update?key=YOUR_FORM_API_KEY&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")
   conn:send("Host: api.thingspeak.com\r\n")
   conn:send("Accept: */*\r\n")
   conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
   conn:send("\r\n")
   conn:on("sent",function(conn)
                    print("Closing connection...")
                    conn:close()
                 end)
   conn:on("disconnection", function(conn)
                           print("success")
     end)
   t1 = nil
   t2 = nil
end
-- send data every X ms to thing speak
tmr.alarm(0, 20000, 1, function() sendData() end )


Even if you don't use it, someone else down the line might be able to.

I struggled with the -1.7 temp for a few hours. It appears the be inconsistencies in the firmware. Also, the chips are not foolproof, so don't be afraid to try 2/3 times to make sure you catch it during the right moment of bootup.

To communicate and read files I used Slovik Dudes
------------------------------------------------- http://esp8266.ru/esplorer/ - Esplorer

To write files I used Hari Wigunas
-------------------------------------------------https://www.youtube.com/watch?v=ty-sUlYM9M4 - LuaUploader
"saving" files directly to the ESP8266

Good Luck, Good Flashing