|
| 1 | +/*! |
| 2 | + * @file AdafruitIO_RP2040.h |
| 3 | + * |
| 4 | + * |
| 5 | + * Adafruit invests time and resources providing this open source code, |
| 6 | + * please support Adafruit and open-source hardware by purchasing |
| 7 | + * products from Adafruit! |
| 8 | + * |
| 9 | + * Copyright 2024 Brent Rubell for Adafruit Industries. |
| 10 | + * |
| 11 | + * MIT license, all text here must be included in any redistribution. |
| 12 | + * |
| 13 | + */ |
| 14 | +#ifndef ADAFRUITIO_RP2040_H |
| 15 | +#define ADAFRUITIO_RP2040_H |
| 16 | + |
| 17 | +#ifdef ARDUINO_ARCH_RP2040 |
| 18 | + |
| 19 | +#include "AdafruitIO.h" |
| 20 | +#include "Adafruit_MQTT.h" |
| 21 | +#include "Adafruit_MQTT_Client.h" |
| 22 | +#include "Arduino.h" |
| 23 | +#include <WiFiClientSecure.h> |
| 24 | + |
| 25 | +/****************************************************************************/ |
| 26 | +/*! |
| 27 | + @brief Class that stores functions for interacting with the RP2040 |
| 28 | + WiFi Client |
| 29 | +*/ |
| 30 | +/****************************************************************************/ |
| 31 | +class AdafruitIO_RP2040 : public AdafruitIO { |
| 32 | + |
| 33 | +public: |
| 34 | + /**************************************************************************/ |
| 35 | + /*! |
| 36 | + @brief Initializes the Adafruit IO class for RP2040 boards. |
| 37 | + @param user |
| 38 | + A reference to the Adafruit IO user, shared by AdafruitIO. |
| 39 | + @param key |
| 40 | + A reference to the Adafruit IO Key, shared by AdafruitIO. |
| 41 | + @param ssid |
| 42 | + A reference to the WiFi network SSID. |
| 43 | + @param pass |
| 44 | + A reference to the WiFi network password. |
| 45 | + */ |
| 46 | + /**************************************************************************/ |
| 47 | + AdafruitIO_RP2040(const char *user, const char *key, const char *ssid, |
| 48 | + const char *pass) |
| 49 | + : AdafruitIO(user, key) { |
| 50 | + _ssid = ssid; |
| 51 | + _pass = pass; |
| 52 | + _mqtt_client = new WiFiClientSecure; |
| 53 | + _mqtt = new Adafruit_MQTT_Client(_mqtt_client, _host, _mqtt_port); |
| 54 | + _http_client = new WiFiClientSecure; |
| 55 | + _http = new HttpClient(*_http_client, _host, _http_port); |
| 56 | + } |
| 57 | + |
| 58 | + /*******************************/ |
| 59 | + /*! |
| 60 | + @brief Class dtor |
| 61 | + */ |
| 62 | + /*******************************/ |
| 63 | + ~AdafruitIO_RP2040() { |
| 64 | + if (_mqtt_client) |
| 65 | + delete _http_client; |
| 66 | + if (_http_client) |
| 67 | + delete _mqtt_client; |
| 68 | + } |
| 69 | + |
| 70 | + /********************************************************/ |
| 71 | + /*! |
| 72 | + @brief Returns the network status of the RP2040. |
| 73 | + @return aio_status_t |
| 74 | + */ |
| 75 | + /********************************************************/ |
| 76 | + aio_status_t networkStatus() { |
| 77 | + switch (WiFi.status()) { |
| 78 | + case WL_CONNECTED: |
| 79 | + return AIO_NET_CONNECTED; |
| 80 | + case WL_CONNECT_FAILED: |
| 81 | + return AIO_NET_CONNECT_FAILED; |
| 82 | + case WL_IDLE_STATUS: |
| 83 | + return AIO_IDLE; |
| 84 | + default: |
| 85 | + return AIO_NET_DISCONNECTED; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /******************************************************************/ |
| 90 | + /*! |
| 91 | + @brief Returns the type of network connection used by AdafruitIO. |
| 92 | + @return RP2040 |
| 93 | + */ |
| 94 | + /******************************************************************/ |
| 95 | + const char *connectionType() { return "RP2040"; } |
| 96 | + |
| 97 | +protected: |
| 98 | + const char *_ssid; ///< WiFi network SSID |
| 99 | + const char *_pass; ///< WiFi network password |
| 100 | + |
| 101 | + WiFiClientSecure *_http_client; ///< HTTP client |
| 102 | + WiFiClientSecure *_mqtt_client; ///< MQTT client |
| 103 | + |
| 104 | + const char *_aio_root_ca_prod = |
| 105 | + "-----BEGIN CERTIFICATE-----\n" |
| 106 | + "MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n" |
| 107 | + "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNl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L29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n" |
| 122 | + "HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n" |
| 123 | + "b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n" |
| 124 | + "HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n" |
| 125 | + "AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n" |
| 126 | + "c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n" |
| 127 | + "ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n" |
| 128 | + "jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n" |
| 129 | + "TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n" |
| 130 | + "7h4SeM6Y8l/7MBRpPCz6l8Y=\n" |
| 131 | + "-----END CERTIFICATE-----\n"; ///< Root certificate for io.adafruit.com |
| 132 | + |
| 133 | + /**************************************************************************/ |
| 134 | + /*! |
| 135 | + @brief Attempts to establish a WiFi connection with the wireless network, |
| 136 | + given _ssid and _pass from the AdafruitIO_RP2040 constructor. |
| 137 | + */ |
| 138 | + /**************************************************************************/ |
| 139 | + void _connect() { |
| 140 | + if (strlen(_ssid) == 0) { |
| 141 | + Serial.println("Invalid SSID!"); |
| 142 | + _status = AIO_SSID_INVALID; |
| 143 | + } else { |
| 144 | + _disconnect(); |
| 145 | + delay(10000); |
| 146 | + WiFi.mode(WIFI_STA); |
| 147 | + WiFi.setTimeout(20000); |
| 148 | + WiFi.begin(_ssid, _pass); |
| 149 | + Serial.println("\nConnecting"); |
| 150 | + _status = AIO_NET_DISCONNECTED; |
| 151 | + } |
| 152 | + _mqtt_client->setCACert(_aio_root_ca_prod); |
| 153 | + } |
| 154 | + |
| 155 | + /**************************************************************************/ |
| 156 | + /*! |
| 157 | + @brief Disconnects from the wifi network. |
| 158 | + */ |
| 159 | + /**************************************************************************/ |
| 160 | + void _disconnect() { |
| 161 | + WiFi.disconnect(); |
| 162 | + delay(AIO_NET_DISCONNECT_WAIT); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +#endif // ADAFRUITIO_RP2040_H |
| 167 | +#endif // ARDUINO_ARCH_RP2040 |
0 commit comments