Skip to content
Browse files

Fixed issues with incorrect connection settings.

  • Loading branch information...
1 parent e323993 commit 6b945082f7a29a1322c0269dcc4f1c9986bff01d Ben Lawson committed Dec 21, 2011
Showing with 84 additions and 39 deletions.
  1. +81 −34 Pachube.cpp
  2. +3 −5 Pachube.h
View
115 Pachube.cpp
@@ -1,52 +1,104 @@
#include "Arduino.h"
-#include "SPI.h"
-#include "Ethernet.h"
+#include <Ethernet.h>
#include "Pachube.h"
-
-byte *mac;
-char *apiKey;
-
-PachubeClient::PachubeClient(byte *macAddress[], char *apiKey[], int feedId)
+#include "Time.h"
+
+byte *_mac;
+char *_api;
+uint16_t _feed;
+uint16_t _datastream;
+long _lastConnMillis = 0;
+bool lastConnected = false;
+const int _interval = 10000;
+EthernetClient _client;
+
+PachubeClient::PachubeClient(byte macAddress[], char apiKey[], int feedId, int datastreamId)
{
- _macAddress = macAddress;
- _apiKey = apiKey;
- _feedId = feedId;
+ _mac = macAddress;
+ _api= apiKey;
+ _feed = feedId;
+ _datastream = datastreamId;
}
-PachubeClient::open()
+bool PachubeClient::openConnection()
{
Serial.begin(9600);
- if (Ethernet.begin(_macAddress) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
+ if (Ethernet.begin(_mac) == 0) {
+ delay(1000);
+
+ if (Ethernet.begin(_mac) == 0) {
+ Serial.println("Failed to configure Ethernet");
+ return false;
+ }
+ }
+ return true;
+}
+
+void PachubeClient::updateFeed(int dataToSend) {
+ if (_client.available()) {
+ char c = _client.read();
+ Serial.print(c);
+ }
+
+ if (!_client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ _client.stop();
+ }
+
+ if (!_client.connected() && (millis() - _lastConnMillis > _interval)) {
@blawson
Owner
blawson added a note Feb 7, 2012

I know it looks confusing, but what the condition is saying here is if you aren't connected (as we have disconnected after the previous send), and the interval has passed, proceed to sendData. In sendData we re-connect to api.pachube.com and transmit the info. It could probably do with some refactoring for clarity though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ sendData(dataToSend);
+ _lastConnMillis = millis();
+ }
- throw;
+ lastConnected = _client.connected();
}
-PachubeClient::sendToFeed(int dataToSend)
+void PachubeClient::sendData(int dataToSend)
{
if (_client.connect("api.pachube.com", 80)) {
Serial.println("Connecting to Pachube...");
- client.print("PUT /v2/"+_feedId+".csv HTTP/1.1\n");
- client.print("Host: www.pachube.com\n");
+ _client.print("PUT /v2/feeds/");
+ _client.print(_feed);
+ _client.print("/datastreams/");
+ _client.print(_datastream);
+ _client.print(".csv HTTP/1.1\n");
+ _client.print("Host: api.pachube.com\n");
- client.print("X-PachubeApiKey: "+_apiKey+"\n");
- client.print("Content-Length: ");
+ _client.print("X-PachubeApiKey: ");
+ _client.print(_api);
+ _client.print("\n");
+ _client.print("Content-Length: ");
int lengthOfData = getLength(dataToSend);
-
- client.print("Content-Type: text/csv\n");
- client.println("Connection: close\n");
-
- client.println(dataToSend, DEC);
- }
- else {
- Serial.println("Connection failed.");
+ time_t _time = now();
+ _client.println(lengthOfData, DEC);
+
+ _client.print("Content-Type: text/csv\n");
+ _client.println("Connection: close\n");
+
+ Serial.println(_time);
+
+ _client.print(dataToSend, DEC);
+ _client.print(",");
+ _client.println(year(_time));
+ _client.print("-");
+ _client.print(month(_time));
+ _client.print("-");
+ _client.print(day(_time));
+ _client.print("T");
+ _client.print(hour(_time));
+ _client.print(":");
+ _client.print(minute(_time));
+ _client.print(":");
+ _client.print(second(_time));
+ _client.print("Z");
}
}
-int getLength(int someValue) {
+int PachubeClient::getLength(int someValue) {
//there's at least one byte:
int digits = 1;
@@ -63,13 +115,8 @@ int getLength(int someValue) {
return digits;
}
-PachubeClient::readFromFeed()
+void PachubeClient::readFromFeed()
{
}
-PachubeClient::~PachubeClient()
-{
- // Close the socket
- _client.close();
-}
View
8 Pachube.h
@@ -1,26 +1,24 @@
#ifndef Pachube_h
#define Pachube_h
+#include <Ethernet.h>
#include "Arduino.h"
-#include "Ethernet.h"
-#include "Time/Time.h"
+#include "Time.h"
class PachubeClient
{
public:
PachubeClient(byte macAddress[], char apiKey[], int feedId, int datastreamId);
- ~PachubeClient();
bool openConnection();
void updateFeed(int dataToSend);
- void sendData(int dataToSend);
void readFromFeed();
bool closeConnection();
int getLength(int data);
private:
+ void sendData(int dataToSend);
byte *_macAddress[];
char *_apiKey[];
int _feedId;
- EthernetClient _client;
};
#endif

0 comments on commit 6b94508

Please sign in to comment.
Something went wrong with that request. Please try again.