I'm using ESP8266Ping
lib inside my iot
lib which hanldes Wifi connectivity and MQTT messages. Since it create its own instance called Ping
when calling #include <ESP8266Ping.h>
- it had to be place in iot.cpp
.
I writing another library, IPmonitor
which checks clients on the network (using pings), I wanted to use ESP8266Ping
again.
My sketch file, create an instance of iot
and IPmonitor
for check/ log errors on monitored clients.
But I get error of using the same library twice.
I guess that since ESP8366Ping.h
library is defined it creates an instance of the class ( see mark below ):
class PingClass {
public:
PingClass();
bool ping(IPAddress dest, byte count = 5);
bool ping(const char* host, byte count = 5);
int averageTime();
protected:
static void _ping_sent_cb(void *opt, void *pdata);
static void _ping_recv_cb(void *opt, void *pdata);
IPAddress _dest;
ping_option _options;
static byte _expected_count, _errors, _success;
static int _avg_time;
};
#include "ESP8266Ping.impl.h"
PingClass Ping; // <----- This
#endif
How can it be solved ?
1 Answer 1
With that library... you can't. All the code is in one big monolithic .h file. As you well know by now that is a big no-no. The author should be given a big slap on the wrists.
The closest you could do without re-writing the whole library is to just copy the class definition from the outer .h file and create an extern
to the existing object - maybe make your own copy of the ESP8266Ping.h
file without the #include "ESP8266Ping.impl.h"
and PingClass Ping;
(the latter of which you change to an extern
).
-
Thank you. Since I don’t any future mishappens after lib update, I’ll probably use calling external function in ‘IPmonitor’ to call the relevant function from ‘iot’ class. And slappingguyd– guyd2021年04月13日 09:19:42 +00:00Commented Apr 13, 2021 at 9:19
-
@Guy.D I have submitted a pull request with the required changes. You may like to test with my fork: github.com/majenkotech/ESP8266PingMajenko– Majenko2021年04月13日 09:28:16 +00:00Commented Apr 13, 2021 at 9:28
-
Well... this is very nice of you! Cheersguyd– guyd2021年04月13日 09:32:05 +00:00Commented Apr 13, 2021 at 9:32