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 thingspeak.com

WRITEKEY = "YOURWRITEKEYGOESHERE"
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
   repeat
      count = count + 1
      addr = ow.reset_search(GPIO2)
      addr = ow.search(GPIO2)
      tmr.wdclr()
   until((addr ~= nil) or (count > 100))
   if count > 100 then
      return nil
   else
      return addr
   end
end

function readtemp(address)
   --print(addr:byte(1,8))
   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)
         ow.select(GPIO2, addr)
         ow.write(GPIO2,0x44,1)
         tmr.delay(1000000)
         present = ow.reset(GPIO2)
         ow.select(GPIO2, addr)
         ow.write(GPIO2,0xBE,1)
         data = string.char(ow.read(GPIO2))
         for i = 1, 8 do
            data = data .. string.char(ow.read(GPIO2))
         end
         --print(data:byte(1,9))
         crc = ow.crc8(string.sub(data,1,8))
         --print("CRC="..crc)
          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
             end
             --  convert Celsius to Fahrenheit
             f1=(((f1*625)*9)/500)+3200
             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
        tmr.wdclr()
      else
         --print("Device family is not recognized.")
         return nil
      end -- if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
   else
      --print("CRC is not valid!")
      return nil
   end -- if (crc == addr:byte(8)) then
   return f2.."."..f3
end

function sendtothingspeak(temp)
   local conn=net.createConnection(net.TCP, 0)
   conn:connect(80,'184.106.153.149')
   conn:send("GET /update?key="..WRITEKEY.."&field1="..temp.." 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\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
            print(line)
            break
         end
      end
      conn:close()
      conn=nil
      line=nil
      payload=nil
   end) -- conn:on("receive", function(conn, payload)
end         

ow.setup(GPIO2)
ds18b20addr=findaddress()
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")
         sendtothingspeak(temp)
      else
         print("crc error")
      end -- if temp ~= nil
   end) -- tmr.alarm(0,1000*60,1,function()
else
   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 thingspeak.com
Closing connection
Got disconnection...




please help
thanks