Pages: [1]   Go Down
Author Topic: need help with pachube  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I created an arduino webserver, and wanted to add graphs displaying the data of an analog sensor. I am using pachube to do this, but I am having problem. I tried to use the code from examples for pachube, but I am getting the message "connection failed". I am unsure what I am doing wrong.  I assume the issue is in this section
Code:
void sendData(int thisData) {
  // if there's a successful connection:
  if (client.connect()) {
    Serial.println("connecting...");
    // send the HTTP PUT request.
    // fill in your feed address here:
    client.print("PUT /api/39579.csv HTTP/1.1\n");
    client.print("Host: www.pachube.com\n");
    // fill in your Pachube API key here:
    client.print("X-PachubeApiKey:"my master api-key"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/csv\n");
    client.println("Connection: close\n");

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

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
I would greatly appreciate it if someone would tell me why it would work thanks.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I couldn't get the example sketch to work for Pachube in any way myself..Always a fail to connect.. I think it's an issue with the checking if connected section, seems to glitch the library.. There's a beta ethernet library somewhere on here that also includes DHCP but it's huge!!! That library worked fine with the pachube example though, so if it's a real simple sketch without adding too much, or if you have a 328, you could try that..

 I'm still working on mine, I took the web client sketch and re-wrote it to update pachube.. I then added a delay using microseconds and worked from there.. I don't need to read back what I sent, so I didn't include that part either.. I managed to get a web server added in as well so mine updates pachube every 30 seconds and also has a small web page showing the temperature of my garage.. I'm only using one sensor at the moment, should be room to add others..
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here's a link to the DHCP library I tried.. It worked great for me, I just didn't need dhcp, and I needed the room..
http://notebook.kulchenko.com/embedded/dhcp-and-dns-servers-with-arduino
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure if it uses the approach you used... but I have a Pachube connection working... sort of... details at...

http://sheepdogsoftware.co.uk/sc2wy.htm
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure if you've had any luck yet, I will post my code for a pachube client and a web server.. It seems to work ok but I'm sure it needs work, lol.. In fact if anyone want's to give me some tips to refine it, let me know.. I am totally unfamiliar with the ethernet library and kind of self taught to begin with..

Code:
/*
  Pachube sensor client and Web server
  This sketch connects analog 0 connected to an lm34 temp sensor
 Sends temp info to pachube and has a web server on port 80 using a Wiznet 810mj
 Circuit:
 * LM34 sensor output attached to analog in 0
 * Ethernet shield attached to pins 10, 11, 12, 13
  created using pachube and web server examples originally created by Tom Igoe
  modified Nov 9th 2011 by tek1229
 This code is in the public domain.
  */
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Enter a MAC address
byte ip[] = { 192,168,4,215 };  // The IP address will be dependent on your local network:
byte pachube[] = { 173,203,98,29 }; // pachube, orignal provided was incorrect..
Client client(pachube, 80); // ethernet client port
Server homeFeed(80);         // ethernet server port
long lastConnectionTime = 0;        // last time you connected to the server, in milliseconds
const int postingInterval = 20000;  //delay between updates to Pachube.com..
const byte webRefresh = 5 ;         // setting in seconds for the web page refresh-this page will auto refresh

void setup() {
  Ethernet.begin(mac, ip); // start the Ethernet connection:
  Serial.begin(9600);      // start the serial library:
  delay(1000);             // give the Ethernet shield a second to initialize:
  homeFeed.begin();         // start web server
  Serial.println("Initialized..."); // Simply a debug to show reset
         }
  void loop() {
  // Pachube timing loop.. uses generic millis timer to send data to pachube when needed
 int sensorReading = (analogRead(0)*.4882)-3;  // read the analog sensor and convert to temp-adjusted for slight variance
    if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(sensorReading);   // if you're not connected, and delay set above have passed then send data to pachube
         }
    //////// Web server section /////
     
     httpRequest(sensorReading);   // http request function--see if web page is being requested
      }
      ///////////////////////// end of loop ///////////////////////////////////////////////////
     
     
  void httpRequest(String tempData) {   // this function checks for a http request and if one is requested serves up a web page with temp..
            Client client = homeFeed.available();
  if (client) {
        boolean currentLineIsBlank = true;  // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.print("<font size = '6'>");
          client.print("<META HTTP-EQUIV='refresh' CONTENT='");
          client.print(webRefresh, DEC);
          client.print("'>");   
          client.println("<br />");
          client.println("<br />");
          client.print("<H1 ALIGN='CENTER'>Temperature of Garage is ");
          client.print(tempData);
          client.println("    degrees</H1");
          client.println("<br />");
          Serial.println("Web page accessed");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
   
    delay(1);   // give the web browser time to receive the data
   
    client.stop();   // close the connection:
   }
  }
             
           
void sendData(String thisData) {     // this function makes a HTTP connection to the pachube server:
   
    Serial.print(thisData); // debug
    Serial.println("  degrees ");  // debug 
    if (client.connect()) {       // if there's a successful connection:
    Serial.println("connecting to pachube...");  // debug
    client.print("PUT /api/XXXXX.csv HTTP/1.1\n"); // send the HTTP PUT request.  Change x's to your feed
    client.print("Host: www.pachube.com\n");                // fill in your feed address here:
    client.print("X-PachubeApiKey: <<<<<API KEY HERE>>>>>>\n");       // fill in your Pachube API key here:
    client.print("Content-Length: ");
    client.println(thisData.length(), DEC);

    // last pieces of the HTTP PUT request:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close\n");

    // here's the actual content of the PUT request:
    client.println(thisData);
    Serial.println("Send temp to Pachube "); // debug
    client.stop();
 
 
    lastConnectionTime = millis();           // note the time that the connection was made:
    }
  else {
     Serial.println("connection failed");   // if you couldn't make a connection:
 
  }
}

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

Pages: [1]   Go Up
Jump to: