Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Cosm (ex Pachube) exactly fails twice on three


  • Please log in to reply
4 replies to this topic

#1 Sharktear

Sharktear

    Member

  • Members
  • PipPip
  • 11 posts

Posted 16 October 2012 - 08:04 AM

Hi,
I writed a class that update a temperature sample on Cosm service... the code comes from the book's (Internet of things) examples and strangely work fine once on three.
The class run in a thread that is called every 5 min (300000ms), but the cosm datastream is updated only every 15 minutes because 2 consecutive times the thread fail.
The code is as follows (running on Netduino Plus with 4.2.0.1 firmware:

using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.SPOT;

namespace SharktearTests
{
    public static class CosmClient
    {
        const string baseUri = "http://api.cosm.com/v2/feeds/";
        const string apiKey = "HERE MY COSM APY KEY";
        const string feedId = "HERE THE FEED ID";

        public static void Send(string sample)
        {
            Debug.Print("time: " + DateTime.Now);
            Debug.Print("memory available: " + Debug.GC(true));
            try
            {
                using (var request = CreateRequest(apiKey, feedId, sample))
                {
                    request.Timeout = 5000;     // 5 seconds
                    // send request and receive response
                    using (var response =
                        (HttpWebResponse)request.GetResponse())
                    {
                        HandleResponse(response);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }

        static HttpWebRequest CreateRequest(string apiKey, string feedId,
            string sample)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(sample);

            var request = (HttpWebRequest)WebRequest.Create
                (baseUri + feedId + ".csv");

            // request line
            request.Method = "PUT";

            // request headers
            request.ContentLength = buffer.Length;
            request.ContentType = "text/csv";
            request.Headers.Add("X-ApiKey", apiKey);

            // request body
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(buffer, 0, buffer.Length);
            }

            return request;
        }

        public static void HandleResponse(HttpWebResponse response)
        {
            Debug.Print("Status code: " + response.StatusCode);
        }
    }
}

I also tried the code that use sockets instead of top level class but this time the program fail on the following line:
IPHostEntry hostEntry = Dns.GetHostEntry(host);

here the complete source code:

using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.SPOT;

namespace SharktearTests
{
    public static class CosmClient
    {
        const string baseUri = "http://api.cosm.com";
        const string apiKey = "HERE MY COSM APY KEY";
        const string feedId = "HERE THE SERVICE FEED ID";

        public static void Send(string sample)
        {
            Debug.Print("time: " + DateTime.Now);
            Debug.Print("memory available: " + Debug.GC(true));
            try
            {
                using (var request = CreateRequest(apiKey, feedId, sample))
                {
                    request.Timeout = 5000;     // 5 seconds
                    // send request and receive response
                    using (var response =
                        (HttpWebResponse)request.GetResponse())
                    {
                        HandleResponse(response);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }

        static HttpWebRequest CreateRequest(string apiKey, string feedId,
            string sample)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(sample);

            var request = (HttpWebRequest)WebRequest.Create
                (baseUri + feedId + ".csv");

            // request line
            request.Method = "PUT";

            // request headers
            request.ContentLength = buffer.Length;
            request.ContentType = "text/csv";
            request.Headers.Add("X-ApiKey", apiKey);

            // request body
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(buffer, 0, buffer.Length);
            }

            return request;
        }

        public static void HandleResponse(HttpWebResponse response)
        {
            Debug.Print("Status code: " + response.StatusCode);
        }
    }
}


Any suggestion?

#2 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 16 October 2012 - 12:14 PM

Do you get any response at all? "Common status codes include: 200 OK: request processed successfully. 401 Not Authorized: either you need to provide authentication credentials, or the credentials provided aren't valid. 403 Forbidden: Cosm understands your request, but refuses to fulfill it. An accompanying error message should explain why. 404 Not Found: either you're requesting an invalid URI or the resource in question doesn't exist (eg. no such feed). 422 Unprocessable Entity: Cosm was unable to create a feed because the EEML/JSON was not complete/valid (e.g. it didn't include a "title" element). 500 Internal Server Error: Something went wrong... Please post to the forum about it and we will investigate. 503 No server error: usually occurs when there are too many requests coming into Cosm - if you get this from an API request then the error message will be returned in XML in the response." https://cosm.com/docs/v2/

#3 Sharktear

Sharktear

    Member

  • Members
  • PipPip
  • 11 posts

Posted 16 October 2012 - 06:29 PM

Do you get any response at all?

"Common status codes include:

200 OK: request processed successfully.
401 Not Authorized: either you need to provide authentication credentials, or the credentials provided aren't valid.
403 Forbidden: Cosm understands your request, but refuses to fulfill it. An accompanying error message should explain why.
404 Not Found: either you're requesting an invalid URI or the resource in question doesn't exist (eg. no such feed).
422 Unprocessable Entity: Cosm was unable to create a feed because the EEML/JSON was not complete/valid (e.g. it didn't include a "title" element).
500 Internal Server Error: Something went wrong... Please post to the forum about it and we will investigate.
503 No server error: usually occurs when there are too many requests coming into Cosm - if you get this from an API request then the error message will be returned in XML in the response."

https://cosm.com/docs/v2/


No, any response. No error code because the execution never arrive at the response elaboration

#4 65tux

65tux

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts

Posted 16 October 2012 - 10:33 PM

Cosm has a good API debug page to see if request make it to their server. Log in to your cosm account and it should be in the icon buttons in the top right corner. I would start with that to see if the problem is with the connect, the request or the response.

#5 Sharktear

Sharktear

    Member

  • Members
  • PipPip
  • 11 posts

Posted 17 October 2012 - 10:48 AM

The problem is not on Cosm side, it's service works well, the problem is in the Netduino program. I think that the first call to Cosm (the one that works properly) leave the request socket connection open or in any undefined state, so the second call (after 5 minutes) fail (hangs in the request creation)... then after others 5 min. (with the third call) the code throws an exception and maybe close the request connection. After 5 min. the 4th call works fine, then the 5th and the 6th fails and so on... is cyclic.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.