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 9
/
FullyFeatured_ESP32_ENC.ino
257 lines (199 loc) · 6.93 KB
/
FullyFeatured_ESP32_ENC.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
/****************************************************************************************************************************
FullyFeature-ESP32_ENC.ino
AsyncMQTT_ESP32 is a library for ESP32 boards using WiFi or LwIP W5500, LAN8720 or ENC28J60
Based on and modified from :
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
2) async-mqtt-client (https://github.com/khoih-prog/AsyncMQTT_Generic)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMQTT_ESP32
*****************************************************************************************************************************/
/*
This example uses FreeRTOS softwaretimers as there is no built-in Ticker library
*/
#include "defines.h"
extern "C"
{
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMQTT_ESP32.h>
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
#define MQTT_HOST "broker.emqx.io" // Broker address
#define MQTT_PORT 1883
const char *PubTopic = "async-mqtt/ESP32_ENC_Pub"; // Topic to publish
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
void connectToMqtt()
{
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void ETH_event(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH starting");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.println("ETH got IP");
Serial.print("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH stops");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_ETH_CONNECTED:
erial.println(F("ETH Connected"));
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.println("ETH connected");
Serial.println("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
#endif
default:
break;
}
}
void printSeparationLine()
{
Serial.println("************************************************");
}
void onMqttConnect(bool sessionPresent)
{
Serial.print("Connected to MQTT broker: ");
Serial.print(MQTT_HOST);
Serial.print(", port: ");
Serial.println(MQTT_PORT);
Serial.print("PubTopic: ");
Serial.println(PubTopic);
printSeparationLine();
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish(PubTopic, 0, true, "ESP32_ENC Test");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
printSeparationLine();
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
(void) reason;
Serial.println("Disconnected from MQTT.");
if (ESP32_ENC_isConnected())
{
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
{
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(const uint16_t& packetId)
{
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
const size_t& len, const size_t& index, const size_t& total)
{
(void) payload;
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void onMqttPublish(const uint16_t& packetId)
{
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
Serial.print("\nStarting FullyFeature_ESP32_ENC on ");
Serial.print(ARDUINO_BOARD);
Serial.println(" with " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_ESP32_ENC_VERSION);
Serial.println(ASYNC_MQTT_ESP32_VERSION);
AMQTT_LOGWARN(F("Default SPI pinout:"));
AMQTT_LOGWARN1(F("MOSI:"), MOSI_GPIO);
AMQTT_LOGWARN1(F("MISO:"), MISO_GPIO);
AMQTT_LOGWARN1(F("SCK:"), SCK_GPIO);
AMQTT_LOGWARN1(F("CS:"), CS_GPIO);
AMQTT_LOGWARN1(F("INT:"), INT_GPIO);
AMQTT_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
AMQTT_LOGWARN(F("========================="));
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
///////////////////////////////////
// To be called before ETH.begin()
WiFi.onEvent(ETH_event);
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
// int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac);
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST );
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(myIP, myGW, mySN, myDNS);
ESP32_ENC_waitForConnect();
///////////////////////////////////
}
void loop()
{
}