Pages: [1]   Go Down
Author Topic: pachube separate put and get  (Read 5172 times)
0 Members and 1 Guest are viewing this topic.
Moss
Offline Offline
Full Member
***
Karma: 1
Posts: 119
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

Does anyone have a working example of a get function for pachube (remote input to arduino from a pachube feed)? the one I have seen on the pachube tutorial is both get and put, but I have not managed to separate the get call.

Joachim
Logged

Oxford, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got a reliable PUT but not a GET - I've found that the code on the Pachube site doesn't survive a router re-connect - it goes into a continuous loop without connecting again. That's why they have to use the watchdog. I've managed to solve this without having to modify the boot loader.

Let me know if you need this code.
Logged

Moss
Offline Offline
Full Member
***
Karma: 1
Posts: 119
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Exellent. Yes please, i would love to see your code
Logged

Oxford, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the code I've been testing and my board - It's been very reliable and can survive disconnection of the Ethernet cable, router re-connect and also power disruption if you bend the pin out on the Ethernet board and connect one of the digital outputs to the reset pin. I used a piece of wire.

I've added a series of LEDs so I can see the system state when it's running from the power supply.



Code:
/*

  PachubeClientTest
  
  Author: Arduinomstr
  Date: 21-12-2010
  Version: 1.1
 
  Sketch to test updating Pachube feed using Official Arduino
  Ethernet shield - Reset pin on Ethernet Controller needs bending
  out of header and connecting to pin 8 to provide hardware reset
  
  Updates feed with fixed data and displays response on serial monitor
  
  Tested on Duemilanove, DFRobot Shield and Arduino 0022

*/

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

#define PACHUBE_FEED_ID XXX
#define PACHUBE_API_KEY "XXXX"
#define PACHUBE_UPDATE_INTERVAL 10000                  /* Delay between updates, milliseconds      */
#define PACHUBE_TEST_DATA "15.5,12"                  /* Hard coded test data                  */

#define RESET_PIN 8                               /* Reset Ethernet using Arduino            */
#define WAITING 2                               /* Waiting state                  */
#define CONNECTING 3                               /* Connecting state                  */
#define SENDING 4                               /* Sending state                  */
#define FAILING 5                               /* Failing state                  */
#define READING 6                               /* Reading state                  */
#define DISCONNECTING 7                         /* Disconnecting state                  */
#define FIRST_LED WAITING                        /* First in consecutive LEDs          */
#define LAST_LED DISCONNECTING                        /* Last LED                        */

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { XXX, XXX, XXX, XXX };
byte gateway[] = { XXX, XXX, XXX, XXX };
byte subnet[] = { XXX, XXX, XXX, XXX };
byte server[] = { 173, 203, 98, 29 };                   /* www.pachube.com                  */
int state = 0;                                     /* System state                        */

Client client(server, 80);                        /* Client listening on port 80            */

String pachubeString = PACHUBE_TEST_DATA;            /* Construct string using test data      */

// Set-up Arduino I/O and Ethernet Controller
// Includes fix to kick-start Ethernet on power-up /
// power-failure by using reset signal from Arduino
// to reset-pin on Ethernet Shield
void setup()
{
  pinMode(RESET_PIN, OUTPUT);                        /* Configure I/O pins as outputs      */
  pinMode(WAITING, OUTPUT);
  pinMode(CONNECTING, OUTPUT);  
  pinMode(SENDING, OUTPUT);
  pinMode(FAILING, OUTPUT);
  pinMode(READING, OUTPUT);
  pinMode(DISCONNECTING, OUTPUT);

  // Start fix
  digitalWrite(RESET_PIN, LOW);                        /* Take Ethernet reset line low            */
  delay(200);                                    /* momentarily                        */
  digitalWrite(RESET_PIN, HIGH);                  /* Bring it up again                  */
  delay(2000);                                    /* Allow Ethernet time to boot            */
  // End of fix
  
  Serial.begin(9600);                              /* Initialise serial line on Arduino      */  
  Ethernet.begin(mac, ip, gateway, subnet);            /* Initialise Ethernet settings            */
}

