Permalink
Browse files

Arduino: Add (by default disabled) Serial error prints for easier deb…

…ugging
  • Loading branch information...
1 parent c083d3d commit 7198ab3b4fe43eef8f479d9dfac5567f302e3b45 @pasky pasky committed Mar 4, 2013
Showing with 19 additions and 0 deletions.
  1. +19 −0 arduino/PubNub.cpp
View
@@ -2,6 +2,16 @@
#include <Ethernet.h>
#include "PubNub.h"
+// #define PUBNUB_DEBUG 1
+
+#ifdef PUBNUB_DEBUG
+#define DBGprint(x...) Serial.print(x)
+#define DBGprintln(x...) Serial.println(x)
+#else
+#define DBGprint(x...)
+#define DBGprintln(x...)
+#endif
+
class PubNub PubNub;
bool PubNub::begin(char *publish_key_, char *subscribe_key_, char *origin_)
@@ -21,6 +31,7 @@ EthernetClient *PubNub::publish(char *channel, char *message, int timeout)
/* connect() timeout is about 30s, much lower than our usual
* timeout is. */
if (!client.connect(origin, 80)) {
+ DBGprintln("Connection error");
client.stop();
return NULL;
}
@@ -82,6 +93,7 @@ PubSubClient *PubNub::subscribe(char *channel, int timeout)
/* connect() timeout is about 30s, much lower than our usual
* timeout is. */
if (!client.connect(origin, 80)) {
+ DBGprintln("Connection error");
client.stop();
return NULL;
}
@@ -103,6 +115,7 @@ PubSubClient *PubNub::subscribe(char *channel, int timeout)
|| !client.connected()
|| client.read() != '[') {
/* Something unexpected. */
+ DBGprintln("Unexpected body in subscribe");
client.stop();
return NULL;
}
@@ -133,6 +146,7 @@ EthernetClient *PubNub::history(char *channel, int limit, int timeout)
retry:
t_start = millis();
if (!client.connect(origin, 80)) {
+ DBGprintln("Connection error");
client.stop();
return NULL;
}
@@ -174,11 +188,13 @@ enum PubNub_BH PubNub::_request_bh(EthernetClient &client, unsigned long t_start
while (client.connected() && !client.available()) { \
/* wait, just check for timeout */ \
if (millis() - t_start > (unsigned long) timeout * 1000) { \
+ DBGprintln("Timeout in bottom half"); \
return PubNub_BH_TIMEOUT; \
} \
} \
if (!client.connected()) { \
/* Oops, connection interrupted. */ \
+ DBGprintln("Connection reset in bottom half"); \
return PubNub_BH_ERROR; \
} \
} while (0)
@@ -194,6 +210,9 @@ enum PubNub_BH PubNub::_request_bh(EthernetClient &client, unsigned long t_start
if (c != '2') {
/* HTTP code that is NOT 2xx means trouble.
* kthxbai */
+ DBGprint("Wrong HTTP status first digit ");
+ DBGprint((int) c, DEC);
+ DBGprintln(" in bottom half");
return PubNub_BH_ERROR;
}

0 comments on commit 7198ab3

Please sign in to comment.