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

Commit 4c2227c

Browse files
authored
v1.1.2
### Release v1.1.2 1. Add SSL debug feature. 2. Enhance examples.
1 parent af72321 commit 4c2227c

File tree

4 files changed

+429
-19
lines changed

4 files changed

+429
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ If for some unfortunate reason you need SSL 3.0 or SSL 2.0, you will need to mod
911911
16. [WebClientRepeating](examples/WebClientRepeating)
912912
17. [WebClient_SSL](examples/WebClient_SSL)
913913
18. [WebServer](examples/WebServer)
914-
19. [**MQTTS_ThingStream**](examples/MQTTS_ThingStream). New from v1.1.1
914+
19. [**MQTTS_ThingStream**](examples/MQTTS_ThingStream).
915+
20. [**MQTT_ThingStream**](examples/MQTT_ThingStream).
915916

916917
---
917918
---

examples/MQTTS_ThingStream/MQTTS_ThingStream.ino

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030

3131
#include "defines.h"
3232

33-
#include "certificates.h" // This file must be regenerated at https://openslab-osu.github.io/bearssl-certificate-utility/
3433
#include <PubSubClient.h>
3534

3635
const char my_cert[] = "FIXME";
3736
const char my_key[] = "FIXME";
3837

39-
SSLClientParameters mTLS = SSLClientParameters::fromPEM(my_cert, sizeof my_cert, my_key, sizeof my_key);
40-
4138
#define USING_THINGSTREAM_IO true
4239

4340
#if USING_THINGSTREAM_IO
@@ -58,6 +55,7 @@ String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
5855
String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
5956

6057
#else
58+
6159
const char* MQTT_SERVER = "broker.emqx.io"; // Broker address
6260

6361
const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
@@ -68,7 +66,10 @@ String subTopic = "STM32_Sub"; // Topic to subcribe to
6866

6967
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
7068

71-
const int MQTT_PORT = 8883; //if you use SSL //1883 no SSL
69+
const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL
70+
71+
String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
72+
const char *pubData = data.c_str();
7273

7374
unsigned long lastMsg = 0;
7475

@@ -77,11 +78,8 @@ unsigned long lastMsg = 0;
7778

7879

7980
EthernetClient ethClient;
80-
EthernetSSLClient ethClientSSL(ethClient, TAs, (size_t)TAs_NUM, 1, EthernetSSLClient::SSL_NONE);
81-
// Debug, max_sessions = 1, debug = SSL_INFO
82-
//EthernetSSLClient ethClientSSL(ethClient, TAs, (size_t)TAs_NUM, 1, EthernetSSLClient::SSL_INFO);
8381

84-
PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClientSSL);
82+
PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient);
8583

