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:
- Create a same feed as http://www.pachube.com/feeds/23408
(A manual feed with three data streams with ids 0, 1, 2.) - Use your API key to replace the space holer PACHUBE_API_KEY below.
- 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
*/
#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(",");
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);
}