|
6 | 6 | * products from Adafruit!
|
7 | 7 | *
|
8 | 8 | * Copyright (c) 2015-2016 Adafruit Industries
|
9 |
| - * Authors: Tony DiCola, Todd Treece |
| 9 | + * Authors: David Goldstein, Morgan Winters |
10 | 10 | * Licensed under the MIT license.
|
11 | 11 | *
|
12 | 12 | * All text above must be included in any redistribution.
|
13 | 13 | */
|
14 | 14 |
|
15 |
| -#ifndef ADAFRUITIO_MKR1010_H |
16 |
| -#define ADAFRUITIO_MKR1010_H |
17 |
| - |
18 |
| -#if defined(ARDUINO_SAMD_MKR1010) |
19 | 15 |
|
20 | 16 | #include "AdafruitIO.h"
|
21 | 17 | #include "Adafruit_MQTT.h"
|
|
28 | 24 | class AdafruitIO_MKR1010 : public AdafruitIO {
|
29 | 25 |
|
30 | 26 | public:
|
31 |
| - AdafruitIO_MKR1010(const char *user, const char *key, const char *ssid, |
32 |
| - const char *pass); |
33 |
| - ~AdafruitIO_MKR1010(); |
| 27 | + AdafruitIO_MKR1010(const char *user, const char *key, |
| 28 | + const char *ssid, const char *pass) |
| 29 | + : AdafruitIO(user, key) { |
| 30 | + _ssid = ssid; |
| 31 | + _pass = pass; |
| 32 | + _client = new WiFiSSLClient; |
| 33 | + _mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port); |
| 34 | + _http = new HttpClient(*_client, _host, _http_port); |
| 35 | +} |
| 36 | + ~AdafruitIO_MKR1010() { |
| 37 | + if (_client) |
| 38 | + delete _client; |
| 39 | + if (_mqtt) |
| 40 | + delete _mqtt; |
| 41 | + } |
34 | 42 |
|
35 |
| - aio_status_t networkStatus(); |
36 |
| - const char *connectionType(); |
| 43 | + aio_status_t networkStatus() { |
| 44 | + switch (WiFi.status()) { |
| 45 | + case WL_CONNECTED: |
| 46 | + return AIO_NET_CONNECTED; |
| 47 | + case WL_CONNECT_FAILED: |
| 48 | + return AIO_NET_CONNECT_FAILED; |
| 49 | + case WL_IDLE_STATUS: |
| 50 | + return AIO_IDLE; |
| 51 | + default: |
| 52 | + return AIO_NET_DISCONNECTED; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const char *connectionType() { return "wifi"; } |
37 | 57 |
|
38 | 58 | protected:
|
39 |
| - void _connect(); |
40 |
| - void _disconnect(); |
| 59 | + void _connect() { |
| 60 | + if (strlen(_ssid) == 0) { |
| 61 | + _status = AIO_SSID_INVALID; |
| 62 | + } else { |
| 63 | + _disconnect(); |
| 64 | + |
| 65 | + WiFi.begin(_ssid, _pass); |
| 66 | + _status = networkStatus(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void _disconnect() { |
| 71 | + WiFi.disconnect(); |
| 72 | + delay(AIO_NET_DISCONNECT_WAIT); |
| 73 | + } |
41 | 74 |
|
42 | 75 | const char *_ssid;
|
43 | 76 | const char *_pass;
|
44 | 77 |
|
45 | 78 | WiFiSSLClient *_client;
|
46 | 79 | };
|
47 | 80 |
|
48 |
| -#endif // ARDUINO_ARCH_SAMD |
49 | 81 |
|
50 |
| -#endif // ADAFRUITIO_MKR1010_H |
|
0 commit comments