|
9 | 9 | //
|
10 | 10 | // All text above must be included in any redistribution.
|
11 | 11 | //
|
12 |
| -#ifndef ADAFRUITIO_Ethernet_H |
13 |
| -#define ADAFRUITIO_Ethernet_H |
14 |
| - |
| 12 | +#ifndef ADAFRUITIO_ETHERNET_H |
| 13 | +#define ADAFRUITIO_ETHERNET_H |
15 | 14 |
|
16 | 15 | #include "Arduino.h"
|
17 | 16 | #include <SPI.h>
|
18 | 17 | #include "Adafruit_MQTT.h"
|
19 | 18 | #include "Adafruit_MQTT_Client.h"
|
20 | 19 |
|
21 |
| -#if __has_include("Ethernet2.h") |
22 |
| - #include <Ethernet2.h> |
23 |
| -#else |
24 |
| - #include <Ethernet.h> |
25 |
| -#endif |
26 |
| - |
| 20 | +#include <Ethernet2.h> |
27 | 21 | #include <EthernetClient.h>
|
28 | 22 | #include <Dns.h>
|
29 | 23 | #include <Dhcp.h>
|
30 | 24 |
|
31 | 25 | #include "AdafruitIO.h"
|
32 | 26 |
|
| 27 | +// all logic in .h to avoid auto compile |
33 | 28 | class AdafruitIO_Ethernet : public AdafruitIO {
|
34 | 29 |
|
35 | 30 | public:
|
36 |
| - AdafruitIO_Ethernet(const char *user, const char *key); |
37 |
| - AdafruitIO_Ethernet(const __FlashStringHelper *user, const __FlashStringHelper *key); |
| 31 | + AdafruitIO_Ethernet(const char *user, const char *key):AdafruitIO(user, key) |
| 32 | + { |
| 33 | + _client = new EthernetClient(); |
| 34 | + _mqtt = new Adafruit_MQTT_Client(_client, _host, _port); |
| 35 | + } |
38 | 36 |
|
39 |
| - aio_status_t networkStatus(); |
| 37 | + AdafruitIO_Ethernet(const __FlashStringHelper *user, const __FlashStringHelper *key):AdafruitIO(user, key) |
| 38 | + { |
| 39 | + _client = new EthernetClient(); |
| 40 | + _mqtt = new Adafruit_MQTT_Client(_client, _host, _port); |
| 41 | + } |
40 | 42 |
|
41 |
| - protected: |
42 |
| - void _connect(); |
| 43 | + aio_status_t networkStatus() |
| 44 | + { |
| 45 | + if(_status == AIO_NET_CONNECTED) |
| 46 | + return _status; |
43 | 47 |
|
| 48 | + _connect(); |
| 49 | + return _status; |
| 50 | + } |
| 51 | + |
| 52 | + protected: |
44 | 53 | byte _mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
45 | 54 | uint16_t _port = 1883;
|
46 | 55 |
|
47 | 56 | EthernetClient *_client;
|
48 | 57 |
|
| 58 | + void _connect() |
| 59 | + { |
| 60 | + if(Ethernet.begin(_mac) == 0) |
| 61 | + _status = AIO_NET_DISCONNECTED; |
| 62 | + else |
| 63 | + _status = AIO_NET_CONNECTED; |
| 64 | + } |
| 65 | + |
49 | 66 | };
|
50 | 67 |
|
51 |
| -#endif // ADAFRUITIO_Ethernet_H |
| 68 | +#endif // ADAFRUITIO_ETHERNET_H |
0 commit comments