-->
Page 1 of 1

Cant recieve the answer back when query Thingspeak

PostPosted: Mon Sep 28, 2015 6:06 pm
by Luciano Jose
Hi! I cant get the answer back when asking thingspeaks about the last value of a field of a certain channel: 184.106.153.149/channels/44665/fields/1/last?key=O2R7O7BYS6M7ES10 which goes ok from firefox, here is part of the code

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("telecentro","bamboklat")
wifi.sta.connect()

tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP no disponible, esperando...")
else
tmr.stop(1)
print("Config hecha, IP:  "..wifi.sta.getip())
end
end)

tmr.alarm(2, 4000, 1, function()

    if (wifi.sta.getip() ~= nil) then       
        sk = net.createConnection(net.TCP, 0) 
        sk:connect(80,'184.106.153.149') -- server ESP IP
       
        sk:on("connection", function(sk)
        sk:send('GET /channels/44665/fields/1/last?key=O2R7O7BYS6M7ES10')
        end )
       
        sk:on("receive", function(sk, receivedData)
        print("received: " .. receivedData)
        sk:close()
         end)

    else
        print("no connection...")
    end
end)


I used this secuence to comunicate 2 esp8266 and I could recieve the server answer, any idas of what I am doing wrong?

Thanks in advence!

Re: Cant recieve the answer back when query Thingspeak

PostPosted: Tue Sep 29, 2015 11:54 am
by jankop
Here is solution...

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("telecentro","bamboklat")
wifi.sta.connect()

tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP no disponible, esperando...")
else
tmr.stop(1)
print("Config hecha, IP:  "..wifi.sta.getip())
end
end)

tmr.alarm(2, 4000, 1, function()

    if (wifi.sta.getip() ~= nil) then
        sk = net.createConnection(net.TCP, 0)
        sk:connect(80,'184.106.153.149') -- server ESP IP

        sk:on("connection", function(sk)
        sk:send('GET /channels/44665/fields/1/last?headers=false&key=O2R7O7BYS6M7ES10'..
      ' 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 )

        sk:on("receive", function(sk, receivedData)
        print("received: " .. receivedData)
        sk:close()
         end)

    else
        print("no connection...")
    end
end)