29
29
#include < PubSubClient.h>
30
30
31
31
// Update these with values suitable for your network.
32
- const char * mqttServer = " broker.hivemq.com" ; // Broker address
32
+ // const char* mqttServer = "broker.example"; // Broker address
33
+ const char * mqttServer = " broker.emqx.io" ; // Broker address
34
+ // const char* mqttServer = "broker.shiftr.io"; // Broker address
35
+
36
+ const char *ID = " MQTTClient_SSL-Client" ; // Name of our device, must be unique
37
+ const char *TOPIC = " MQTT_Pub" ; // Topic to subcribe to
38
+ const char *subTopic = " MQTT_Sub" ; // Topic to subcribe to
39
+
33
40
// IPAddress mqttServer(172, 16, 0, 2);
34
41
35
42
void callback (char * topic, byte* payload, unsigned int length)
@@ -49,6 +56,49 @@ void callback(char* topic, byte* payload, unsigned int length)
49
56
EthernetClient ethClient;
50
57
PubSubClient client (mqttServer, 1883 , callback, ethClient);
51
58
59
+ void reconnect ()
60
+ {
61
+ // Loop until we're reconnected
62
+ while (!client.connected ())
63
+ {
64
+ Serial.print (" Attempting MQTT connection to " );
65
+ Serial.print (mqttServer);
66
+
67
+ // Attempt to connect
68
+ if (client.connect (" arduino" , " try" , " try" ))
69
+ {
70
+ Serial.println (" ...connected" );
71
+
72
+ // Once connected, publish an announcement...
73
+ String data = " Hello from MQTTClient_SSL on " + String (BOARD_NAME);
74
+
75
+ client.publish (TOPIC, data.c_str ());
76
+
77
+ // Serial.println("Published connection message successfully!");
78
+ // Serial.print("Subcribed to: ");
79
+ // Serial.println(subTopic);
80
+
81
+ // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
82
+ // ethClientSSL.flush();
83
+ // ... and resubscribe
84
+ client.subscribe (subTopic);
85
+ // for loopback testing
86
+ client.subscribe (TOPIC);
87
+ // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
88
+ // ethClientSSL.flush();
89
+ }
90
+ else
91
+ {
92
+ Serial.print (" ...failed, rc=" );
93
+ Serial.print (client.state ());
94
+ Serial.println (" try again in 5 seconds" );
95
+
96
+ // Wait 5 seconds before retrying
97
+ delay (5000 );
98
+ }
99
+ }
100
+ }
101
+
52
102
void setup ()
53
103
{
54
104
// Open serial communications and wait for port to open:
@@ -58,7 +108,25 @@ void setup()
58
108
Serial.print (" \n Start MQTTClient_Auth on " + String (BOARD_NAME));
59
109
Serial.println (" with " + String (SHIELD_TYPE));
60
110
61
- ET_LOGWARN3 (F (" Board :" ), BOARD_NAME, F (" , setCsPin:" ), USE_THIS_SS_PIN);
111
+ #if USE_ETHERNET_WRAPPER
112
+
113
+ EthernetInit ();
114
+
115
+ #else
116
+
117
+ #if USE_ETHERNET
118
+ ET_LOGWARN (F (" =========== USE_ETHERNET ===========" ));
119
+ #elif USE_ETHERNET2
120
+ ET_LOGWARN (F (" =========== USE_ETHERNET2 ===========" ));
121
+ #elif USE_ETHERNET3
122
+ ET_LOGWARN (F (" =========== USE_ETHERNET3 ===========" ));
123
+ #elif USE_ETHERNET_LARGE
124
+ ET_LOGWARN (F (" =========== USE_ETHERNET_LARGE ===========" ));
125
+ #elif USE_ETHERNET_ESP8266
126
+ ET_LOGWARN (F (" =========== USE_ETHERNET_ESP8266 ===========" ));
127
+ #else
128
+ ET_LOGWARN (F (" =========================" ));
129
+ #endif
62
130
63
131
ET_LOGWARN (F (" Default SPI pinout:" ));
64
132
ET_LOGWARN1 (F (" MOSI:" ), MOSI);
@@ -67,10 +135,58 @@ void setup()
67
135
ET_LOGWARN1 (F (" SS:" ), SS);
68
136
ET_LOGWARN (F (" =========================" ));
69
137
70
- #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
138
+ #if defined(ESP8266)
139
+ // For ESP8266, change for other boards if necessary
140
+ #ifndef USE_THIS_SS_PIN
141
+ #define USE_THIS_SS_PIN D2 // For ESP8266
142
+ #endif
143
+
144
+ ET_LOGWARN1 (F (" ESP8266 setCsPin:" ), USE_THIS_SS_PIN);
145
+
146
+ #if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
147
+ // For ESP8266
148
+ // Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8
149
+ // Ethernet 0 X X X X 0
150
+ // Ethernet2 X X X X X 0
151
+ // Ethernet3 X X X X X 0
152
+ // EthernetLarge X X X X X 0
153
+ // Ethernet_ESP8266 0 0 0 0 0 0
154
+ // D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs
155
+ // Must use library patch for Ethernet, EthernetLarge libraries
156
+ Ethernet.init (USE_THIS_SS_PIN);
157
+
158
+ #elif USE_ETHERNET3
159
+ // Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
160
+ #ifndef ETHERNET3_MAX_SOCK_NUM
161
+ #define ETHERNET3_MAX_SOCK_NUM 4
162
+ #endif
163
+
164
+ Ethernet.setCsPin (USE_THIS_SS_PIN);
165
+ Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
166
+
167
+ #elif USE_CUSTOM_ETHERNET
168
+
169
+ // You have to add initialization for your Custom Ethernet here
170
+ // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
171
+ Ethernet.init (USE_THIS_SS_PIN);
172
+
173
+ #endif // ( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET3 || USE_ETHERNET_LARGE )
174
+
175
+ #elif defined(ESP32)
176
+
177
+ #ifndef USE_THIS_SS_PIN
178
+ #define USE_THIS_SS_PIN 22 // For ESP32
179
+ #endif
180
+
181
+ ET_LOGWARN1 (F (" ESP32 setCsPin:" ), USE_THIS_SS_PIN);
182
+
71
183
// For other boards, to change if necessary
72
- #if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
73
- // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
184
+ #if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
185
+ // Must use library patch for Ethernet, EthernetLarge libraries
186
+ // ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
187
+ // ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
188
+
189
+ // Ethernet.setCsPin (USE_THIS_SS_PIN);
74
190
Ethernet.init (USE_THIS_SS_PIN);
75
191
76
192
#elif USE_ETHERNET3
@@ -81,39 +197,126 @@ void setup()
81
197
82
198
Ethernet.setCsPin (USE_THIS_SS_PIN);
83
199
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
200
+
201
+ #elif USE_CUSTOM_ETHERNET
202
+
203
+ // You have to add initialization for your Custom Ethernet here
204
+ // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
205
+ Ethernet.init (USE_THIS_SS_PIN);
206
+
207
+ #endif // ( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET3 || USE_ETHERNET_LARGE )
208
+
209
+ #else // defined(ESP8266)
210
+ // unknown board, do nothing, use default SS = 10
211
+ #ifndef USE_THIS_SS_PIN
212
+ // Select USE_THIS_SS_PIN as follows
213
+ // 10 // Most Arduino shields
214
+ // 5 // MKR ETH shield
215
+ // 0 // Teensy 2.0
216
+ // 20 // Teensy++ 2.0
217
+ // 15 // ESP8266 with Adafruit Featherwing Ethernet
218
+ // 33 // ESP32 with Adafruit Featherwing Ethernet
219
+ #define USE_THIS_SS_PIN 10 // For other boards
220
+ #endif
84
221
222
+ ET_LOGWARN3 (F (" Board :" ), BOARD_NAME, F (" , setCsPin:" ), USE_THIS_SS_PIN);
223
+
224
+ // For other boards, to change if necessary
225
+ #if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
226
+ // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
227
+
228
+ Ethernet.init (USE_THIS_SS_PIN);
229
+
230
+ #elif USE_ETHERNET3
231
+ // Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
232
+ #ifndef ETHERNET3_MAX_SOCK_NUM
233
+ #define ETHERNET3_MAX_SOCK_NUM 4
234
+ #endif
235
+
236
+ Ethernet.setCsPin (USE_THIS_SS_PIN);
237
+ Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
238
+
85
239
#elif USE_CUSTOM_ETHERNET
240
+
86
241
// You have to add initialization for your Custom Ethernet here
87
242
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
88
- // Ethernet.init(USE_THIS_SS_PIN);
89
-
90
- #endif // ( ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
91
- #endif
243
+ Ethernet.init (USE_THIS_SS_PIN);
244
+
245
+ #endif // ( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET3 || USE_ETHERNET_LARGE )
92
246
247
+ #endif // defined(ESP8266)
248
+
249
+
250
+ #endif // USE_ETHERNET_WRAPPER
251
+
252
+
93
253
// start the ethernet connection and the server:
94
254
// Use DHCP dynamic IP and random mac
95
255
uint16_t index = millis () % NUMBER_OF_MAC;
96
256
// Use Static IP
97
257
// Ethernet.begin(mac[index], ip);
98
258
Ethernet.begin (mac[index]);
99
259
100
- // you're connected now, so print out the data
101
- Serial.print (F (" You're connected to the network, IP = " ));
260
+ // Just info to know how to connect correctly
261
+ Serial.println (" =========================" );
262
+ Serial.println (" Currently Used SPI pinout:" );
263
+ Serial.print (" MOSI:" );
264
+ Serial.println (MOSI);
265
+ Serial.print (" MISO:" );
266
+ Serial.println (MISO);
267
+ Serial.print (" SCK:" );
268
+ Serial.println (SCK);
269
+ Serial.print (" SS:" );
270
+ Serial.println (SS);
271
+ #if USE_ETHERNET3
272
+ Serial.print (" SPI_CS:" );
273
+ Serial.println (SPI_CS);
274
+ #endif
275
+ Serial.println (F (" =========================" ));
276
+
277
+ Serial.print (F (" Using mac index = " ));
278
+ Serial.println (index);
279
+
280
+ Serial.print (F (" Connected! IP address: " ));
102
281
Serial.println (Ethernet.localIP ());
103
282
104
283
// Note - the default maximum packet size is 128 bytes. If the
105
284
// combined length of clientId, username and password exceed this use the
106
285
// following to increase the buffer size:
107
286
// client.setBufferSize(255);
287
+ }
288
+
289
+ #define MQTT_PUBLISH_INTERVAL_MS 5000L
290
+
291
+ String data = " Hello from MQTTClient_Auth on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
292
+ const char *pubData = data.c_str();
293
+
294
+ unsigned long lastMsg = 0 ;
295
+
296
+ void loop ()
297
+ {
298
+ static unsigned long now;
108
299
109
- if (client.connect ( " arduinoClient " , " testuser " , " testpass " ))
300
+ if (! client.connected ( ))
110
301
{
111
- client.publish (" outTopic" , " hello world" );
112
- client.subscribe (" inTopic" );
302
+ reconnect ();
113
303
}
114
- }
115
304
116
- void loop ()
117
- {
305
+ // Sending Data
306
+ now = millis ();
307
+
308
+ if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
309
+ {
310
+ lastMsg = now;
311
+
312
+ if (!client.publish (TOPIC, pubData))
313
+ {
314
+ Serial.println (" Message failed to send." );
315
+ }
316
+
317
+ Serial.print (" Message Send : " + String (TOPIC) + " => " );
318
+ Serial.println (data);
319
+ }
320
+
118
321
client.loop ();
119
322
}
0 commit comments