Pages: [1]   Go Down
Author Topic: Problems with multiple feeds to Pachube  (Read 1031 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new with Arduino/any sort of programming so please excuse my ignorance. I am trying to read 3 digital pulse sensors and post them to Pachube. I know I am communicating with Pachube because it is updating with zero. I started with reading the inputs and had everything working fine. Once I add the Ethernet and the Data feed to Pachube, it quits counting. Any help understanding what I am doing wrong would be greatly appreciated.


/*
Pachube Data Out

Demonstrates use of the ERxPachube library.
Push local sensor data to Pachube server.
If you don't have a Pachube account, register one first (http://www.pachube.com/).

To run this sketch, you need:
 1. Create a same feed as http://www.pachube.com/feeds/23408
    (A manual feed with three data streams with ids 0, 1, 2.)
 2. Use your API key to replace the space holer PACHUBE_API_KEY below.
 3. Use your feed id to replace the space holer feed id 23408 below.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

* Created 22 April 2011
* By Jeffrey Sun
* http://code.google.com/p/pachubelibrary/

*/

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

byte mac[] = { 0xCC, 0xAC, 0xBE, 0xEF, 0xFE, 0x91 }; // make sure this is unique on your network
byte ip[] = { 192, 168, 69, 245   };                  // no DHCP so we set our own IP address

#define PACHUBE_API_KEY            "*********************************" // fill in your API key PACHUBE_API_KEY
#define PACHUBE_FEED_ID            ***** // fill in your feed id

ERxPachubeDataOut dataout(PACHUBE_API_KEY, PACHUBE_FEED_ID);

void PrintDataStream(const ERxPachube& pachube);
void PrintString(const String& message) ;


/////////////////////////Start integer Setup///////////////////////////////////////////////////
const int  freshwaterinput = 2;    // the pin that the fresh water is attached to
const int  irrigationwaterinput = 3; //the pin irrigation water is attached to
const int  gasinput = 4; //the pin gas meter is attached to

// Variables will change:
int FreshWaterCounter = 0;   // counter for the number of fresh water
int FreshGallonState = 0;         // current state of Fresh Gallons
int lastFreshGallonState = 0;     // previous state of the Fresh Gallons
int IrrigationWaterCounter = 0;   // counter for the number of irrigation water
int IrrigationGallonState = 0;    // current state of irrigation
int LastIrrigationGallonState = 0; // last state of irrigation
int GasUnitCounter = 0;            //counter for gas units
int GasUnitState = 0;              //current state of gas meter
int LastGasUnitState = 0;          //last state of gas meter

/////////////////////////////////////End Of Integer Setup///////////////////////////////////////////


void setup() {

   Serial.begin(9600);
   Ethernet.begin(mac, ip);

   dataout.addData(0);
   dataout.addData(1);
   dataout.addData(2);


        pinMode(freshwaterinput, INPUT);
        pinMode(irrigationwaterinput, INPUT);
        pinMode(gasinput, INPUT);
}

void loop()
{
  Serial.println("Starting loop");   
 /////////////////////////////////////////////
    //Begining of fresh water loop

// read the fresh water input pin 2:
  FreshGallonState = digitalRead(freshwaterinput);
  // compare the buttonState to its previous state
  if (FreshGallonState != lastFreshGallonState) {
    // if the state has changed, increment the counter
    if (FreshGallonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      FreshWaterCounter++;
      Serial.println("on");
      Serial.print("Fresh water gallons used:  ");
      Serial.println(FreshWaterCounter, DEC);
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
 
  // save the current state as the last state,
  //for next time through the loop
  lastFreshGallonState = FreshGallonState;
  }

//Begining of irrigation loop
  //Read irrigation water input pin 3
IrrigationGallonState = digitalRead(irrigationwaterinput);
  // compare the buttonState to its previous state
  if (IrrigationGallonState != LastIrrigationGallonState) {
    // if the state has changed, increment the counter
    if (IrrigationGallonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      IrrigationWaterCounter++;
      Serial.println("on");
      Serial.print("Irrigation water gallons used:  ");
      Serial.println(IrrigationWaterCounter, DEC);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  LastIrrigationGallonState = IrrigationGallonState;

  //Begining of gas loop
  //Read Gas input pin 4
 
GasUnitState = digitalRead(gasinput);
  // compare the buttonState to its previous state
  if (GasUnitState != LastGasUnitState) {
    // if the state has changed, increment the counter
    if (GasUnitState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      GasUnitCounter++;
      Serial.println("on");
      Serial.print("Gas units used:  ");
      Serial.println(GasUnitCounter, DEC);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  LastGasUnitState = GasUnitState;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
 
        dataout.updateData(0, FreshWaterCounter);
   dataout.updateData(1, IrrigationWaterCounter);
   dataout.updateData(2, GasUnitCounter);
   int status = dataout.updatePachube();

   Serial.print("sync status code <OK == 200> => ");
   Serial.println(status);

   PrintDataStream(dataout);

   delay(100000);
}
void PrintDataStream(const ERxPachube& pachube)
{
   unsigned int count = pachube.countDatastreams();
   Serial.print("data count=> ");
   Serial.println(count);

   Serial.println("<id>,<value>");
   for(unsigned int i = 0; i < count; i++)
   {
      Serial.print(pachube.getIdByIndex(i));
      Serial.print(",");
      PrintString(pachube.getValueByIndex(i));
      Serial.println();
   }
}

void PrintString(const String& message)
{
   String tmpStr = message;
   int bufferSize = tmpStr.length() +1;
   char pBuffer[bufferSize];
   tmpStr.toCharArray(pBuffer, bufferSize);

   Serial.print(pBuffer);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   delay(100000);
Literals are treated as ints, even though delay() takes an unsigned long. You need UL on the end of your literal to force the compiler to treat it as an unsigned long.

You must like wasting memory.
Code:
   String tmpStr = message;
Making a copy of the string is unnecessary.
Code:
   char pBuffer[bufferSize];
   tmpStr.toCharArray(pBuffer, bufferSize);

   Serial.print(pBuffer);
So is making a copy of the contents of the copy of the string. Serial::print() has an overload that takes a String object.

Quote
I know I am communicating with Pachube because it is updating with zero.
It? What does "it" refer to?

Quote
Once I add the Ethernet and the Data feed to Pachube, it quits counting.
Once again, what does "it" refer to?

Have you checked how much FreeMemory (search term) you have?
Logged

China
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Once I add the Ethernet and the Data feed to Pachube, it quits counting.
It seems the problem of the code "delay(100000);". Have you wait enough time to finish the delay?

Regarding the function "void PrintString(const String& message)", I think I should remove it from the example and use the "Built in" print function. Thanks, PaulS.
Logged

Pages: [1]   Go Up
Jump to: