This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMQTT_ThingStream_Ethernet_RP2040.ino
497 lines (374 loc) · 13.9 KB
/
MQTT_ThingStream_Ethernet_RP2040.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************************************************************************
MQTT_ThingStream_Ethernet_RP2040.ino
For W5x00, LAN8720 and ENC28J60 Ethernet shields.
Ethernet_Manager is a library for nRF52, Teensy, STM32, SAM DUE and SAMD boards, with Ethernet W5x00, LAN8720 or ENC28J60 shields,
to enable easy configuration/reconfiguration of Credentials and autoconnect/autoreconnect of Ethernet.
AVR Mega is not supported.
Built by Khoi Hoang https://github.com/khoih-prog/Ethernet_Manager
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
///////////// Start MQTT ThingStream ///////////////
#include <PubSubClient.h>
// GOT FROM ThingsStream!
const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/";
const char *MQTT_ANNOUNCE_TOPIC = "/status";
const char *MQTT_CONTROL_TOPIC = "/control";
const char *MQTT_BLE_TOPIC = "/ble";
String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
//////////// End MQTT ThingStream ///////////////
Ethernet_Manager ethernet_manager;
IPAddress localEthernetIP;
///////////// Start MQTT ThingStream ///////////////
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
unsigned long lastMsg = 0;
// Initialize the SSL client library
// Arguments: EthernetClient, our trust anchors
EthernetClient ethClient;
PubSubClient* client = NULL;
/*
Called whenever a payload is received from a subscribed MQTT topic
*/
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("\nMQTT Message receive [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect()
{
// Loop until we're reconnected
while (!client->connected())
{
Serial.print("Attempting MQTT connection to ");
Serial.println(MQTT_SERVER);
// Attempt to connect
int connect_status = client->connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, "");
if (connect_status)
{
Serial.println("...connected");
// Once connected, publish an announcement...
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
client->publish(topic.c_str(), data.c_str());
Serial.println("Published connection message successfully!");
Serial.print("Subcribed to: ");
Serial.println(subTopic);
// ... and resubscribe
client->subscribe(subTopic.c_str());
// for loopback testing
client->subscribe(topic.c_str());
}
else
{
Serial.print("failed, rc=");
Serial.print(client->state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
///////////// End MQTT ThingStream ///////////////
// Use to detect W5100 shield. The linkStatus() is not working with W5100
// in Ethernet and EthernetLarge libraries
bool isW5500 = true;
void heartBeatPrint()
{
static int num = 1;
static int linkStatus = 0;
localEthernetIP = Ethernet.localIP();
#if (USE_ETHERNET_GENERIC)
#if USE_W5100
// The linkStatus() is not working with W5100. Just using IP != 0.0.0.0
if ( (uint32_t) localEthernetIP != 0 )
#else
linkStatus = Ethernet.link();
ETM_LOGINFO1("localEthernetIP = ", localEthernetIP);
if ( ( linkStatus == 1 ) || ((uint32_t) localEthernetIP != 0) )
#endif
#else
// The linkStatus() is not working with W5100. Just using IP != 0.0.0.0
// Better to use ping for W5100
linkStatus = (int) Ethernet.linkStatus();
ETM_LOGINFO1("localEthernetIP = ", localEthernetIP);
if ( ( (linkStatus == LinkON) || !isW5500 ) || ((uint32_t) localEthernetIP != 0) )
#endif
{
Serial.print(F("H"));
}
else
Serial.print(F("F"));
if (num == 80)
{
Serial.println();
ethernet_manager.printMacAddress();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
#define STATUS_CHECK_INTERVAL 10000L
static unsigned long checkstatus_timeout = STATUS_CHECK_INTERVAL;
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout))
{
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void initEthernet()
{
#if ( defined(USE_UIP_ETHERNET) && USE_UIP_ETHERNET )
ET_LOGWARN(F("======== USE_UIP_ETHERNET ========"));
#elif USE_NATIVE_ETHERNET
ET_LOGWARN(F("======== USE_NATIVE_ETHERNET ========"));
#elif USE_ETHERNET_GENERIC
ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ==========="));
#elif USE_ETHERNET_ENC
ET_LOGWARN(F("=========== USE_ETHERNET_ENC ==========="));
#else
ET_LOGWARN(F("========================="));
#endif
ET_LOGWARN(F("Default SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
ET_LOGWARN(F("========================="));
#if defined(ESP8266)
#define PIN_D5 14 // Pin D5 mapped to pin GPIO14/HSCLK of ESP8266
#define PIN_D6 12 // Pin D6 mapped to pin GPIO12/HMISO of ESP8266
#define PIN_D7 13 // Pin D7 mapped to pin GPIO13/RXD2/HMOSI of ESP8266
// Connection for ESP8266
// MOSI: D7/GPIO13, MISO: D6/GPIO12, SCK: D5/GPIO14, CS/SS: D2/GPIO4
// For ESP8266, change for other boards if necessary
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN D2 // For ESP8266
#endif
ET_LOGWARN1(F("ESP8266 setCsPin:"), USE_THIS_SS_PIN);
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// For ESP8266
// Pin D0(GPIO16) D1(GPIO5) D2(GPIO4) D3(GPIO0) D4(GPIO2) D8
// Ethernet 0 X X X X 0
// Ethernet2 X X X X X 0
// Ethernet3 X X X X X 0
// EthernetLarge X X X X X 0
// Ethernet_ESP8266 0 0 0 0 0 0
// D2 is safe to used for Ethernet, Ethernet2, Ethernet3, EthernetLarge libs
// Must use library patch for Ethernet, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET3 || USE_ETHERNET_LARGE )
#elif defined(ESP32)
#define PIN_D18 18 // Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32
#define PIN_D19 19 // Pin D19 mapped to pin GPIO19/VSPI_MISO of ESP32
#define PIN_D23 23 // Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Connection for ESP32
// MOSI: GPIO23, MISOP: GPIO19, SCK: GPIO18, CS/SS: GPIO22
#ifndef USE_THIS_SS_PIN
#define USING_M5STACK_W5500 false
#if USING_M5STACK_W5500
#warning Using M5Stack_Core_ESP32 with W5500 mudule
#define USE_THIS_SS_PIN 26 // For M5Stack_Core_ESP32 with W5500 mudule
#else
#define USE_THIS_SS_PIN 22 // For ESP32
#endif
#endif
ET_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, EthernetLarge libraries
// ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
// ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_LARGE )
#else //defined(ESP8266)
// unknown board, do nothing, use default SS = 10
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 10 // For other boards
#endif
#if defined(BOARD_NAME)
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
#else
ET_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN);
#endif
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET )
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_LARGE )
#endif //defined(ESP8266)
// Just info to know how to connect correctly
#if defined(CUR_PIN_MISO)
ET_LOGWARN(F("Currently Used SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI);
ET_LOGWARN1(F("MISO:"), CUR_PIN_MISO);
ET_LOGWARN1(F("SCK:"), CUR_PIN_SCK);
ET_LOGWARN1(F("SS:"), CUR_PIN_SS);
#else
ET_LOGWARN(F("Currently Used SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
#endif
ET_LOGWARN(F("========================="));
}
#if USING_CUSTOMS_STYLE
const char NewCustomsStyle[] /*PROGMEM*/ =
"<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\
button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#endif
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.println("\nStart MQTT_ThingStream_Ethernet_RP2040 on " + String(BOARD_NAME));
Serial.println("Ethernet Shield type : " + String(SHIELD_TYPE));
Serial.println(ETHERNET_MANAGER_VERSION);
Serial.println(DOUBLERESETDETECTOR_GENERIC_VERSION);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
initEthernet();
//////////////////////////////////////////////
#if USING_CUSTOMS_STYLE
ethernet_manager.setCustomsStyle(NewCustomsStyle);
#endif
#if USING_CUSTOMS_HEAD_ELEMENT
ethernet_manager.setCustomsHeadElement("<style>html{filter: invert(10%);}</style>");
#endif
#if USING_CORS_FEATURE
ethernet_manager.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
//////////////////////////////////////////////
#define USING_CUSTOM_MAC_ADDRESS false
#if USING_CUSTOM_MAC_ADDRESS
// To use your specified macAddress
byte newMacAddress[6] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF };
ethernet_manager.setMacAddress(newMacAddress);
#endif
//////////////////////////////////////////////
ethernet_manager.begin();
//////////////////////////////////////////////
localEthernetIP = Ethernet.localIP();
if ( (uint32_t) localEthernetIP != 0 )
{
Serial.print(F("Connected! IP address: "));
Serial.println(localEthernetIP);
}
else
{
Serial.println(F("Ethernet not Connected! Please check."));
}
// Detect W5100 only in Ethernet and EthernetLarge libraries
#if (USE_ETHERNET_GENERIC)
isW5500 = (Ethernet.hardwareStatus() == EthernetW5500);
Serial.print(F("Ethernet type is "));
Serial.println(isW5500 ? "W5500" : "W5100");
#endif
Serial.println(F("***************************************"));
Serial.println(topic);
Serial.println(F("***************************************"));
}
#if (USE_DYNAMIC_PARAMETERS)
void displayCredentials()
{
Serial.println("\nYour stored Credentials :");
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.println(String(myMenuItems[i].displayName) + " = " + myMenuItems[i].pdata);
}
}
void displayCredentialsOnce()
{
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
}
#endif
#define MQTT_PUBLISH_INTERVAL_MS 20000L
String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
const char *pubData = data.c_str();
void loop()
{
static bool inConfigMode = true;
static unsigned long currentMillis;
inConfigMode = ethernet_manager.run();
if (!inConfigMode)
{
if (!client)
{
client = new PubSubClient(MQTT_SERVER, atoi(MQTT_PORT), mqtt_receive_callback, ethClient);
// Note - the default maximum packet size is 256 bytes. If the
// combined length of clientId, username and password exceed this use the
// following to increase the buffer size:
//client->setBufferSize(256);
}
if (!client->connected())
{
reconnect();
}
// Sending Data
currentMillis = millis();
if (currentMillis - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
{
lastMsg = currentMillis;
if (!client->publish(topic.c_str(), pubData))
{
Serial.println("Message failed to send.");
}
Serial.print("\nMQTT Message Send : " + topic + " => ");
Serial.println(data);
}
client->loop();
check_status();
#if (USE_DYNAMIC_PARAMETERS)
displayCredentialsOnce();
#endif
}
}