Skip to content

Commit 4e83f46

Browse files
committed
set up a default error printer
1 parent 2887983 commit 4e83f46

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

AdafruitIO.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ AdafruitIO_Feed* AdafruitIO::feed(const __FlashStringHelper *name)
5959
return new AdafruitIO_Feed(this, name);
6060
}
6161

62+
void errorCallback(char *err, uint16_t len)
63+
{
64+
AIO_ERR_PRINTLN();
65+
AIO_ERR_PRINT("ERROR: ");
66+
AIO_ERR_PRINTLN(err);
67+
AIO_ERR_PRINTLN();
68+
}
69+
6270
void AdafruitIO::_init()
6371
{
6472
// we have never pinged, so set last ping to now
@@ -79,6 +87,7 @@ void AdafruitIO::_init()
7987
// setup error sub
8088
_err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic);
8189
_mqtt->subscribe(_err_sub);
90+
_err_sub->setCallback(errorCallback);
8291

8392
} else {
8493

@@ -99,6 +108,7 @@ void AdafruitIO::_init()
99108
// setup throttle sub
100109
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
101110
_mqtt->subscribe(_throttle_sub);
111+
_throttle_sub->setCallback(errorCallback);
102112

103113
} else {
104114

@@ -108,12 +118,6 @@ void AdafruitIO::_init()
108118
}
109119
}
110120

111-
void AdafruitIO::setErrorHandler(SubscribeCallbackBufferType cb)
112-
{
113-
_err_sub->setCallback(cb);
114-
_throttle_sub->setCallback(cb);
115-
}
116-
117121
const __FlashStringHelper* AdafruitIO::statusText()
118122
{
119123
switch(_status) {

AdafruitIO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ class AdafruitIO {
3232

3333
void connect(const char *user, const char *key);
3434
void connect(const __FlashStringHelper *user, const __FlashStringHelper *key);
35+
3536
void run();
36-
void setErrorHandler(SubscribeCallbackBufferType cb);
3737

3838
AdafruitIO_Feed* feed(const char *name);
3939
AdafruitIO_Feed* feed(const __FlashStringHelper *name);
40+
4041
const __FlashStringHelper* statusText();
4142

4243
aio_status_t status();

AdafruitIO_Definitions.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ class AdafruitIO_Data;
1717

1818
typedef void (*AdafruitIODataCallbackType)(AdafruitIO_Data *data);
1919

20+
// uncomment/comment to turn on/off error output
21+
#define AIO_ERROR
22+
23+
// where debug messages will be printed
24+
#define AIO_PRINTER Serial
25+
26+
#ifdef AIO_ERROR
27+
#define AIO_ERR_PRINT(...) { AIO_PRINTER.print(__VA_ARGS__); }
28+
#define AIO_ERR_PRINTLN(...) { AIO_PRINTER.println(__VA_ARGS__); }
29+
#define AIO_ERR_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
30+
#else
31+
#define AIO_ERR_PRINT(...) {}
32+
#define AIO_ERR_PRINTLN(...) {}
33+
#define AIO_ERR_PRINTBUFFER(buffer, len) {}
34+
#endif
35+
2036
#define AIO_PING_INTERVAL 60000
2137
#define AIO_THROTTLE_RECONNECT_INTERVAL 60000
2238

0 commit comments

Comments
 (0)