mirror of
https://github.com/adafruit/Adafruit_MQTT_Library.git
synced 2025-06-07 13:22:02 +03:00
166 lines
5.9 KiB
C++
166 lines
5.9 KiB
C++
/***********************************************************************
|
|
Adafruit MQTT Library ESP32 Adafruit IO SSL/TLS example
|
|
|
|
Use the latest version of the ESP32 Arduino Core:
|
|
https://github.com/espressif/arduino-esp32
|
|
|
|
Works great with Adafruit Huzzah32 Feather and Breakout Board:
|
|
https://www.adafruit.com/product/3405
|
|
https://www.adafruit.com/products/4172
|
|
|
|
Adafruit invests time and resources providing this open source code,
|
|
please support Adafruit and open-source hardware by purchasing
|
|
products from Adafruit!
|
|
|
|
Written by Tony DiCola for Adafruit Industries.
|
|
Modified by Brent Rubell for Adafruit Industries
|
|
MIT license, all text above must be included in any redistribution
|
|
**********************************************************************/
|
|
#include <WiFi.h>
|
|
#include "WiFiClientSecure.h"
|
|
#include "Adafruit_MQTT.h"
|
|
#include "Adafruit_MQTT_Client.h"
|
|
|
|
/************************* WiFi Access Point *********************************/
|
|
|
|
#define WLAN_SSID "WLAN_SSID"
|
|
#define WLAN_PASS "WIFI_PASSWORD"
|
|
|
|
/************************* Adafruit.io Setup *********************************/
|
|
|
|
#define AIO_SERVER "io.adafruit.com"
|
|
|
|
// Using port 8883 for MQTTS
|
|
#define AIO_SERVERPORT 8883
|
|
|
|
// Adafruit IO Account Configuration
|
|
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
|
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
|
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
|
|
|
/************ Global State (you don't need to change this!) ******************/
|
|
|
|
// WiFiFlientSecure for SSL/TLS support
|
|
WiFiClientSecure client;
|
|
|
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
|
|
|
// io.adafruit.com root CA
|
|
const char* adafruitio_root_ca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AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
|
|
"BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
|
|
"HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
|
|
"b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
|
|
"HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
|
|
"AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
|
|
"c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
|
|
"ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
|
|
"jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
|
|
"TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
|
|
"7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
|
|
"-----END CERTIFICATE-----\n";
|
|
|
|
/****************************** Feeds ***************************************/
|
|
|
|
// Setup a feed called 'test' for publishing.
|
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
|
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
|
|
|
/*************************** Sketch Code ************************************/
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(10);
|
|
|
|
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
|
|
|
|
// Connect to WiFi access point.
|
|
Serial.println(); Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(WLAN_SSID);
|
|
|
|
delay(1000);
|
|
|
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
|
delay(2000);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println();
|
|
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
|
|
|
// Set Adafruit IO's root CA
|
|
client.setCACert(adafruitio_root_ca);
|
|
}
|
|
|
|
uint32_t x=0;
|
|
|
|
void loop() {
|
|
// Ensure the connection to the MQTT server is alive (this will make the first
|
|
// connection and automatically reconnect when disconnected). See the MQTT_connect
|
|
// function definition further below.
|
|
MQTT_connect();
|
|
|
|
// Now we can publish stuff!
|
|
Serial.print(F("\nSending val "));
|
|
Serial.print(x);
|
|
Serial.print(F(" to test feed..."));
|
|
if (! test.publish(x++)) {
|
|
Serial.println(F("Failed"));
|
|
} else {
|
|
Serial.println(F("OK!"));
|
|
}
|
|
|
|
// wait a couple seconds to avoid rate limit
|
|
delay(2000);
|
|
|
|
}
|
|
|
|
// Function to connect and reconnect as necessary to the MQTT server.
|
|
// Should be called in the loop function and it will take care if connecting.
|
|
void MQTT_connect() {
|
|
int8_t ret;
|
|
|
|
// Stop if already connected.
|
|
if (mqtt.connected()) {
|
|
return;
|
|
}
|
|
|
|
Serial.print("Connecting to MQTT... ");
|
|
|
|
uint8_t retries = 3;
|
|
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
|
Serial.println(mqtt.connectErrorString(ret));
|
|
Serial.println("Retrying MQTT connection in 5 seconds...");
|
|
mqtt.disconnect();
|
|
delay(5000); // wait 5 seconds
|
|
retries--;
|
|
if (retries == 0) {
|
|
// basically die and wait for WDT to reset me
|
|
while (1);
|
|
}
|
|
}
|
|
|
|
Serial.println("MQTT Connected!");
|
|
}
|