Post your best Lua script examples here

User avatar
By MaxSchulz2
#8161 I've got a question related to the router settings and the port setup.

I've been using several code examples but I cant make my esp upload the ds18b20 data to thingspeak.

1) I tried both the api key and the write api key - which one to use?
2) In my router setup I'm setting a rule to forward the port 80. Is that correct or do I have to forward port 8080 in order to speak through port 80?
3) Do I set the router to port forwarding OR port triggering?

I just cant seem to make it work... thank you very much for your help!
User avatar
By jim121049
#8185 You don't need to mess with your router. Just plug your write key into line 4 below.

Code: Select all--once every 10 minutes send temperature from DS18B20 to

INTERVAL = 10 -- every 10 minutes

-- GPIO0  = 3   
-- GPIO1  = 10
-- GPIO2  = 4   
-- GPIO3  = 9
-- GPIO4  = 1   
-- GPIO5  = 2   
-- GPIO9  = 11
-- GPIO10 = 12
-- GPIO12 = 6      
-- GPIO13 = 7   
-- GPIO14 = 5      
-- GPIO15 = 8
-- GPIO16 = 0   

GPIO2 = 4 -- use GPIO2 for 1-wire

function findaddress()
   local count = 0
      count = count + 1
      addr = ow.reset_search(GPIO2)
      addr =
   until((addr ~= nil) or (count > 100))
   if count > 100 then
      return nil
      return addr

function readtemp(address)
   local data = nil
   local crc = ow.crc8(string.sub(addr,1,7))
   if (crc == addr:byte(8)) then
      if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
         --print("Device is a DS18S20 family device.")
         ow.reset(GPIO2), addr)
         present = ow.reset(GPIO2), addr)
         data = string.char(
         for i = 1, 8 do
            data = data .. string.char(
         crc = ow.crc8(string.sub(data,1,8))
          if (crc == data:byte(9)) then
             -- read temperature
             f1 = data:byte(1)+data:byte(2)*256
             if (f1 > 32768) then -- for negative termperatures
                f1 = (bxor(f1, 0xffff)) + 1
                f1 = (-1) * f1
             --  convert Celsius to Fahrenheit
             f2 = f1/100 -- integer part of Fahrenheit temperature
             f3 = f1%10  -- fractional part of Fahrenheit temperature (use %100 for 2 decimal places)
             --print("Temp = "..f2.."."..f3.." F.")
          end -- if (crc == data:byte(9)) then
         --print("Device family is not recognized.")
         return nil
      end -- if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
      --print("CRC is not valid!")
      return nil
   end -- if (crc == addr:byte(8)) then
   return f2.."."..f3

function sendtothingspeak(temp)
   local conn=net.createConnection(net.TCP, 0)
   conn:send("GET /update?key="..WRITEKEY.."&field1="..temp.." 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\r\n")
   conn:on("receive", function(conn, payload)
      for line in payload:gmatch("[^\r\n]+") do  -- split "payload" into lines
         if string.find(line,"HTTP/1.1") ~= nil then
   end) -- conn:on("receive", function(conn, payload)

if ds18b20addr ~= nil then
   tmr.alarm(0,1000*60*INTERVAL,1,function()  -- fire timer every 10 minutes...
      temp = readtemp(ds18b20addr)
      if temp ~= nil then
         print(node.heap().." bytes")
         print(temp.." F")
         print("crc error")
      end -- if temp ~= nil
   end) -- tmr.alarm(0,1000*60,1,function()
   print("DS18B20 not found!")
end -- if ds18b20addr ~= nil then
User avatar
By MaxSchulz2
#8199 Still now working ;( I really think my router might cause problems like blocking port 80 for some reason. Still thank you very much for your help!
Anybody have some more suggestions? Things to consider?
My ESP seems to be properly connected to the WiFi. Just a few days ago I uploaded the same sensor data to a webserver running on the ESP, so the module shouldn't be faulty.
User avatar
By rajbadri
#8500 i have inserted the thinkspeak api and my wifi settings are ok but get this message and data is not updated on thinkspeak

Last temp: 253125
Temp:25.3125 C

Sending data to
Closing connection
Got disconnection...

please help