Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 50a8ab5

Browse files
authored
v1.1.2
### Release v1.1.2 1. Add SSL debug feature. 2. Enhance examples.
1 parent 2530c88 commit 50a8ab5

File tree

11 files changed

+446
-49
lines changed

11 files changed

+446
-49
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ Check [`EthernetWebServer Library Issue: Support for STM32F Series`](https://git
117117
---
118118
---
119119

120+
### Release v1.1.2
121+
122+
1. Add SSL debug feature.
123+
2. Enhance examples.
124+
120125
### Release v1.1.1
121126

122127
1. Permit sites with "Chain could not be linked to a trust anchor" such as mqtt.thingstream.io.
@@ -1965,6 +1970,11 @@ MQTT Message receive [esp32-sniffer/12345678/ble] Hello from MQTTS_ThingStream_T
19651970

19661971
## Releases History
19671972

1973+
### Release v1.1.2
1974+
1975+
1. Add SSL debug feature.
1976+
2. Enhance examples.
1977+
19681978
### Release v1.1.1
19691979

19701980
1. Permit sites with "Chain could not be linked to a trust anchor" such as mqtt.thingstream.io.
@@ -2046,6 +2056,7 @@ Submit issues to: [EthernetWebServer_SSL_STM32 issues](https://github.com/khoih-
20462056
3. Support more non-compatible Ethernet Libraries such as Ethernet_Shield_W5200, EtherCard, EtherSia
20472057
4. Add mDNS features.
20482058
5. Use AsyncTCP features from [Phil Bowles' STM32AsyncTCP](https://github.com/philbowles/STM32AsyncTCP).
2059+
6. Add **High-level HTTP (GET, POST, PUT, PATCH, DELETE) and WebSocket Client**
20492060

20502061
### DONE
20512062

examples/MQTTClient_Auth/MQTTClient_Auth.ino

Lines changed: 220 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
#include <PubSubClient.h>
3030

3131
// 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+
3340
//IPAddress mqttServer(172, 16, 0, 2);
3441

3542
void callback(char* topic, byte* payload, unsigned int length)
@@ -49,6 +56,49 @@ void callback(char* topic, byte* payload, unsigned int length)
4956
EthernetClient ethClient;
5057
PubSubClient client(mqttServer, 1883, callback, ethClient);
5158

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+
52102
void setup()
53103
{
54104
// Open serial communications and wait for port to open:
@@ -58,7 +108,25 @@ void setup()
58108
Serial.print("\nStart MQTTClient_Auth on " + String(BOARD_NAME));
59109
Serial.println(" with " + String(SHIELD_TYPE));
60110

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
62130

63131
ET_LOGWARN(F("Default SPI pinout:"));
64132
ET_LOGWARN1(F("MOSI:"), MOSI);
@@ -67,10 +135,58 @@ void setup()
67135
ET_LOGWARN1(F("SS:"), SS);
68136
ET_LOGWARN(F("========================="));
69137

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+
71183
// 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);
74190
Ethernet.init (USE_THIS_SS_PIN);
75191

76192
#elif USE_ETHERNET3
@@ -81,39 +197,126 @@ void setup()
81197

82198
Ethernet.setCsPin (USE_THIS_SS_PIN);
83199
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
84221

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+
85239
#elif USE_CUSTOM_ETHERNET
240+
86241
// You have to add initialization for your Custom Ethernet here
87242
// 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 )
92246

247+
#endif //defined(ESP8266)
248+
249+
250+
#endif //USE_ETHERNET_WRAPPER
251+
252+
93253
// start the ethernet connection and the server:
94254
// Use DHCP dynamic IP and random mac
95255
uint16_t index = millis() % NUMBER_OF_MAC;
96256
// Use Static IP
97257
//Ethernet.begin(mac[index], ip);
98258
Ethernet.begin(mac[index]);
99259

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: "));
102281
Serial.println(Ethernet.localIP());
103282

104283
// Note - the default maximum packet size is 128 bytes. If the
105284
// combined length of clientId, username and password exceed this use the
106285
// following to increase the buffer size:
107286
// 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;
108299

109-
if (client.connect("arduinoClient", "testuser", "testpass"))
300+
if (!client.connected())
110301
{
111-
client.publish("outTopic", "hello world");
112-
client.subscribe("inTopic");
302+
reconnect();
113303
}
114-
}
115304

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+
118321
client.loop();
119322
}

0 commit comments

Comments
 (0)