// Main function
void loop()
{
  Serial.println("WAITING....");  
  waiting();                                      /* Provide delay between updates      */
  
  if (!client.connected()) {
    Serial.println("CONNECTING.......");
    connecting();                                /* Connect if no connection            */
  }
  
  if (client.connected()) {
    Serial.println("SENDING........");
    sending();                                /* Do PUT if connection is present      */
    Serial.print("Data sent: ");
    Serial.println(pachubeString);
  } else {
    Serial.println("FAILING........");
    fail();                                    /* Otherwise enter failure state      */
  }  
  
  if (client.available()) {
    Serial.println("READING......");
    reading();                                    /* Output response if there is one      */
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("DISCONNECTING....");
    disconnecting();                              /* Stop client if disconnected            */
  }
}                                          /* End of main loop                  */

// Set WAITING state and
// wait for update interval
void waiting() {
  state = WAITING;                              /* Set state                        */
  showStatus();                                    /* Turn on LED                        */
  delay(PACHUBE_UPDATE_INTERVAL);                  /* Delay until net update            */
}

// Set CONNECTING state and try to connect
void connecting() {
  state = CONNECTING;                              /* Set state                        */
  showStatus();                                    /* Turn on LED                        */
  client.connect();                                /* Try to connect                  */
}

// Set SENDING state
// build PUT request and send data
void sending() {
  state = SENDING;                              /* Set state                        */
  showStatus();                                    /* Turn on LED                        */
  client.print("PUT /api/feeds/");
  client.print(PACHUBE_FEED_ID);
  client.println(".csv HTTP/1.0");                   /* update using csv format            */
  client.println("Host: www.pachube.com");
  client.print("X-PachubeApiKey: ");
  client.println(PACHUBE_API_KEY);
  client.print("Content-Length: ");
  client.println(pachubeString.length(), DEC);              /* Calculate length of request            */    
  client.println("Content-Type: text/csv");
  client.println("Connection: close");    
  client.println();
  client.println(pachubeString);                  /* Output test data                  */  
}

// Set FAILING state and continue
void fail() {
  state = FAILING;                              /* Set state                        */
  showStatus();                                    /* Turn on LED                        */
}

// Set READING state and keep reading
// bytes from buffer until it is empty
// outputting bytes to serial monitor
void reading() {
  state = READING;                              /* Set state                        */                                                
  showStatus();                                    /* Turn on LED                        */
  while (client.available()) {                        /* When buffer has data                  */
    char c = client.read();                        /* Read byte                      */
    Serial.print(c);                              /* Output byte                        */
  }
}

// Set DISCONNECTING state
// and stop client
void disconnecting() {
  state = DISCONNECTING;                        /* Set state                        */
  showStatus();                                    /* Turn on LED                        */
  client.stop();                                /* Stop client                        */
}

// Turn off each LEDs unless it
// matches current status
void showStatus() {
  for (int LED=FIRST_LED; LED<=LAST_LED; LED++){
    if (LED != state) {                              /* Turn off LED is pin does not            */
      digitalWrite(LED, LOW);                        /* match state                        */
    }
    else {
      digitalWrite(LED, HIGH);                        /* Turn on LED when it matches            */
    }                                          /* state                        */
  }
  delay(2000);                                     /* Keep LED on for short while            */
}
« Last Edit: January 23, 2011, 01:25:21 pm by arduinomstr » Logged

Oxford, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a picture of my test board
Logged

Moss
Offline Offline
Full Member
***
Karma: 1
Posts: 119
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot, neat set-up. Please beware that you are showing your api-key...
Logged

Oxford, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops! I removed the key - thanks. You should be able to convert the PUT to a GET easily enough
Logged

Pages: [1]   Go Up
Jump to: