|
| 1 | +// |
| 2 | +// Adafruit invests time and resources providing this open source code. |
| 3 | +// Please support Adafruit and open source hardware by purchasing |
| 4 | +// products from Adafruit! |
| 5 | +// |
| 6 | +// Copyright (c) 2015-2016 Adafruit Industries |
| 7 | +// Authors: Tony DiCola, Todd Treece |
| 8 | +// Licensed under the MIT license. |
| 9 | +// |
| 10 | +// All text above must be included in any redistribution. |
| 11 | +// |
| 12 | +#include "AdafruitIO.h" |
| 13 | + |
| 14 | +AdafruitIO::AdafruitIO(const char *user, const char *key) |
| 15 | +{ |
| 16 | + _mqtt = 0; |
| 17 | + _username = user; |
| 18 | + _key = key; |
| 19 | + _err_topic = 0; |
| 20 | + _throttle_topic = 0; |
| 21 | + _err_sub = 0; |
| 22 | + _throttle_sub = 0; |
| 23 | + _packetread_timeout = 100; |
| 24 | + |
| 25 | + _init(); |
| 26 | +} |
| 27 | + |
| 28 | +AdafruitIO::AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key) |
| 29 | +{ |
| 30 | + _mqtt = 0; |
| 31 | + _username = (const char*)user; |
| 32 | + _key = (const char*)key; |
| 33 | + _err_topic = 0; |
| 34 | + _throttle_topic = 0; |
| 35 | + _err_sub = 0; |
| 36 | + _throttle_sub = 0; |
| 37 | + _packetread_timeout = 100; |
| 38 | + |
| 39 | + _init(); |
| 40 | +} |
| 41 | + |
| 42 | +void errorCallback(char *err, uint16_t len) |
| 43 | +{ |
| 44 | + AIO_ERR_PRINTLN(); |
| 45 | + AIO_ERR_PRINT("ERROR: "); |
| 46 | + AIO_ERR_PRINTLN(err); |
| 47 | + AIO_ERR_PRINTLN(); |
| 48 | +} |
| 49 | + |
| 50 | +void AdafruitIO::connect() |
| 51 | +{ |
| 52 | + |
| 53 | + if(_err_sub) { |
| 54 | + // setup error sub |
| 55 | + _err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic); |
| 56 | + _mqtt->subscribe(_err_sub); |
| 57 | + _err_sub->setCallback(errorCallback); |
| 58 | + } |
| 59 | + |
| 60 | + if(_throttle_sub) { |
| 61 | + // setup throttle sub |
| 62 | + _throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic); |
| 63 | + _mqtt->subscribe(_throttle_sub); |
| 64 | + _throttle_sub->setCallback(errorCallback); |
| 65 | + } |
| 66 | + |
| 67 | + _connect(); |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +AdafruitIO::~AdafruitIO() |
| 72 | +{ |
| 73 | + if(_err_topic) |
| 74 | + free(_err_topic); |
| 75 | + |
| 76 | + if(_throttle_topic) |
| 77 | + free(_throttle_topic); |
| 78 | + |
| 79 | + if(_err_sub) |
| 80 | + delete _err_sub; |
| 81 | + |
| 82 | + if(_throttle_sub) |
| 83 | + delete _throttle_sub; |
| 84 | +} |
| 85 | + |
| 86 | +AdafruitIO_Feed* AdafruitIO::feed(const char* name) |
| 87 | +{ |
| 88 | + return new AdafruitIO_Feed(this, name); |
| 89 | +} |
| 90 | + |
| 91 | +AdafruitIO_Feed* AdafruitIO::feed(const __FlashStringHelper *name) |
| 92 | +{ |
| 93 | + return new AdafruitIO_Feed(this, name); |
| 94 | +} |
| 95 | + |
| 96 | +void AdafruitIO::_init() |
| 97 | +{ |
| 98 | + |
| 99 | + // we have never pinged, so set last ping to now |
| 100 | + _last_ping = millis(); |
| 101 | + |
| 102 | + // dynamically allocate memory for err topic |
| 103 | + _err_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_ERROR_TOPIC) + 1)); |
| 104 | + |
| 105 | + if(_err_topic) { |
| 106 | + |
| 107 | + // build error topic |
| 108 | + strcpy(_err_topic, _username); |
| 109 | + strcat(_err_topic, AIO_ERROR_TOPIC); |
| 110 | + |
| 111 | + } else { |
| 112 | + |
| 113 | + // malloc failed |
| 114 | + _err_topic = 0; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + // dynamically allocate memory for throttle topic |
| 119 | + _throttle_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_THROTTLE_TOPIC) + 1)); |
| 120 | + |
| 121 | + if(_throttle_topic) { |
| 122 | + |
| 123 | + // build throttle topic |
| 124 | + strcpy(_throttle_topic, _username); |
| 125 | + strcat(_throttle_topic, AIO_THROTTLE_TOPIC); |
| 126 | + |
| 127 | + } else { |
| 128 | + |
| 129 | + // malloc failed |
| 130 | + _throttle_topic = 0; |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +const __FlashStringHelper* AdafruitIO::statusText() |
| 137 | +{ |
| 138 | + switch(_status) { |
| 139 | + |
| 140 | + // CONNECTING |
| 141 | + case AIO_IDLE: return F("Idle. Waiting for connect to be called..."); |
| 142 | + case AIO_NET_DISCONNECTED: return F("Network disconnected."); |
| 143 | + case AIO_DISCONNECTED: return F("Disconnected from Adafruit IO."); |
| 144 | + |
| 145 | + // FAILURE |
| 146 | + case AIO_NET_CONNECT_FAILED: return F("Network connection failed."); |
| 147 | + case AIO_CONNECT_FAILED: return F("Adafruit IO connection failed."); |
| 148 | + case AIO_FINGERPRINT_INVALID: return F("Adafruit IO SSL fingerprint verification failed."); |
| 149 | + case AIO_AUTH_FAILED: return F("Adafruit IO authentication failed."); |
| 150 | + |
| 151 | + // SUCCESS |
| 152 | + case AIO_NET_CONNECTED: return F("Network connected."); |
| 153 | + case AIO_CONNECTED: return F("Adafruit IO connected."); |
| 154 | + case AIO_CONNECTED_INSECURE: return F("Adafruit IO connected. **THIS CONNECTION IS INSECURE** SSL/TLS not supported for this platform."); |
| 155 | + case AIO_FINGERPRINT_UNSUPPORTED: return F("Adafruit IO connected over SSL/TLS. Fingerprint verification unsupported."); |
| 156 | + case AIO_FINGERPRINT_VALID: return F("Adafruit IO connected over SSL/TLS. Fingerprint valid."); |
| 157 | + |
| 158 | + default: return F("Unknown status code"); |
| 159 | + |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void AdafruitIO::run(uint16_t busywait_ms) |
| 164 | +{ |
| 165 | + // loop until we have a connection |
| 166 | + while(mqttStatus() != AIO_CONNECTED){} |
| 167 | + |
| 168 | + if(busywait_ms > 0) |
| 169 | + _packetread_timeout = busywait_ms; |
| 170 | + |
| 171 | + _mqtt->processPackets(_packetread_timeout); |
| 172 | + |
| 173 | + // ping to keep connection alive if needed |
| 174 | + if(millis() > (_last_ping + AIO_PING_INTERVAL)) { |
| 175 | + _mqtt->ping(); |
| 176 | + _last_ping = millis(); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +aio_status_t AdafruitIO::status() |
| 181 | +{ |
| 182 | + aio_status_t net_status = networkStatus(); |
| 183 | + |
| 184 | + // if we aren't connected, return network status |
| 185 | + if(net_status != AIO_NET_CONNECTED) { |
| 186 | + _status = net_status; |
| 187 | + return _status; |
| 188 | + } |
| 189 | + |
| 190 | + // check mqtt status and return |
| 191 | + _status = mqttStatus(); |
| 192 | + return _status; |
| 193 | +} |
| 194 | + |
| 195 | +aio_status_t AdafruitIO::mqttStatus() |
| 196 | +{ |
| 197 | + // if the connection failed, |
| 198 | + // return so we don't hammer IO |
| 199 | + if(_status == AIO_CONNECT_FAILED) |
| 200 | + return _status; |
| 201 | + |
| 202 | + if(_mqtt->connected()) |
| 203 | + return AIO_CONNECTED; |
| 204 | + |
| 205 | + switch(_mqtt->connect(_username, _key)) { |
| 206 | + case 0: |
| 207 | + return AIO_CONNECTED; |
| 208 | + case 1: // invalid mqtt protocol |
| 209 | + case 2: // client id rejected |
| 210 | + case 4: // malformed user/pass |
| 211 | + case 5: // unauthorized |
| 212 | + case 7: // banned |
| 213 | + return AIO_CONNECT_FAILED; |
| 214 | + case 3: // mqtt service unavailable |
| 215 | + case 6: // throttled |
| 216 | + // delay to prevent fast reconnects |
| 217 | + delay(AIO_THROTTLE_RECONNECT_INTERVAL); |
| 218 | + return AIO_DISCONNECTED; |
| 219 | + default: |
| 220 | + return AIO_DISCONNECTED; |
| 221 | + } |
| 222 | +} |
0 commit comments