16
16
#ifndef __ESP32_MQTT_H__
17
17
#define __ESP32_MQTT_H__
18
18
#include <String.h>
19
+ #include <Client.h>
19
20
#include <WiFi.h>
20
21
#include <WiFiClientSecure.h>
21
22
22
- #include <rBase64.h>
23
23
#include <MQTT.h>
24
24
25
25
#include <CloudIoTCore.h>
26
+ #include <CloudIoTCoreMqtt.h>
26
27
#include "ciotc_config.h" // Update this file with your configuration
27
28
28
- // Initialize the Genuino WiFi SSL client library / RTC
29
- WiFiClientSecure * netClient ;
30
- MQTTClient * mqttClient ;
29
+ // !!REPLACEME!!
30
+ // The MQTT callback function for commands and configuration updates
31
+ // Place your message handler code here.
32
+ void messageReceived (String & topic , String & payload ) {
33
+ Serial .println ("incoming: " + topic + " - " + payload );
34
+ }
35
+ ///////////////////////////////
31
36
32
- // Clout IoT configuration that you don't need to change
37
+ // Initialize WiFi and MQTT for this board
38
+ Client * netClient ;
33
39
CloudIoTCoreDevice * device ;
40
+ CloudIoTCoreMqtt * mqtt ;
41
+ MQTTClient * mqttClient ;
34
42
unsigned long iss = 0 ;
35
43
String jwt ;
36
44
@@ -70,159 +78,24 @@ void setupWifi() {
70
78
}
71
79
}
72
80
73
-
74
- ///////////////////////////////
75
- // MQTT common functions
76
- ///////////////////////////////
77
- void messageReceived (String & topic , String & payload ) {
78
- Serial .println ("incoming: " + topic + " - " + payload );
79
- }
80
-
81
- void startMQTT () {
82
- mqttClient -> begin ("mqtt.googleapis.com" , 8883 , * netClient );
83
- mqttClient -> onMessage (messageReceived );
84
- }
85
-
86
- void publishTelemetry (String data ) {
87
- mqttClient -> publish (device -> getEventsTopic (), data );
88
- }
89
-
90
- // Helper that just sends default sensor
91
- void publishState (String data ) {
92
- mqttClient -> publish (device -> getStateTopic (), data );
93
- }
94
-
95
- // FIXME: Move to config?
96
- int __backoff__ = 1000 ; // current backoff, milliseconds
97
- int __factor__ = 2.5f ;
98
- int __minbackoff__ = 1000 ; // minimum backoff, ms
99
- int __max_backoff__ = 60000 ; // maximum backoff, ms
100
- int __jitter__ = 500 ; // max random jitter, ms
101
- void mqttConnect () {
102
- Serial .print ("\nconnecting..." );
103
- bool keepgoing = true;
104
- while (keepgoing ) {
105
- mqttClient -> connect (device -> getClientId ().c_str (), "unused" , getJwt ().c_str (), false);
106
-
107
- if (mqttClient -> lastError () != LWMQTT_SUCCESS ){
108
- Serial .println (mqttClient -> lastError ());
109
- switch (mqttClient -> lastError ()) {
110
- case (LWMQTT_BUFFER_TOO_SHORT ):
111
- Serial .println ("LWMQTT_BUFFER_TOO_SHORT" );
112
- break ;
113
- case (LWMQTT_VARNUM_OVERFLOW ):
114
- Serial .println ("LWMQTT_VARNUM_OVERFLOW" );
115
- break ;
116
- case (LWMQTT_NETWORK_FAILED_CONNECT ):
117
- Serial .println ("LWMQTT_NETWORK_FAILED_CONNECT" );
118
- break ;
119
- case (LWMQTT_NETWORK_TIMEOUT ):
120
- Serial .println ("LWMQTT_NETWORK_TIMEOUT" );
121
- break ;
122
- case (LWMQTT_NETWORK_FAILED_READ ):
123
- Serial .println ("LWMQTT_NETWORK_FAILED_READ" );
124
- break ;
125
- case (LWMQTT_NETWORK_FAILED_WRITE ):
126
- Serial .println ("LWMQTT_NETWORK_FAILED_WRITE" );
127
- break ;
128
- case (LWMQTT_REMAINING_LENGTH_OVERFLOW ):
129
- Serial .println ("LWMQTT_REMAINING_LENGTH_OVERFLOW" );
130
- break ;
131
- case (LWMQTT_REMAINING_LENGTH_MISMATCH ):
132
- Serial .println ("LWMQTT_REMAINING_LENGTH_MISMATCH" );
133
- break ;
134
- case (LWMQTT_MISSING_OR_WRONG_PACKET ):
135
- Serial .println ("LWMQTT_MISSING_OR_WRONG_PACKET" );
136
- break ;
137
- case (LWMQTT_CONNECTION_DENIED ):
138
- Serial .println ("LWMQTT_CONNECTION_DENIED" );
139
- break ;
140
- case (LWMQTT_FAILED_SUBSCRIPTION ):
141
- Serial .println ("LWMQTT_FAILED_SUBSCRIPTION" );
142
- break ;
143
- case (LWMQTT_SUBACK_ARRAY_OVERFLOW ):
144
- Serial .println ("LWMQTT_SUBACK_ARRAY_OVERFLOW" );
145
- break ;
146
- case (LWMQTT_PONG_TIMEOUT ):
147
- Serial .println ("LWMQTT_PONG_TIMEOUT" );
148
- break ;
149
- default :
150
- Serial .println ("This error code should never be reached." );
151
- break ;
152
- }
153
-
154
- Serial .println (mqttClient -> returnCode ());
155
- switch (mqttClient -> returnCode ()) {
156
- case (LWMQTT_CONNECTION_ACCEPTED ):
157
- Serial .println ("OK" );
158
- break ;
159
- case (LWMQTT_UNACCEPTABLE_PROTOCOL ):
160
- Serial .println ("LWMQTT_UNACCEPTABLE_PROTOCOLL" );
161
- break ;
162
- case (LWMQTT_IDENTIFIER_REJECTED ):
163
- Serial .println ("LWMQTT_IDENTIFIER_REJECTED" );
164
- break ;
165
- case (LWMQTT_SERVER_UNAVAILABLE ):
166
- Serial .println ("LWMQTT_SERVER_UNAVAILABLE" );
167
- break ;
168
- case (LWMQTT_BAD_USERNAME_OR_PASSWORD ):
169
- Serial .println ("LWMQTT_BAD_USERNAME_OR_PASSWORD" );
170
- iss = 0 ; // Force JWT regeneration
171
- break ;
172
- case (LWMQTT_NOT_AUTHORIZED ):
173
- Serial .println ("LWMQTT_NOT_AUTHORIZED" );
174
- iss = 0 ; // Force JWT regeneration
175
- break ;
176
- case (LWMQTT_UNKNOWN_RETURN_CODE ):
177
- Serial .println ("LWMQTT_UNKNOWN_RETURN_CODE" );
178
- break ;
179
- default :
180
- Serial .println ("This return code should never be reached." );
181
- break ;
182
- }
183
- // See https://cloud.google.com/iot/docs/how-tos/exponential-backoff
184
- if (__backoff__ < __minbackoff__ ) {
185
- __backoff__ = __minbackoff__ ;
186
- }
187
- __backoff__ = (__backoff__ * __factor__ ) + random (__jitter__ );
188
- if (__backoff__ > __max_backoff__ ) {
189
- __backoff__ = __max_backoff__ ;
190
- }
191
-
192
- // Clean up the client
193
- mqttClient -> disconnect ();
194
- Serial .println ("Delaying " + String (__backoff__ ) + "ms" );
195
- delay (__backoff__ );
196
- keepgoing = true;
197
- } else {
198
- // We're now connected
199
- Serial .println ("\nconnected!" );
200
- keepgoing = false;
201
- __backoff__ = __minbackoff__ ;
202
- }
203
- }
204
-
205
- mqttClient -> subscribe (device -> getConfigTopic (), 1 ); // Set QoS to 1 (ack) for configuration messages
206
- mqttClient -> subscribe (device -> getCommandsTopic (), 0 ); // QoS 0 (no ack) for commands
207
- if (ex_num_topics > 0 ) { // Subscribe to the extra topics
208
- for (int i = 0 ; i < ex_num_topics ; i ++ ) {
209
- mqttClient -> subscribe (ex_topics [i ], 0 ); // QoS 0 (no ack) for commands
210
- }
81
+ void connectWifi () {
82
+ Serial .print ("checking wifi..." );
83
+ while (WiFi .status () != WL_CONNECTED ) {
84
+ Serial .print ("." );
85
+ delay (1000 );
211
86
}
212
-
213
- publishState ("connected" );
214
87
}
215
88
216
89
///////////////////////////////
217
90
// Orchestrates various methods from preceeding code.
218
91
///////////////////////////////
92
+ void publishTelemetry (String data ) {
93
+ mqtt -> publishTelemetry (data );
94
+ }
95
+
219
96
void connect () {
220
- Serial .print ("checking wifi..." );
221
- while (WiFi .status () != WL_CONNECTED ) {
222
- Serial .print ("." );
223
- delay (1000 );
224
- }
225
- mqttConnect ();
97
+ connectWifi ();
98
+ mqtt -> mqttConnect ();
226
99
}
227
100
228
101
void setupCloudIoT () {
@@ -234,6 +107,7 @@ void setupCloudIoT() {
234
107
netClient = new WiFiClientSecure ();
235
108
mqttClient = new MQTTClient (512 );
236
109
mqttClient -> setOptions (180 , true, 1000 ); // keepAlive, cleanSession, timeout
237
- startMQTT ();
110
+ mqtt = new CloudIoTCoreMqtt (mqttClient , netClient , device );
111
+ mqtt -> startMQTT ();
238
112
}
239
113
#endif //__ESP32_MQTT_H__
0 commit comments