Post your best Lua script examples here

User avatar
By alonewolfx2
#8508 can you share your code? propably you are sending temp with C char. thingspeak accept just number.

rajbadri wrote: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
User avatar
By Quosek
#8526 Hi,

I have similar problem - I've got one of sample codes and it's not working ....
I'm receiving on console info (every 1 min) about current temp, but it is not sending that to thingspeak (yes, key is proper one, when I make request via browser: http://api.thingspeak.com/update?key=XXX&field1=71.55 it is working fine)

Here is my code (the only changed things is thingspeak key).

Code: Select all--once every 1 minute send temperature from DS18B20 to thingspeak.com

WRITEKEY = "XXXXX"
INTERVAL = 1 -- 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         

wifi.setmode(wifi.STATION)
wifi.sta.config("ASUS_Guest1","")
print(wifi.sta.getip())

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 Quosek
#8540 One more thing that I've found out
If I execute commands from console:
Code: Select allconn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, payload) print("Get done.", payload) end )
conn:connect(80,"xxx.xx")
conn:send("POST/insert?k=key&t=11.44 HTTP/1.1\r\nHost: xx.xx.xx.xx\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")

I get connection to the server.

But if I define in init.lua a function:
Code: Select allfunction sendTemp()
conn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, payload) print("Get done.", payload) end )
conn:connect(80,"xxx.xx")
conn:send("POST/insert?k=key&t=11.44 HTTP/1.1\r\nHost: xx.xx.xx.xx\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end

and then execute this function from console -> POST is not being sent

Anybody can help me ?
Thanks
User avatar
By MaxSchulz2
#8594 Unfortunately I dont have a solution for your problem but I just wanted to add that I tried out several codes and adjusted them and still it wont work.

Meanwhile I dont think it's a router problem (forwarding port 80 fails) anymore as I think http requests should always! be enabled right? Otherwise I would have to open up port 80 after resetting it to factory defaults in order to use simple web pages which is obviously not the case.