|
1 | | -#include <wolfMQTT.h> |
2 | | -#include <Ethernet.h> |
| 1 | +/* Uncomment this to enable TLS support */ |
| 2 | +/* Make sure and include the wolfSSL library */ |
| 3 | +//#define ENABLE_MQTT_TLS |
| 4 | + |
| 5 | +#ifdef ENABLE_MQTT_TLS |
| 6 | +#include <config.h> |
3 | 7 | #include <wolfssl.h> |
4 | 8 | #include <wolfssl/ssl.h> |
5 | | -#include <wolfmqtt/mqtt_client.h> |
6 | | -#include <examples/mqttnet.h> |
| 9 | +#endif |
| 10 | + |
| 11 | +#include <wolfMQTT.h> |
| 12 | +#include <Ethernet.h> |
| 13 | + |
7 | 14 |
|
8 | 15 | /* Configuration */ |
| 16 | +#define DEFAULT_MQTT_HOST "iot.eclipse.org" /* broker.hivemq.com */ |
9 | 17 | #define DEFAULT_CMD_TIMEOUT_MS 30000 |
10 | 18 | #define DEFAULT_CON_TIMEOUT_MS 5000 |
11 | 19 | #define DEFAULT_MQTT_QOS MQTT_QOS_0 |
|
19 | 27 | #define TEST_TOPIC_COUNT 2 |
20 | 28 |
|
21 | 29 | /* Local Variables */ |
| 30 | +#ifdef ENABLE_MQTT_TLS |
22 | 31 | static WOLFSSL_METHOD* mMethod = 0; |
23 | 32 | static WOLFSSL_CTX* mCtx = 0; |
24 | 33 | static WOLFSSL* mSsl = 0; |
| 34 | +static const char* mTlsFile = NULL; |
| 35 | +#endif |
25 | 36 | static word16 mPort = 0; |
26 | 37 | static const char* mHost = "iot.eclipse.org"; |
27 | 38 | static int mStopRead = 0; |
28 | | -static const char* mTlsFile = NULL; |
29 | 39 |
|
30 | 40 | EthernetClient ethClient; |
31 | 41 |
|
@@ -67,6 +77,7 @@ static int EthernetDisconnect(void *context) |
67 | 77 | return 0; |
68 | 78 | } |
69 | 79 |
|
| 80 | +#ifdef ENABLE_MQTT_TLS |
70 | 81 | static int mqttclient_tls_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store) |
71 | 82 | { |
72 | 83 | char buffer[WOLFSSL_MAX_ERROR_SZ]; |
@@ -101,6 +112,7 @@ static int mqttclient_tls_cb(MqttClient* cli) |
101 | 112 |
|
102 | 113 | return rc; |
103 | 114 | } |
| 115 | +#endif /* ENABLE_MQTT_TLS */ |
104 | 116 |
|
105 | 117 | #define MAX_PACKET_ID ((1 << 16) - 1) |
106 | 118 | static int mPacketIdLast; |
@@ -168,7 +180,11 @@ void loop() { |
168 | 180 | MqttClient client; |
169 | 181 | EthernetClient ethClient; |
170 | 182 | MqttNet net; |
| 183 | +#ifdef ENABLE_MQTT_TLS |
| 184 | + int use_tls = 1; |
| 185 | +#else |
171 | 186 | int use_tls = 0; |
| 187 | +#endif |
172 | 188 | MqttQoS qos = DEFAULT_MQTT_QOS; |
173 | 189 | byte clean_session = 1; |
174 | 190 | word16 keep_alive_sec = 60; |
@@ -200,7 +216,13 @@ void loop() { |
200 | 216 |
|
201 | 217 | /* Connect to broker server socket */ |
202 | 218 | rc = MqttClient_NetConnect(&client, mHost, mPort, |
203 | | - DEFAULT_CON_TIMEOUT_MS, use_tls, mqttclient_tls_cb); |
| 219 | + DEFAULT_CON_TIMEOUT_MS, use_tls, |
| 220 | +#ifdef ENABLE_MQTT_TLS |
| 221 | + mqttclient_tls_cb |
| 222 | +#else |
| 223 | + NULL |
| 224 | +#endif |
| 225 | + ); |
204 | 226 | Serial.print("MQTT Socket Connect: "); |
205 | 227 | Serial.print(MqttClient_ReturnCodeToString(rc)); |
206 | 228 | Serial.print(" "); |
|
0 commit comments