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

Commit 1677e1d

Browse files
authored
v1.1.0 to add support to WT32_ETH01
### Releases v1.1.0 1. Add support to **WT32_ETH01 (SSL and non-SSL)** 2. Add examples for `WT32_ETH01`
1 parent 6b0d524 commit 1677e1d

File tree

4 files changed

+502
-0
lines changed

4 files changed

+502
-0
lines changed
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/****************************************************************************************************************************
2+
FullyFeatureSSL_WT32_ETH01.ino
3+
4+
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support
5+
6+
Based on and modified from :
7+
8+
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic
11+
*****************************************************************************************************************************/
12+
13+
#include "defines.h"
14+
15+
extern "C"
16+
{
17+
#include "freertos/FreeRTOS.h"
18+
#include "freertos/timers.h"
19+
}
20+
21+
#define ASYNC_TCP_SSL_ENABLED true
22+
//#define ASYNC_TCP_SSL_ENABLED false
23+
24+
#include <AsyncMqtt_Generic.h>
25+
26+
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
27+
#define MQTT_HOST "broker.emqx.io" // Broker address
28+
29+
#if ASYNC_TCP_SSL_ENABLED
30+
31+
#define MQTT_SECURE true
32+
33+
const uint8_t MQTT_SERVER_FINGERPRINT[] = {0x7e, 0x36, 0x22, 0x01, 0xf9, 0x7e, 0x99, 0x2f, 0xc5, 0xdb, 0x3d, 0xbe, 0xac, 0x48, 0x67, 0x5b, 0x5d, 0x47, 0x94, 0xd2};
34+
const char *PubTopic = "async-mqtt/WT32_ETH01_SSL_Pub"; // Topic to publish
35+
36+
#define MQTT_PORT 8883
37+
38+
#else
39+
40+
const char *PubTopic = "async-mqtt/WT32_ETH01_Pub"; // Topic to publish
41+
42+
#define MQTT_PORT 1883
43+
44+
#endif
45+
46+
AsyncMqttClient mqttClient;
47+
TimerHandle_t mqttReconnectTimer;
48+
49+
void connectToMqtt()
50+
{
51+
Serial.println("Connecting to MQTT...");
52+
mqttClient.connect();
53+
}
54+
55+
void ETH_event(WiFiEvent_t event)
56+
{
57+
switch (event)
58+
{
59+
#if USING_CORE_ESP32_CORE_V200_PLUS
60+
case ARDUINO_EVENT_ETH_START:
61+
Serial.println("ETH starting");
62+
break;
63+
case ARDUINO_EVENT_ETH_CONNECTED:
64+
Serial.println("ETH connected");
65+
break;
66+
case ARDUINO_EVENT_ETH_GOT_IP:
67+
Serial.println("ETH got IP");
68+
Serial.print("IP address: "); Serial.println(ETH.localIP());
69+
connectToMqtt();
70+
break;
71+
case ARDUINO_EVENT_ETH_DISCONNECTED:
72+
Serial.println("ETH lost connection");
73+
74+
// ensure we don't reconnect to MQTT when no ETH
75+
xTimerStop(mqttReconnectTimer, 0);
76+
77+
break;
78+
case ARDUINO_EVENT_ETH_STOP:
79+
Serial.println("ETH stops");
80+
81+
// ensure we don't reconnect to MQTT when no ETH
82+
xTimerStop(mqttReconnectTimer, 0);
83+
84+
break;
85+
#else
86+
case SYSTEM_EVENT_ETH_CONNECTED:
87+
erial.println(F("ETH Connected"));
88+
break;
89+
90+
case SYSTEM_EVENT_ETH_GOT_IP:
91+
Serial.println("ETH connected");
92+
Serial.println("IP address: "); Serial.println(ETH.localIP());
93+
connectToMqtt();
94+
break;
95+
case SYSTEM_EVENT_ETH_DISCONNECTED:
96+
case SYSTEM_EVENT_ETH_STOP:
97+
Serial.println("ETH lost connection");
98+
99+
// ensure we don't reconnect to MQTT when no ETH
100+
xTimerStop(mqttReconnectTimer, 0);
101+
102+
break;
103+
#endif
104+
}
105+
}
106+
107+
void printSeparationLine()
108+
{
109+
Serial.println("************************************************");
110+
}
111+
112+
void onMqttConnect(bool sessionPresent)
113+
{
114+
Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST);
115+
Serial.print(", port: "); Serial.println(MQTT_PORT);
116+
Serial.print("PubTopic: "); Serial.println(PubTopic);
117+
118+
printSeparationLine();
119+
Serial.print("Session present: "); Serial.println(sessionPresent);
120+
121+
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
122+
Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub);
123+
124+
mqttClient.publish(PubTopic, 0, true, "WT32_ETH01 Test");
125+
Serial.println("Publishing at QoS 0");
126+
127+
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
128+
Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1);
129+
130+
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
131+
Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2);
132+
133+
printSeparationLine();
134+
}
135+
136+
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
137+
{
138+
(void) reason;
139+
140+
Serial.println("Disconnected from MQTT.");
141+
142+
if (WT32_ETH01_isConnected())
143+
{
144+
xTimerStart(mqttReconnectTimer, 0);
145+
}
146+
}
147+
148+
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
149+
{
150+
Serial.println("Subscribe acknowledged.");
151+
Serial.print(" packetId: "); Serial.println(packetId);
152+
Serial.print(" qos: "); Serial.println(qos);
153+
}
154+
155+
void onMqttUnsubscribe(const uint16_t& packetId)
156+
{
157+
Serial.println("Unsubscribe acknowledged.");
158+
Serial.print(" packetId: "); Serial.println(packetId);
159+
}
160+
161+
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
162+
const size_t& len, const size_t& index, const size_t& total)
163+
{
164+
(void) payload;
165+
166+
Serial.println("Publish received.");
167+
Serial.print(" topic: "); Serial.println(topic);
168+
Serial.print(" qos: "); Serial.println(properties.qos);
169+
Serial.print(" dup: "); Serial.println(properties.dup);
170+
Serial.print(" retain: "); Serial.println(properties.retain);
171+
Serial.print(" len: "); Serial.println(len);
172+
Serial.print(" index: "); Serial.println(index);
173+
Serial.print(" total: "); Serial.println(total);
174+
}
175+
176+
void onMqttPublish(const uint16_t& packetId)
177+
{
178+
Serial.println("Publish acknowledged");
179+
Serial.print(" packetId: "); Serial.println(packetId);
180+
}
181+
182+
void setup()
183+
{
184+
Serial.begin(115200);
185+
while (!Serial);
186+
187+
Serial.print("\nStarting FullyFeatureSSL_WT32_ETH01 on "); Serial.print(BOARD_NAME);
188+
Serial.println(" with " + String(SHIELD_TYPE));
189+
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
190+
Serial.println(ASYNC_MQTT_GENERIC_VERSION);
191+
192+
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
193+
194+
mqttClient.onConnect(onMqttConnect);
195+
mqttClient.onDisconnect(onMqttDisconnect);
196+
mqttClient.onSubscribe(onMqttSubscribe);
197+
mqttClient.onUnsubscribe(onMqttUnsubscribe);
198+
mqttClient.onMessage(onMqttMessage);
199+
mqttClient.onPublish(onMqttPublish);
200+
201+
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
202+
203+
#if ASYNC_TCP_SSL_ENABLED
204+
mqttClient.setSecure(MQTT_SECURE);
205+
206+
if (MQTT_SECURE)
207+
{
208+
//mqttClient.addServerFingerprint((const uint8_t[])MQTT_SERVER_FINGERPRINT);
209+
mqttClient.addServerFingerprint((const uint8_t *)MQTT_SERVER_FINGERPRINT);
210+
}
211+
#endif
212+
213+
//////////////////////////////////////////////
214+
215+
// To be called before ETH.begin()
216+
WiFi.onEvent(ETH_event);
217+
218+
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
219+
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
220+
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
221+
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
222+
223+
// Static IP, leave without this line to get IP via DHCP
224+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
225+
//ETH.config(myIP, myGW, mySN, myDNS);
226+
227+
WT32_ETH01_waitForConnect();
228+
229+
//////////////////////////////////////////////
230+
}
231+
232+
void loop()
233+
{
234+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support
4+
5+
Based on and modified from :
6+
7+
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
8+
9+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic
10+
***************************************************************************************************************************************/
11+
12+
#ifndef defines_h
13+
#define defines_h
14+
15+
// Debug Level from 0 to 4
16+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
17+
#define _ASYNC_MQTT_LOGLEVEL_ 4
18+
19+
#include <WebServer_WT32_ETH01.h>
20+
21+
// Select the IP address according to your local network
22+
IPAddress myIP(192, 168, 2, 232);
23+
IPAddress myGW(192, 168, 2, 1);
24+
IPAddress mySN(255, 255, 255, 0);
25+
26+
// Google DNS Server IP
27+
IPAddress myDNS(8, 8, 8, 8);
28+
29+
#endif //defines_h

0 commit comments

Comments
 (0)