Skip to content

Commit 581650c

Browse files
committed
rework constructor to fix sketches without subscriptions
1 parent ded9cde commit 581650c

File tree

6 files changed

+65
-85
lines changed

6 files changed

+65
-85
lines changed

AdafruitIO.cpp

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,59 @@
77
//
88
#include "AdafruitIO.h"
99

10-
AdafruitIO::AdafruitIO()
10+
AdafruitIO::AdafruitIO(const char *user, const char *key)
1111
{
1212
_mqtt = 0;
13-
_username = 0;
14-
_key = 0;
13+
_username = user;
14+
_key = key;
15+
_err_topic = 0;
16+
_throttle_topic = 0;
17+
_err_sub = 0;
18+
_throttle_sub = 0;
19+
20+
_init();
21+
}
22+
23+
AdafruitIO::AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key)
24+
{
25+
_mqtt = 0;
26+
_username = (const char*)user;
27+
_key = (const char*)key;
1528
_err_topic = 0;
1629
_throttle_topic = 0;
1730
_err_sub = 0;
1831
_throttle_sub = 0;
32+
33+
_init();
34+
}
35+
36+
void errorCallback(char *err, uint16_t len)
37+
{
38+
AIO_ERR_PRINTLN();
39+
AIO_ERR_PRINT("ERROR: ");
40+
AIO_ERR_PRINTLN(err);
41+
AIO_ERR_PRINTLN();
42+
}
43+
44+
void AdafruitIO::connect()
45+
{
46+
47+
if(_err_sub) {
48+
// setup error sub
49+
_err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic);
50+
_mqtt->subscribe(_err_sub);
51+
_err_sub->setCallback(errorCallback);
52+
}
53+
54+
if(_throttle_sub) {
55+
// setup throttle sub
56+
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
57+
_mqtt->subscribe(_throttle_sub);
58+
_throttle_sub->setCallback(errorCallback);
59+
}
60+
61+
_connect();
62+
1963
}
2064

2165
AdafruitIO::~AdafruitIO()
@@ -33,22 +77,6 @@ AdafruitIO::~AdafruitIO()
3377
delete _throttle_sub;
3478
}
3579

36-
void AdafruitIO::connect(const char *user, const char *key)
37-
{
38-
_username = user;
39-
_key = key;
40-
41-
_init();
42-
}
43-
44-
void AdafruitIO::connect(const __FlashStringHelper *user, const __FlashStringHelper *key)
45-
{
46-
_username = (const char*)user;
47-
_key = (const char*)key;
48-
49-
_init();
50-
}
51-
5280
AdafruitIO_Feed* AdafruitIO::feed(const char* name)
5381
{
5482
return new AdafruitIO_Feed(this, name);
@@ -59,22 +87,12 @@ AdafruitIO_Feed* AdafruitIO::feed(const __FlashStringHelper *name)
5987
return new AdafruitIO_Feed(this, name);
6088
}
6189

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-
7090
void AdafruitIO::_init()
7191
{
92+
7293
// we have never pinged, so set last ping to now
7394
_last_ping = millis();
7495

75-
// call child class connect
76-
_connect();
77-
7896
// dynamically allocate memory for err topic
7997
_err_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_ERROR_TOPIC) + 1));
8098

@@ -84,11 +102,6 @@ void AdafruitIO::_init()
84102
strcpy(_err_topic, _username);
85103
strcat(_err_topic, AIO_ERROR_TOPIC);
86104

87-
// setup error sub
88-
_err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic);
89-
_mqtt->subscribe(_err_sub);
90-
_err_sub->setCallback(errorCallback);
91-
92105
} else {
93106

94107
// malloc failed
@@ -105,17 +118,13 @@ void AdafruitIO::_init()
105118
strcpy(_throttle_topic, _username);
106119
strcat(_throttle_topic, AIO_THROTTLE_TOPIC);
107120

108-
// setup throttle sub
109-
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
110-
_mqtt->subscribe(_throttle_sub);
111-
_throttle_sub->setCallback(errorCallback);
112-
113121
} else {
114122

115123
// malloc failed
116124
_throttle_topic = 0;
117125

118126
}
127+
119128
}
120129

121130
const __FlashStringHelper* AdafruitIO::statusText()

AdafruitIO.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class AdafruitIO {
2727
friend class AdafruitIO_Feed;
2828

2929
public:
30-
AdafruitIO();
30+
AdafruitIO(const char *user, const char *key);
31+
AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key);
3132
virtual ~AdafruitIO();
3233

33-
void connect(const char *user, const char *key);
34-
void connect(const __FlashStringHelper *user, const __FlashStringHelper *key);
34+
void connect();
3535

3636
void run(uint16_t busywait_ms = 100);
3737

@@ -46,7 +46,6 @@ class AdafruitIO {
4646

4747
protected:
4848
virtual void _connect() = 0;
49-
5049
aio_status_t _status = AIO_IDLE;
5150
uint32_t _last_ping = 0;
5251

AdafruitIO_ESP8266.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
#include "AdafruitIO_ESP8266.h"
1111

12-
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const char *ssid, const char *pass):AdafruitIO()
12+
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const char *user, const char *key, const char *ssid, const char *pass):AdafruitIO(user, key)
1313
{
1414
_ssid = ssid;
1515
_pass = pass;
1616
_client = new WiFiClientSecure;
1717
_mqtt = new Adafruit_MQTT_Client(_client, _host, _port);
1818
}
1919

