Post your best Lua script examples here

User avatar
By bbx10node
#29745 might be an old or wrong IP address. Connect to "" instead.

If this fails, try "ping" 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 is 15s
   ssid="yourSSID"    -- your router SSID
        pass="yourPassword"   -- your router password

   apiKey="yourAPIwriteKey" -- set your key

-- send data to
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'') -- This row is good, but only for newer firmware
--conn:connect(80,'') -- This is worse, but it also works well with the older firmware.
   conn:send('GET /update?key='..apiKey..
   ' HTTP/1.1\r\n'..
   'Host:\r\nAccept: */*\r\n'..
   'User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   --conn:close() -- You can enable this row for skip answer
   function(conn, payload)

User avatar
By M0J01
#42877 Hey There!

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

1. going to, 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")
--modify according your wireless router settings
wifi.sta.config("YOUR ROUTERr","PASSWORD")
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("waiting for ip")
print("Got ip "..wifi.sta.getip())

Code: Select all-- ds1820.lua
-- Measure temperature and post data to
-- 2014 OK1CDJ
--- Tem sensor DS18B20 is conntected to GPIO0
--- 2015.01.21 sza2 temperature value concatenation bug correction
--- 2016.01.06 ahanktcd edits
pin = 4
function bxor(a,b)
   local r = 0
   for i = 0, 31 do
      if ( a % 2 + b % 2 == 1 ) then
         r = r + 2^i
      a = a / 2
      b = b / 2
   return r
--- Get temp and send data to
function sendData()
   ow.write(pin,0xCC, 1)
   ow.write(pin, 0x44, 1)
   ow.write(pin,0xCC, 1)
   ow.write(pin,0xBE, 1)
   data = nil
   data = string.char(
   for i = 1, 8 do
     data = data .. string.char(
   t = (data:byte(1) + data:byte(2) * 256)
   data = nil
   if (t > 32768) then
      t = (bxor(t, 0xffff)) + 1
      t = (-1) * t
   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
   print("Sending to thingspeak")
   conn=net.createConnection(net.TCP, 0)
   conn:on("receive", function(conn, payload) print(payload) end)
   conn:send("GET /update?key=YOUR_FORM_API_KEY&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")
   conn:send("Accept: */*\r\n")
   conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
                    print("Closing connection...")
   conn:on("disconnection", function(conn)
   t1 = nil
   t2 = nil
-- 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
------------------------------------------------- - Esplorer

To write files I used Hari Wigunas
------------------------------------------------- - LuaUploader
"saving" files directly to the ESP8266

Good Luck, Good Flashing