Socket does not connect to Thingspeak

Hi,

I have been using application Thingspeak for a while to post data. Suddenly it stopped working. After some debugging it seems the Netduino hangs when trying to connect the socket ( socket.connect(remoteEndPoint)). This only happens for api.thingspeak.com. If i try a local url or e.g. google.com the socket connection happens without any problems. Connecting to api.thingspeak.com from other devices ( POST test on my laptop ) works without problems. I have spend two days trying to figure out what could be wrong without results.

Can anyone help me ??

Br, Arno

Perhaps this will lead you in the right direction.

Hi Stephen,

Thanks for picking up my problem. My Thingspeak account is still active and I can post data with a Google test app to POST a message. It is only the Netduino ( 2 PLUS ) which hangs on socket connect.

Are you using Netduino to connect to Thingspeak ? If yes, what code are you using to connect ?

Arno

No, I am not using a Netduino 2+ for thingspeak. So I would not have any code to share with you.

Hi

Do you have a small sample that I could have a look at to debug?

Have you got code which dumps the Network config on startup?

Regards

@KiwiBryn

Hi, I found some code examples for Thingspeak and implemented those on the NP2. A bit to my surprise this is working now so the problem is somehow solved though i still do not understand why my current code suddenly stopped working. My not working code is:

public static void POSTToThingspeak(string data, int connectTimeout)
{
string host = “api.thingspeak.com”;
//Debug.Print(“Trying to send POST data to Thingsspeak server”);
IPEndPoint remoteEndPoint = new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], 80);
try
{
Socket connection = new SocketConnectWithTimeOut(remoteEndPoint, connectTimeout, “tcp”).Connect();
SendThingspeakPOSTRequest(connection, data);
connection.Close();
//Debug.Print(“Ready sending POST data to Thingsspeak”);
}
catch (SocketException e)
{
Debug.Print("connection error for: " + remoteEndPoint.Address.ToString() + e.Message + e.ErrorCode.ToString());
if (e.ErrorCode == 10038) Debug.Print(“Socket Timeout error 10038”);
}
catch (SocketTimeoutException e)
{
Debug.Print("Socket Timeout error " + e.Message);
}
catch (Exception e)
{
Debug.Print("connection error for: " + remoteEndPoint.Address.ToString() + e.Message);
}

    }

    public static void SendThingspeakPOSTRequest(Socket s, string content)
    {
        //Debug.Print("Sending message: ");
        //Debug.Print(content);
        byte[] contentBuffer = Encoding.UTF8.GetBytes(content);
        var requestLine = "POST /update HTTP/1.1" + CRLF;
        byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine);
        var headers =
            "Host: api.thingsspeak.com" + CRLF +
            "Content-Type: application/x-www-form-urlencoded" + CRLF +
            "Connection: close" + CRLF +
            "Content-Length: " + contentBuffer.Length + CRLF +
            CRLF;
        byte[] headersBuffer = Encoding.UTF8.GetBytes(headers);
        s.SendTimeout = connectionTimeOut;
        s.Send(requestLineBuffer);
        s.Send(headersBuffer);
        s.Send(contentBuffer);
        //     string response = HTTP_Methods.ReadResponse(s);
        string response = HTTP_Methods.HTTPReceive(s);
        //     string HTTPStatusCode = HTTP_Methods.DigestHTTPHeader(response, "Status-Code");
        //     Debug.Print("Received status from: " + uri + " : " + HTTPStatusCode);
        //           Debug.Print("Received response: ");
        //           Debug.Print(response);
        string HTTPStatusCode = HTTP_Methods.DigestHTTPHeader(response, "Status-Code");
        Debug.Print("Received response: " + HTTPStatusCode);
        //            Debug.Print(response);
        s.Close();
    }