Left here for archival purposes.

User avatar
By Luke Dixon
#16274 I'm seeing a similar issue when posting to ThingSpeak.

Initially everything works ok. I'm getting a soil moisture from a hygrometer and successfully post to the API. However, after some time it simply jumps to "disconnection" instead of going to my "connection" callback and doesn't send the data. I left it running all night and it occasionally works again but normally only once or twice.

My code is as such:
Code: Select allfunction sendData()
    connout = nil
    connout = net.createConnection(net.TCP, 0)

    print("Moisture:"..adc.read(0)..".")
 
    connout:on("receive", function(connout, payloadout)
        if (string.find(payloadout, "Status: 200 OK") ~= nil) then
            print("Posted OK");
        end
    end)
 
    connout:on("connection", function(connout, payloadout)
 
        print ("Posting...");   
 
        connout:send("GET /update?api_key=[KEY]&field1=" .. adc.read(0)
        .. " HTTP/1.1\r\n"
        .. "Host: api.thingspeak.com\r\n"
        .. "Connection: close\r\n"
        .. "Accept: */*\r\n"
        .. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
        .. "\r\n")

        print("Sent");
    end)
 
    connout:on("disconnection", function(connout, payloadout)
        print("Disconnection");
        connout:close();
        collectgarbage();
    end)

    print("================");
    print("Starting...");
    connout:connect(80,'184.106.153.149')
end

sendData()
tmr.alarm(0, 60000, 1, function() sendData() end )


I've tried everything i can think of and many code examples from the web. Any idea what could be doing this? Their API limit is 15 seconds so i don't think it's that.

Any help is greatly appreciated. :)
User avatar
By Nicky Jaspers
#16728
TerryE wrote:No I mean look at your Apache (or whatever) logs as well as any firewall that you may have on your host. Just because the connection is being closed doesn't mean that the ESP code isn't making the attempt.


Well, tcpdump says the following when using the esp:
Code: Select all22:10:28.063343 IP (tos 0x0, ttl 255, id 3, offset 0, flags [none], proto TCP (6), length 44)
    192.168.178.51.35323 > NAS.8084: Flags [S], cksum 0xd76c (correct), seq 6827, win 5840, options [mss 1460], length 0
22:10:28.063391 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 44)
    NAS.8084 > 192.168.178.51.35323: Flags [S.], cksum 0x3658 (correct), seq 247566914, ack 6828, win 5840, options [mss 1460], length 0
22:10:28.520303 IP (tos 0x0, ttl 255, id 4, offset 0, flags [none], proto TCP (6), length 44)
    192.168.178.51.35323 > NAS.8084: Flags [S], cksum 0xd76c (correct), seq 6827, win 5840, options [mss 1460], length 0
22:10:28.520342 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 44)
    NAS.8084 > 192.168.178.51.35323: Flags [S.], cksum 0x3658 (correct), seq 247566914, ack 6828, win 5840, options [mss 1460], length 0
22:10:29.020335 IP (tos 0x0, ttl 255, id 5, offset 0, flags [none], proto TCP (6), length 44)
    192.168.178.51.35323 > NAS.8084: Flags [S], cksum 0xd76c (correct), seq 6827, win 5840, options [mss 1460], length 0
22:10:29.020371 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 44)
    NAS.8084 > 192.168.178.51.35323: Flags [S.], cksum 0x3658 (correct), seq 247566914, ack 6828, win 5840, options [mss 1460], length 0
22:10:29.521650 IP (tos 0x0, ttl 255, id 6, offset 0, flags [none], proto TCP (6), length 44)
    192.168.178.51.35323 > NAS.8084: Flags [S], cksum 0xd76c (correct), seq 6827, win 5840, options [mss 1460], length 0
22:10:29.521686 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 44)
.....


When using a browser on a pc on my local network:
Code: Select all22:11:37.422105 IP (tos 0x0, ttl 128, id 12957, offset 0, flags [DF], proto TCP (6), length 52)
    Woonkamer.fritz.box.54138 > NAS.8084: Flags [S], cksum 0x098a (correct), seq 3695284589, win 8192, options [mss 1260,nop,wscale 8,nop,nop,sackOK], length 0
22:11:37.422149 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52)
    NAS.8084 > Woonkamer.fritz.box.54138: Flags [S.], cksum 0xc570 (correct), seq 1337654456, ack 3695284590, win 5840, options [mss 1460,nop,nop,sackOK,nop,wscale 5], length 0
22:11:37.422364 IP (tos 0x0, ttl 128, id 12958, offset 0, flags [DF], proto TCP (6), length 40)
    Woonkamer.fritz.box.54138 > NAS.8084: Flags [.], cksum 0x1c0d (correct), ack 1, win 260, length 0
22:11:37.426165 IP (tos 0x0, ttl 128, id 12959, offset 0, flags [DF], proto TCP (6), length 365)
    Woonkamer.fritz.box.54138 > NAS.8084: Flags [P.], cksum 0x1e5f (correct), seq 1:326, ack 1, win 260, length 325
22:11:37.426203 IP (tos 0x0, ttl 64, id 14787, offset 0, flags [DF], proto TCP (6), length 40)
.....


I see some different options in the request, but still not really a clue, so at least a request is received, but to me it is still not clear on which level if fails.