Gsiot.PachubeClient is a very simple C# library for the .NET Micro Framework that allows to send measurements, in the form of comma-separated values, to the Pachube Web service.

To build the library, you need to reference the following assemblies: mscorlib, Microsoft.SPOT.Native, and System.Http.

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

 

namespace Gsiot.PachubeClient
{
    public static class PachubeClient
    {
        const string baseUri = "http://api.pachube.com/v2/feeds/";

 

        public static void Send(string apiKey, string feedId,
            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-PachubeApiKey", 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);
        }
    }
}

Companion Web site for the book
Getting Started with the Internet of Things
By Cuno Pfister
Publisher: O'Reilly Media