8684
/*
8785
Called whenever a payload is received from a subscribed MQTT topic
@@ -100,7 +98,6 @@ void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
10098
Serial.println();
10199
}
102100

103-
104101
void reconnect()
105102
{
106103
// Loop until we're reconnected
@@ -122,8 +119,6 @@ void reconnect()
122119
Serial.println("...connected");
123120

124121
// Once connected, publish an announcement...
125-
String data = "Hello from MQTTS_ThingStream on " + String(BOARD_NAME);
126-
127122
client.publish(topic.c_str(), data.c_str());
128123

129124
Serial.println("Published connection message successfully!");
@@ -158,12 +153,9 @@ void setup()
158153
Serial.begin(115200);
159154
while (!Serial);
160155

161-
Serial.print("\nStart MQTTS_ThingStream on " + String(BOARD_NAME));
156+
Serial.print("\nStart MQTT_ThingStream on " + String(BOARD_NAME));
162157
Serial.println(" with " + String(SHIELD_TYPE));
163158

164-
// Enable mutual TLS with SSLClient
165-
//ethClientSSL.setMutualAuthParams(mTLS);
166-
167159
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
168160

169161
ET_LOGWARN(F("Default SPI pinout:"));
@@ -219,9 +211,6 @@ void setup()
219211

220212
#define MQTT_PUBLISH_INTERVAL_MS 5000L
221213

222-
String data = "Hello from MQTTS_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
223-
const char *pubData = data.c_str();
224-
225214
void loop()
226215
{
227216
static unsigned long now;
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/****************************************************************************************************************************
2+
MQTT_ThingStream.ino - Dead simple MQTT Client for Ethernet shields
3+
4+
For STM32F/L/H/G/WB/MP1 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module
5+
6+
EthernetWebServer_SSL_STM32 is a library for STM32 using the Ethernet shields to run WebServer and Client with/without SSL
7+
8+
Use SSLClient Library code from https://github.com/OPEnSLab-OSU/SSLClient
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_SSL_STM32
11+
Licensed under MIT license
12+
*****************************************************************************************************************************/
13+
/*
14+
Basic MQTT example (without SSL!)
15+
This sketch demonstrates the basic capabilities of the library.
16+
It connects to an MQTT server then:
17+
- publishes {Hello from MQTTClient_SSL on NUCLEO_F767ZI} to the topic [STM32_Pub]
18+
- subscribes to the topic [STM32_Sub], printing out any messages
19+
it receives. NB - it assumes the received payloads are strings not binary
20+
It will reconnect to the server if the connection is lost using a blocking
21+
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
22+
achieve the same result without blocking the main loop.
23+
24+
You will need to populate "certificates.h" with your trust anchors
25+
(see https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md)
26+
and my_cert/my_key with your certificate/private key pair
27+
(see https://github.com/OPEnSLab-OSU/SSLClient#mtls).
28+
*/
29+
30+
#include "defines.h"
31+
32+
#include <PubSubClient.h>
33+
34+
const char my_cert[] = "FIXME";
35+
const char my_key[] = "FIXME";
36+
37+
#define USING_THINGSTREAM_IO true
38+
39+
#if USING_THINGSTREAM_IO
40+
41+
const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/";
42+
const char *MQTT_ANNOUNCE_TOPIC = "/status";
43+
const char *MQTT_CONTROL_TOPIC = "/control";
44+
const char *MQTT_BLE_TOPIC = "/ble";
45+
46+
47+
// GOT FROM ThingsStream!
48+
const char *MQTT_SERVER = "mqtt.thingstream.io";
49+
const char *MQTT_USER = "MQTT_USER";
50+
const char *MQTT_PASS = "MQTT_PASS";
51+
const char *MQTT_CLIENT_ID = "MQTT_CLIENT_ID";
52+
53+
String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
54+
String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
55+
56+
#else
57+
58+
const char* MQTT_SERVER = "broker.emqx.io"; // Broker address
59+
60+
const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
61+
String topic = "STM32_Pub"; // Topic to subcribe to
62+
String subTopic = "STM32_Sub"; // Topic to subcribe to
63+
64+
#endif
65+
66+
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
67+
68+
const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL
69+
70+
String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
71+
const char *pubData = data.c_str();
72+
73+
unsigned long lastMsg = 0;
74+
75+
// Initialize the SSL client library
76+
// Arguments: EthernetClient, our trust anchors
77+
78+
79+
EthernetClient ethClient;
80+
81+
PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient);
82+
83+
/*
84+
Called whenever a payload is received from a subscribed MQTT topic
85+
*/
86+
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
87+
{
88+
Serial.print("MQTT Message receive [");
89+
Serial.print(topic);
90+
Serial.print("] ");
91+
92+
for (int i = 0; i < length; i++)
93+
{
94+
Serial.print((char)payload[i]);
95+
}
96+
97+
Serial.println();
98+
}
99+
100+
void reconnect()
101+
{
102+
// Loop until we're reconnected
103+
while (!client.connected())
104+
{
105+
Serial.print("Attempting MQTT connection to ");
106+
Serial.println(MQTT_SERVER);
107+
108+
// Attempt to connect
109+
110+
#if USING_THINGSTREAM_IO
111+
int connect_status = client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, "");
112+
#else
113+
int connect_status = client.connect(ID);
114+
#endif
115+
116+
if (connect_status)
117+
{
118+
Serial.println("...connected");
119+
120+
// Once connected, publish an announcement...
121+
client.publish(topic.c_str(), data.c_str());
122+
123+
Serial.println("Published connection message successfully!");
124+
125+
Serial.print("Subcribed to: ");
126+
Serial.println(subTopic);
127+
128+
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
129+
//ethClientSSL.flush();
130+
// ... and resubscribe
131+
client.subscribe(subTopic.c_str());
132+
// for loopback testing
133+
client.subscribe(topic.c_str());
134+
// This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
135+
//ethClientSSL.flush();
136+
}
137+
else
138+
{
139+
Serial.print("failed, rc=");
140+
Serial.print(client.state());
141+
Serial.println(" try again in 5 seconds");
142+
143+
// Wait 5 seconds before retrying
144+
delay(5000);
145+
}
146+
}
147+
}
148+
149+
void setup()
150+
{
151+
// Open serial communications and wait for port to open:
152+
Serial.begin(115200);
153+
while (!Serial);
154+
155+
Serial.print("\nStart MQTT_ThingStream on " + String(BOARD_NAME));
156+
Serial.println(" with " + String(SHIELD_TYPE));
157+
158+
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
159+
160+
ET_LOGWARN(F("Default SPI pinout:"));
161+
ET_LOGWARN1(F("MOSI:"), MOSI);
162+
ET_LOGWARN1(F("MISO:"), MISO);
163+
ET_LOGWARN1(F("SCK:"), SCK);
164+
ET_LOGWARN1(F("SS:"), SS);
165+
ET_LOGWARN(F("========================="));
166+
167+
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
168+
// For other boards, to change if necessary
169+
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
170+
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
171+
Ethernet.init (USE_THIS_SS_PIN);
172+
173+
#elif USE_ETHERNET3
174+
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
175+
#ifndef ETHERNET3_MAX_SOCK_NUM
176+
#define ETHERNET3_MAX_SOCK_NUM 4
177+
#endif
178+
179+
Ethernet.setCsPin (USE_THIS_SS_PIN);
180+
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
181+
182+
#elif USE_CUSTOM_ETHERNET
183+
// You have to add initialization for your Custom Ethernet here
184+
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
185+
//Ethernet.init(USE_THIS_SS_PIN);
186+
187+
#endif //( ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
188+
#endif
189+
190+
// start the ethernet connection and the server:
191+
// Use DHCP dynamic IP and random mac
192+
uint16_t index = millis() % NUMBER_OF_MAC;
193+
// Use Static IP
194+
//Ethernet.begin(mac[index], ip);
195+
Ethernet.begin(mac[index]);
196+
197+
// you're connected now, so print out the data
198+
Serial.print(F("You're connected to the network, IP = "));
199+
Serial.println(Ethernet.localIP());
200+
201+
// Note - the default maximum packet size is 256 bytes. If the
202+
// combined length of clientId, username and password exceed this use the
203+
// following to increase the buffer size:
204+
//client.setBufferSize(256);
205+
206+
Serial.println("***************************************");
207+
Serial.println(topic);
208+
Serial.println("***************************************");
209+
}
210+
211+
#define MQTT_PUBLISH_INTERVAL_MS 5000L
212+
213+
void loop()
214+
{
215+
static unsigned long now;
216+
217+
if (!client.connected())
218+
{
219+
reconnect();
220+
}
221+
222+
// Sending Data
223+
now = millis();
224+
225+
if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
226+
{
227+
lastMsg = now;
228+
229+
if (!client.publish(topic.c_str(), pubData))
230+
{
231+
Serial.println("Message failed to send.");
232+
}
233+
234+
Serial.print("MQTT Message Send : " + topic + " => ");
235+
Serial.println(data);
236+
}
237+
238+
client.loop();
239+
}

0 commit comments

Comments
 (0)