Go Down

Topic: Why wont this program post to pachtube (Read 807 times) previous topic - next topic

lacrosse1991

I am currently working on setting up an ethernet shield connected arduino that posts information obtained from a dust sensor to pachtube. Im pretty sure that the non-pachtube related stuff works fine as ive tested it out by just making it post information via serial, but for some reason I cannot get it to post to pachtube at all, although it does not give me a "connection failed" message. If someone could point out what I am doing wrong here I would really appreciate it as I am clueless as to what I can do to fix this problem at this point in time, thanks! also for the actual project example I am referencing from, here is the url http://www.howmuchsnow.com/arduino/airquality/grovedust/. Below is the code, I have altered the Key for pachtube of course, but the feed ID is the same.

Code: [Select]

    /*
     Interface to Shinyei Model PPD42NS Particle Sensor
     Program by Christopher Nafis
     Written April 2012

     http://www.seeedstudio.com/depot/grove-dust-sensor-p-1050.html
     http://www.sca-shinyei.com/pdf/PPD42NS.pdf

     JST Pin 1 (Black Wire)  => Arduino GND
     JST Pin 3 (Red wire)    => Arduino 5VDC
     JST Pin 4 (Yellow wire) => Arduino Digital Pin 8
     */

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

    byte mac[] = {
      0x90, 0xA2, 0xDA, 0x0D, 0x27, 0xBF};
    IPAddress ip(192,168,1,89);
    IPAddress gateway(192,168,1,1);
    IPAddress subnet(255, 255, 255, 0);

    // initialize the library instance:
    EthernetClient client;

    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 = 10000;  //delay between updates to Pachube.com

    int pin = 8;
    //int ledPin =  13;
    //int ledPin2 = 12;
    unsigned long duration;
    unsigned long starttime;
    unsigned long sampletime_ms = 30000;
    unsigned long lowpulseoccupancy = 0;
    float ratio = 0;
    float concentration = 0;
    char s[32];

    void setup(){
      Serial.begin(9600);

      // give the ethernet module time to boot up:
      delay(1000);
      // start the Ethernet connection:
      if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // Configure manually:
        Ethernet.begin(mac, ip);
      }
      pinMode(8,INPUT);
      starttime = millis();
      //pinMode(ledPin, OUTPUT);
      //pinMode(ledPin2, OUTPUT);
      //digitalWrite(ledPin, HIGH);
      //digitalWrite(ledPin2, LOW);
    }

    void loop(){
       duration = pulseIn(pin, LOW);
      lowpulseoccupancy = lowpulseoccupancy+duration;

      if ((millis()-starttime) > sampletime_ms)
      {
        //digitalWrite(ledPin2, LOW);
        //digitalWrite(ledPin, HIGH);
        //digitalWrite(ledPin2, HIGH);
        //digitalWrite(ledPin, LOW);
        //delay(1000);
        //digitalWrite(ledPin, HIGH);
        //digitalWrite(ledPin2, LOW);
        ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
        concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
        Serial.print(lowpulseoccupancy);
        Serial.print(",");
        Serial.print(ratio);
        Serial.print(",");
        Serial.println(concentration);
        lowpulseoccupancy = 0;
        starttime = millis();
      }

      // 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)) {
        String dataString = "";
        dataString += dtostrf(concentration, 9, 4, s);
        dataString += ",";
        dataString += dtostrf(ratio, 9, 4, s);
        sendData(dataString);
        Serial.println(dataString);
        delay(1000);
      }
      // 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(String thisData) {
      // if there's a successful connection:
      if (client.connect("www.pachube.com", 80)) {
         Serial.println("connecting...");
        // send the HTTP PUT request.
        // fill in your feed address here:
        client.print("PUT /api/67259.csv HTTP/1.1\n");
        client.print("Host: www.pachube.com\n");
        // fill in your Pachube API key here:
        client.print("X-PachubeApiKey: LAjqaUq0e_btR9dMaOWuSGJS766SAKxuS3lISlBvT0dOVT0g\n");
        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);

        // note the time that the connection was made:
        lastConnectionTime = millis();


      }
      else {
        // if you couldn't make a connection:
        Serial.println("connection failed");
      }
    }

pylon

First of all you don't get the server's response. You won't get an idea of what's going wrong if you don't care for what the server is telling you. Reading the response is also necessary to free the WIZnet socket, else you waste your 4 available sockets very fast.

You should also get rid of the String objects as the class is fragmenting your memory very fast. You should never use it in any sketch except a beginners practice. Use character arrays instead.

Go Up