Hello Pachube (AnalogInput) - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
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

Hello Pachube (AnalogInput)


  • Please log in to reply
3 replies to this topic

#1 ljbeng

ljbeng

    New Member

  • Members
  • Pip
  • 8 posts

Posted 21 May 2012 - 01:20 PM

I just tried Hello Pachube example to get started with ethernet activity.

var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);

The above line is not computing. AnalogInput is wavy underlined and Error reads:

Error 1 The type or namespace name 'AnalogInput' could not be found (are you missing a using directive or an assembly reference?)

#2 Cuno

Cuno

    Advanced Member

  • Members
  • PipPipPip
  • 144 posts
  • LocationZürich / Switzerland

Posted 22 May 2012 - 10:00 AM

I just tried Hello Pachube example to get started with ethernet activity.

var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);

The above line is not computing. AnalogInput is wavy underlined and Error reads:

Error 1 The type or namespace name 'AnalogInput' could not be found (are you missing a using directive or an assembly reference?)

Hello ljbeng

it looks like you are missing an assembly reference to Microsoft.SPOT.Hardware. If you add that reference, the problem should disappear.

I just checked whether this sample also works with Cosm (the rebranded Pachube), and with new Cosm accounts. Fortunately, it does.

Best regards

Cuno

#3 ljbeng

ljbeng

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 May 2012 - 02:36 PM

Here is the entire program. I copied right out of Internet of Things book online examples.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

public class HelloPachubeSockets
{
    public static void Main()
    {
        const string apiKey = "your Pachube API key";
        const string feedId = "your Pachube feed id";
        const int samplingPeriod = 20000;   // 20 seconds

        const double maxVoltage = 3.3;
        const int maxAdcValue = 1023;

        var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);
        var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
        var highPort = new OutputPort(Pins.GPIO_PIN_A2, true);

        Socket connection = null;

        while (true)   // main loop
        {
            WaitUntilNextPeriod(samplingPeriod);

            Debug.Print("time: " + DateTime.Now);
            Debug.Print("memory available: " + Debug.GC(true));

            if (connection == null)   // create connection
            {
                try
                {
                    connection = Connect("api.pachube.com",
                        samplingPeriod / 2);
                }
                catch
                {
                    Debug.Print("connection error");
                }
            }

            if (connection != null)
            {
                try
                {
                    int rawValue = voltagePort.Read();
                    double value = (rawValue * maxVoltage) /
                        maxAdcValue;
                    string sample = "voltage," + value.ToString("f");
                    Debug.Print("new message: " + sample);
                    SendRequest(connection, apiKey, feedId, sample);
                }
                catch (SocketException)
                {
                    connection.Close();
                    connection = null;
                }
            }
        }
    }

    static Socket Connect(string host, int timeout)
    {
        // look up host's domain name to find IP address(es)
        IPHostEntry hostEntry = Dns.GetHostEntry(host);
        // extract a returned address
        IPAddress hostAddress = hostEntry.AddressList[0];
        IPEndPoint remoteEndPoint = new IPEndPoint(hostAddress, 80);

        // connect!
        Debug.Print("connect...");
        var connection = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
        connection.Connect(remoteEndPoint);
        connection.SetSocketOption(SocketOptionLevel.Tcp,
            SocketOptionName.NoDelay, true);
        connection.SendTimeout = timeout;
        return connection;
    }

    static void SendRequest(Socket s, string apiKey, string feedId,
        string content)
    {
        byte[] contentBuffer = Encoding.UTF8.GetBytes(content);
        const string CRLF = "\r\n";
        var requestLine =
            "PUT /v2/feeds/" + feedId + ".csv HTTP/1.1" + CRLF;
        byte[] requestLineBuffer = Encoding.UTF8.
            GetBytes(requestLine);
        var headers =
            "Host: api.pachube.com" + CRLF +
            "X-PachubeApiKey: " + apiKey + CRLF +
            "Content-Type: text/csv" + CRLF +
            "Content-Length: " + contentBuffer.Length + CRLF +
            CRLF;
        byte[] headersBuffer = Encoding.UTF8.GetBytes(headers);
        s.Send(requestLineBuffer);
        s.Send(headersBuffer);
        s.Send(contentBuffer);
    }

    static void WaitUntilNextPeriod(int period)
    {
        long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
        var offset = (int)(now % period);
        int delay = period - offset;
        Debug.Print("sleep for " + delay + " ms\r\n");
        Thread.Sleep(delay);
    }
}



#4 Gilberto Garcia

Gilberto Garcia

    New Member

  • Members
  • Pip
  • 8 posts

Posted 22 May 2012 - 06:47 PM

I just tried Hello Pachube example to get started with ethernet activity.

var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);

The above line is not computing. AnalogInput is wavy underlined and Error reads:

Error 1 The type or namespace name 'AnalogInput' could not be found (are you missing a using directive or an assembly reference?)


I had a similar problem before. The following code solved my problem:

var voltagePort = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A1);

I agree with Cuno, Pachube's code is working fine with the Cosm.




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.