Question

Cette adaptation triviale de l'échantillon Pachube pour Netduino ne fonctionne pas. Il n'a pas barf, il échoue silencieusement. J'ai remarqué que si vous attendez un peu après l'écriture, la prise rapporte 315 octets dans sa propriété Available, mais en essayant de lire les données ne fonctionne tout simplement pas -. cb contient zéro et le tampon inchangé est d'accord

Pourquoi ne puis-je lire ces données? Je pense que c'est une réponse du serveur qui pourrait bien me aider à comprendre ce qui ne va pas avec le reste de l'application.

Voici le code:

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;
using SecretLabs.NETMF.Hardware;

public class HelloPachubeSockets
{
    static string apiKey = "wggHejZTKTvE6ZplSc5feSs1rQLeD4cYmi7WPYiG0VLIx_TC-isp8CI9vkKq3nQxadhydl3gUiSK8vS5SBd9JCCoXQG-g0N8FXYlYcNjEfJVMQJA-usgijpE1LRT-xw5";
    static string feedId = "43974";
    static double maxVoltage = 3.3;
    static int maxAdcValue = 1023;
    static int interval = 20000;
    static Socket connection = null;
    static Timer timer;
    static AnalogInput voltagePort;
    static OutputPort lowPort, highPort;
    static IPEndPoint remoteEndPoint;
    static double V = 0;

    static void LogSensor(object state)
    {
        Debug.Print("time: " + DateTime.Now);
        Debug.Print("memory available: " + Debug.GC(true));
        try
        {
            connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connection.SendTimeout = interval / 2;
            connection.Connect(remoteEndPoint);
            int rawValue = voltagePort.Read();
            double value = (rawValue * maxVoltage) / maxAdcValue;
            string sample = "voltage," + V.ToString("f");
            V += 0.1;
            Debug.Print("new message: " + sample);
            byte[] contentBuffer = Encoding.UTF8.GetBytes(sample);
            const string CRLF = "\r\n";
            var requestLine = "PUT /v2/feeds/" + feedId + ".csv HTTP/1.1\r\n";
            byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine);
            var headers =
                "Host: api.pachube.com\r\n" +
                "X-PachubeApiKey: " + apiKey + CRLF +
                "Content­Type: text/csv\r\n" +
                "Content­Length: " + contentBuffer.Length + CRLF +
                CRLF;
            byte[] headersBuffer = Encoding.UTF8.GetBytes(headers);
            connection.Send(requestLineBuffer);
            connection.Send(headersBuffer);
            connection.Send(contentBuffer);
            Thread.Sleep(400);
            int a = connection.Available;
            byte[] r = new byte[a];
            int cb = connection.Receive(r, 0, a, SocketFlags.None);
            connection.Close();
        }
        catch
        {
          Debug.Print("connection failed");
        }
    }

    public static void Main()
    {
        voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);
        lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
        highPort = new OutputPort(Pins.GPIO_PIN_A2, true);
        remoteEndPoint = new IPEndPoint(Dns.GetHostEntry("api.pachube.com").AddressList[0], 80);
        timer = new Timer(LogSensor, null, 0, interval);
        Thread.Sleep(Timeout.Infinite);
    }
}

En ce qui concerne la validité de la clé Pachube, j'ai vérifié avec cURL et les détails clés et aliments pour animaux sont à droite.

Était-ce utile?

La solution

En regardant http://msdn.microsoft.com/en- nous / bibliothèque / w3xtz6a5.aspx # Y0 , vous ne devriez pas avoir besoin de dormir pour attendre une réponse sur la prise en réception est une opération de blocage si le tampon est vide.

Que faire si vous avez essayé de simplifier votre code de réception pour quelque chose comme:

// Blocks until send returns.
connection.Send(...)

// Get reply from the server.
int byteCount = server.Receive(bytes, 0, connection.Available, 
                               SocketFlags.None);

if (byteCount > 0)
    Console.WriteLine(Encoding.UTF8.GetString(bytes));

Vous pouvez essayer explicitement de lire quelque chose comme 256 octets hors de la prise, au lieu du nombre disponible.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top