1
+ /* **********************************************************************
2
+ Adafruit MQTT Library ESP32 Adafruit IO SSL/TLS example
3
+
4
+ Use the latest version of the ESP32 Arduino Core:
5
+ https://github.com/espressif/arduino-esp32
6
+
7
+ Works great with Adafruit Huzzah32 Feather and Breakout Board:
8
+ https://www.adafruit.com/product/3405
9
+ https://www.adafruit.com/products/4172
10
+
11
+ Adafruit invests time and resources providing this open source code,
12
+ please support Adafruit and open-source hardware by purchasing
13
+ products from Adafruit!
14
+
15
+ Written by Tony DiCola for Adafruit Industries.
16
+ Modified by Brent Rubell for Adafruit Industries
17
+ MIT license, all text above must be included in any redistribution
18
+ **********************************************************************/
19
+ #include < WiFi.h>
20
+ #include " WiFiClientSecure.h"
21
+ #include " Adafruit_MQTT.h"
22
+ #include " Adafruit_MQTT_Client.h"
23
+
24
+ /* ************************ WiFi Access Point *********************************/
25
+
26
+ #define WLAN_SSID " WLAN_SSID"
27
+ #define WLAN_PASS " WIFI_PASSWORD"
28
+
29
+ /* ************************ Adafruit.io Setup *********************************/
30
+
31
+ #define AIO_SERVER " io.adafruit.com"
32
+
33
+ // Using port 8883 for MQTTS
34
+ #define AIO_SERVERPORT 8883
35
+
36
+ // Adafruit IO Account Configuration
37
+ // (to obtain these values, visit https://io.adafruit.com and click on Active Key)
38
+ #define AIO_USERNAME " YOUR_ADAFRUIT_IO_USERNAME"
39
+ #define AIO_KEY " YOUR_ADAFRUIT_IO_KEY"
40
+
41
+ /* *********** Global State (you don't need to change this!) ******************/
42
+
43
+ // WiFiFlientSecure for SSL/TLS support
44
+ WiFiClientSecure client;
45
+
46
+ // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
47
+ Adafruit_MQTT_Client mqtt (&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
48
+
49
+ // io.adafruit.com root CA
50
+ const char * adafruitio_root_ca = \
51
+ " -----BEGIN CERTIFICATE-----\n " \
52
+ " MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHS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" \
67
+ " hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n " \
68
+ " 06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n " \
69
+ " PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n " \
70
+ " YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n " \
71
+ " CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n " \
72
+ " -----END CERTIFICATE-----\n " ;
73
+
74
+ /* ***************************** Feeds ***************************************/
75
+
76
+ // Setup a feed called 'test' for publishing.
77
+ // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
78
+ Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME " /feeds/test" );
79
+
80
+ /* ************************** Sketch Code ************************************/
81
+
82
+ void setup () {
83
+ Serial.begin (115200 );
84
+ delay (10 );
85
+
86
+ Serial.println (F (" Adafruit IO MQTTS (SSL/TLS) Example" ));
87
+
88
+ // Connect to WiFi access point.
89
+ Serial.println (); Serial.println ();
90
+ Serial.print (" Connecting to " );
91
+ Serial.println (WLAN_SSID);
92
+
93
+ delay (1000 );
94
+
95
+ WiFi.begin (WLAN_SSID, WLAN_PASS);
96
+ delay (2000 );
97
+
98
+ while (WiFi.status () != WL_CONNECTED) {
99
+ delay (500 );
100
+ Serial.print (" ." );
101
+ }
102
+ Serial.println ();
103
+
104
+ Serial.println (" WiFi connected" );
105
+ Serial.println (" IP address: " ); Serial.println (WiFi.localIP ());
106
+
107
+ // Set Adafruit IO's root CA
108
+ client.setCACert (adafruitio_root_ca);
109
+ }
110
+
111
+ uint32_t x=0 ;
112
+
113
+ void loop () {
114
+ // Ensure the connection to the MQTT server is alive (this will make the first
115
+ // connection and automatically reconnect when disconnected). See the MQTT_connect
116
+ // function definition further below.
117
+ MQTT_connect ();
118
+
119
+ // Now we can publish stuff!
120
+ Serial.print (F (" \n Sending val " ));
121
+ Serial.print (x);
122
+ Serial.print (F (" to test feed..." ));
123
+ if (! test.publish (x++)) {
124
+ Serial.println (F (" Failed" ));
125
+ } else {
126
+ Serial.println (F (" OK!" ));
127
+ }
128
+
129
+ // wait a couple seconds to avoid rate limit
130
+ delay (2000 );
131
+
132
+ }
133
+
134
+ // Function to connect and reconnect as necessary to the MQTT server.
135
+ // Should be called in the loop function and it will take care if connecting.
136
+ void MQTT_connect () {
137
+ int8_t ret;
138
+
139
+ // Stop if already connected.
140
+ if (mqtt.connected ()) {
141
+ return ;
142
+ }
143
+
144
+ Serial.print (" Connecting to MQTT... " );
145
+
146
+ uint8_t retries = 3 ;
147
+ while ((ret = mqtt.connect ()) != 0 ) { // connect will return 0 for connected
148
+ Serial.println (mqtt.connectErrorString (ret));
149
+ Serial.println (" Retrying MQTT connection in 5 seconds..." );
150
+ mqtt.disconnect ();
151
+ delay (5000 ); // wait 5 seconds
152
+ retries--;
153
+ if (retries == 0 ) {
154
+ // basically die and wait for WDT to reset me
155
+ while (1 );
156
+ }
157
+ }
158
+
159
+ Serial.println (" MQTT Connected!" );
160
+ }
0 commit comments