Archivi tag: multiple

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.

*stesso FEED

wait 12secondi
wait 12secondi

Il risultato è stato questo:



  Pachube sensor client
 This sketch connects an analog sensor to Pachube ( 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.
 * 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. 
 *Using Pachube's V2 API
 *Analog sensor attached to analog in 2 and 3 
 *New IP
 *Using one feed and two datastreams: first for analog 2 and second for analog 3
 by Andrea Esposito

#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[] = {
byte subnet[] = { 
  255, 255, 255, 0 };

//  The address of the server you want to connect to (
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
short flag=0;

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

void loop() {

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

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

  // 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 =;

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

  // 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); 
      Serial.print("ID_STREAMING_1 = ");
    { //sending data to second datastream
      sendData(sensorKelvin, FEED, ID_STREAMING_2); 
      Serial.print("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

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

    // 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+=" HTTP/1.1n";

    client.print("Host: api.pachube.comn");
    // fill in your Pachube API key here:
    client.print("X-PachubeApiKey: ");
    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;
  // return the number of digits:
  return digits;