Usare Pachube con più datastream

Oggi ho giocato un pò con il codice per inviare più dati su più datastream di pachube.
Per ogni datastream si possono mandare solo un dato alla volta: non si possono mandare contemporaneamente più dati da rappresentare su un solo grafo o più dati con un unica PUT.
Il modo più semplice per arginare tale problema è usare un algoritmo di scheduling Round Robin: inviare i dati a turno.

Esempio:
*stesso FEED

loop
PUT su DATASTREAM_1 del DATO_X
wait 12secondi
PUT su DATASTREAM_2 del DATO_Y
wait 12secondi

Il risultato è stato questo:

capturebez

Codice:

/*
  Pachube sensor client
 
 This sketch connects an analog sensor to Pachube (http://www.pachube.com) with one feed
 and two datastreams using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 the Adafruit Ethernet shield, either one will work, as long as it's got
 a Wiznet Ethernet module on board.
 
 Circuit:
 * Analog sensors attached to analog in 2 and 3
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 This code is in the public domain. 
 
 UPDATE:
 
 *Using Pachube's V2 API
 *Analog sensor attached to analog in 2 and 3 
 *New pachube.com IP
 *Using one feed and two datastreams: first for analog 2 and second for analog 3
 
 08/10/2011
 
 by Andrea Esposito
 www.blackstufflabs.com
 
 */

#include <SPI.h>
#include <Ethernet.h>

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// assign an IP address for the controller:
byte ip[] = { 
  192,168,1,20 };
byte gateway[] = {
  192,168,1,1};	
byte subnet[] = { 
  255, 255, 255, 0 };

//  The address of the server you want to connect to (pachube.com):
byte server[] = { 
  173,203,98,29 }; 

// initialize the library instance:
Client client(server, 80);

long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds
boolean lastConnected = false;      // state of the connection last time through the main loop
const int postingInterval = 12000;  //delay between updates to Pachube.com
short flag=0;

void setup() {
  // start the ethernet connection and serial port:
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  // give the ethernet module time to boot up:
  delay(1000);
}

void loop() {

  //FEED number on your account settings
  //ex.: String FEED=37040;
  String FEED=YOUR_FEED_HERE;

  //ID_DATASTREAMS setted by you
  //ex.:String ID_STREAMING_1="1";
  String ID_STREAMING_1="YOUR_FIRST_ID_STREAM_HERE";
  String ID_STREAMING_2="YOUR_SECOND_ID_STREAM_HERE";

  // voltage=5v but on my board is 4.91v 
  float voltage=4.91; 

  // read the analog sensor:
  //LM35 on A2
  int sensorCelsius = analogRead(2); 
  sensorCelsius = (voltage * sensorCelsius * 100.0)/1024.0;

  //LM335 on A3
  int sensorKelvin = analogRead(3); 
  sensorKelvin = (voltage * sensorKelvin * 100.0)/1024.0;
  //sensorKelving-=273.15; //if you want it in C°

  // if there's incoming data from the net connection.
  // send it out the serial port. This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data: 
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) 
  { //Round Robin
    if (flag==0) //sending data to first datastream
    {
      sendData(sensorCelsius, FEED, ID_STREAMING_1); 
      flag=1;
      Serial.print("ID_STREAMING_1 = ");
      Serial.println(ID_STREAMING_1);
    }
    else
    { //sending data to second datastream
      sendData(sensorKelvin, FEED, ID_STREAMING_2); 
      flag=0;
      Serial.print("ID_STREAMING_2 = ");
      Serial.println(ID_STREAMING_2);
    }
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData , String myfeed , String myid) {

  //Your API_KEY on your account settings
  String API_KEY="YOUR_API_KEY_HERE"; 

  // if there's a successful connection:
  if (client.connect()) {
    Serial.print("Temperature: ");
    Serial.println(thisData);
    Serial.println("connecting...");

    // send the HTTP PUT request. 
    // fill in your feed address here:
    // PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
    String myput="PUT /v2/feeds/";
    myput+=myfeed;
    myput+="/datastreams/";
    myput+=myid;
    myput+=" HTTP/1.1n";
    Serial.println(myput);
    client.print(myput);

    client.print("Host: api.pachube.comn");
    // fill in your Pachube API key here:
    client.print("X-PachubeApiKey: ");
    client.print(API_KEY);
    client.print("n");
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    int thisLength = getLength(thisData);
    client.println(thisLength, DEC);

    // last pieces of the HTTP PUT request:
    client.print("Content-Type: text/csvn");
    client.println("Connection: closen");

    // here's the actual content of the PUT request:
    client.println(thisData, DEC);

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}

2 pensieri su “Usare Pachube con più datastream

  1. Conti Valentino

    Ciao, sto facendo prove con Pachube ed ho visto ora il tuo sketch.
    Vorrei inviare i dati di 6 sensori, puoi darmi una info su come modificare il tuo codice ?

    Ciao Valentino

    Rispondi

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Anti-Spam Quiz:

 

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.