20-
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const __FlashStringHelper *ssid, const __FlashStringHelper *pass):AdafruitIO()
20+
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const __FlashStringHelper *user, const __FlashStringHelper *key, const __FlashStringHelper *ssid, const __FlashStringHelper *pass):AdafruitIO(user, key)
2121
{
2222
_ssid = (const char*)ssid;
2323
_pass = (const char*)pass;

AdafruitIO_ESP8266.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
class AdafruitIO_ESP8266 : public AdafruitIO {
2121

2222
public:
23-
AdafruitIO_ESP8266(const char *ssid, const char *pass);
24-
AdafruitIO_ESP8266(const __FlashStringHelper *ssid, const __FlashStringHelper *pass);
23+
AdafruitIO_ESP8266(const char *user, const char *key, const char *ssid, const char *pass);
24+
AdafruitIO_ESP8266(const __FlashStringHelper *user, const __FlashStringHelper *key, const __FlashStringHelper *ssid, const __FlashStringHelper *pass);
2525
~AdafruitIO_ESP8266();
2626

2727
aio_status_t networkStatus();
2828

2929
protected:
3030
void _connect();
31+
3132
const char *_ssid;
3233
const char *_pass;
3334
WiFiClientSecure *_client;

AdafruitIO_Feed.cpp

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n)
1414
name = n;
1515
_sub = 0;
1616
_pub = 0;
17+
18+
_init();
1719
}
1820

1921
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const __FlashStringHelper *n)
@@ -22,6 +24,8 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const __FlashStringHelper *n)
2224
name = (const char*)n;
2325
_sub = 0;
2426
_pub = 0;
27+
28+
_init();
2529
}
2630

2731
AdafruitIO_Feed::~AdafruitIO_Feed()
@@ -41,9 +45,6 @@ AdafruitIO_Feed::~AdafruitIO_Feed()
4145

4246
void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb)
4347
{
44-
if(! _sub)
45-
_init();
46-
4748
_dataCallback = cb;
4849
_io->_mqtt->subscribe(_sub);
4950

@@ -52,81 +53,54 @@ void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb)
5253

5354
bool AdafruitIO_Feed::save(char *value, double lat, double lon, double ele)
5455
{
55-
if(! _pub)
56-
init();
57-
5856
_data->setValue(value, lat, lon, ele);
5957
return _pub->publish(_data->toCSV());
6058
}
6159

6260
bool AdafruitIO_Feed::save(bool value, double lat, double lon, double ele)
6361
{
64-
if(! _pub)
65-
init();
66-
6762
_data->setValue(value, lat, lon, ele);
6863
return _pub->publish(_data->toCSV());
6964
}
7065

7166
bool AdafruitIO_Feed::save(String value, double lat, double lon, double ele)
7267
{
73-
if(! _pub)
74-
init();
75-
7668
_data->setValue(value, lat, lon, ele);
7769
return _pub->publish(_data->toCSV());
7870
}
7971

8072
bool AdafruitIO_Feed::save(int value, double lat, double lon, double ele)
8173
{
82-
if(! _pub)
83-
init();
84-
8574
_data->setValue(value, lat, lon, ele);
8675
return _pub->publish(_data->toCSV());
8776
}
8877

8978
bool AdafruitIO_Feed::save(unsigned int value, double lat, double lon, double ele)
9079
{
91-
if(! _pub)
92-
init();
93-
9480
_data->setValue(value, lat, lon, ele);
9581
return _pub->publish(_data->toCSV());
9682
}
9783

9884
bool AdafruitIO_Feed::save(long value, double lat, double lon, double ele)
9985
{
100-
if(! _pub)
101-
init();
102-
10386
_data->setValue(value, lat, lon, ele);
10487
return _pub->publish(_data->toCSV());
10588
}
10689

10790
bool AdafruitIO_Feed::save(unsigned long value, double lat, double lon, double ele)
10891
{
109-
if(! _pub)
110-
init();
111-
11292
_data->setValue(value, lat, lon, ele);
11393
return _pub->publish(_data->toCSV());
11494
}
11595

11696
bool AdafruitIO_Feed::save(float value, double lat, double lon, double ele, int precision)
11797
{
118-
if(! _pub)
119-
init();
120-
12198
_data->setValue(value, lat, lon, ele, precision);
12299
return _pub->publish(_data->toCSV());
123100
}
124101

125102
bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele, int precision)
126103
{
127-
if(! _pub)
128-
init();
129-
130104
_data->setValue(value, lat, lon, ele, precision);
131105
return _pub->publish(_data->toCSV());
132106
}
@@ -138,9 +112,6 @@ void AdafruitIO_Feed::setLocation(double lat, double lon, double ele)
138112

139113
void AdafruitIO_Feed::subCallback(char *val, uint16_t len)
140114
{
141-
if(! _dataCallback)
142-
return;
143-
144115
_data->setCSV(val);
145116
_dataCallback(_data);
146117
}

AdafruitIO_WiFi.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class AdafruitIO_WiFi: public AdafruitIO_WINC1500 {
2929
class AdafruitIO_WiFi: public AdafruitIO_ESP8266 {
3030

3131
public:
32-
AdafruitIO_WiFi(const char *ssid, const char *pass) :
33-
AdafruitIO_ESP8266(ssid, pass) {}
34-
AdafruitIO_WiFi(const __FlashStringHelper *ssid, const __FlashStringHelper *pass) :
35-
AdafruitIO_ESP8266(ssid, pass) {}
32+
AdafruitIO_WiFi(const char *user, const char *key, const char *ssid, const char *pass) :
33+
AdafruitIO_ESP8266(user, key, ssid, pass) {}
34+
AdafruitIO_WiFi(const __FlashStringHelper *user, const __FlashStringHelper *key, const __FlashStringHelper *ssid, const __FlashStringHelper *pass) :
35+
AdafruitIO_ESP8266(user, key, ssid, pass) {}
3636

3737
};
3838

0 commit comments

Comments
 (0)