-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPNowLANGateway.ino
307 lines (249 loc) · 6.87 KB
/
ESPNowLANGateway.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
#include <esp_now.h>
#include <ETH.h>
#include <WiFiClientSecure.h>
#include "PubSubClient.h"
#include "Updates.h"
#define CHANNEL 1
#define ETH_POWER_PIN -1
#define ETH_ADDR 1
#define RXLED -1
#define CONNLED -1
#define RXBLINKDELAY 100
long lastrxblink = 0;
//TODO: Configuration
const char* host = "espNow32mqtt1";
//TODO: Configuration
#define MQTT_SERVER "..."
#define MQTT_PORT 8883
#define MQTT_CLIENTID "..."
#define MQTT_USERNAME "..."
#define MQTT_KEY "..."
//TODO: Configuration
#define MQTT_ROOT_TOPIC "octopus/espnow"
#define KEEPALIVE 5000
#define MQTT_KEEPALIVE 60000
WiFiClientSecure mqttclient;
PubSubClient mqtt(mqttclient);
// Init ESP Now with fallback
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
static bool eth_connected = false;
bool espnow_received = false;
char espnow_macStr[18];
uint8_t espnow_dataLen;
uint8_t *espnow_data;
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_ETH_START:
Serial.println("ETH Started");
ETH.setHostname(host);
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("ETH Connected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.print("ETH MAC: ");
Serial.print(ETH.macAddress());
Serial.print(", IPv4: ");
Serial.print(ETH.localIP());
if (ETH.fullDuplex()) {
Serial.print(", FULL_DUPLEX");
}
Serial.print(", ");
Serial.print(ETH.linkSpeed());
Serial.println("Mbps");
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("ETH Disconnected");
eth_connected = false;
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH Stopped");
eth_connected = false;
break;
default:
break;
}
}
void MQTT_connect() {
// Loop until we're reconnected
int retry = 0;
while (!mqtt.connected()) {
retry++;
Serial.print(F("Attempting MQTT connection..."));
// Create a random client ID
String clientId = MQTT_CLIENTID;
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqtt.connect(clientId.c_str(), MQTT_USERNAME, MQTT_KEY)) {
Serial.println(F("connected"));
// Once connected, publish an announcement...
mqtt.publish(MQTT_ROOT_TOPIC, MQTT_CLIENTID);
mqtt.subscribe(String(MQTT_ROOT_TOPIC + String(F("/read"))).c_str());
} else {
Serial.print(F("failed, rc="));
Serial.print(mqtt.state());
if (retry > 10) {
Serial.println("Failed connect to MQTT, reboot");
ESP.restart();
}
Serial.println(F(" try again in 5 seconds"));
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// config AP SSID
void configDeviceAP() {
WiFi.mode(WIFI_AP);
const char *SSID = "ESPNow Gateway";
bool result = WiFi.softAP(SSID, "donttellmeyourpassword", CHANNEL, 0);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
}
}
void banner() {
Serial.println("ESP Now Gateway v0.2");
Serial.println("====================");
}
void setup() {
Serial.begin(115200);
banner();
Serial.print(F("LAN connect... "));
if (RXLED >= 0) {
pinMode(RXLED, OUTPUT);
}
if (CONNLED >= 0) {
pinMode(CONNLED, OUTPUT);
}
WiFi.onEvent(WiFiEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLOCK_GPIO17_OUT);
int retry = 0;
while (!eth_connected) {
Serial.println(F("Waiting"));
delay(1000);
retry++;
if (retry > 10) {
break;
}
}
if (!eth_connected) {
Serial.println("Connect LAN failed, rebooting");
ESP.restart();
}
Serial.println(F("Connected"));
configDeviceAP();
InitESPNow();
esp_now_register_recv_cb(OnDataRecv);
Serial.println(F("Ready"));
Serial.print(F("IP address: "));
Serial.println(ETH.localIP());
Serial.print(F("Configure OTA... "));
OTA_Setup();
Serial.println(F("OK"));
Serial.print(F("Configure WEB... "));
WEB_Setup();
Serial.println(F("OK"));
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
mqtt.setCallback(mqtt_callback);
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
Serial.print(F("Message arrived ["));
Serial.print(topic);
Serial.print(F("] "));
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
if (espnow_received) {
Serial.println("Not processed yet, skipping message");
return;
}
espnow_received = true;
snprintf(espnow_macStr, sizeof(espnow_macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
espnow_dataLen = data_len;
espnow_data = (uint8_t*)malloc(data_len * sizeof(uint8_t));
memcpy(espnow_data, data, data_len);
}
long previousMQTTKeepAliveMillis = 0;
long mqtt_keepalive_counter = 0;
void handleMQTTKeepAlive() {
if (millis() - previousMQTTKeepAliveMillis >= MQTT_KEEPALIVE) {
previousMQTTKeepAliveMillis = millis();
mqtt_keepalive_counter++;
if (!mqtt.connected()) {
return;
}
mqtt.publish(String(MQTT_ROOT_TOPIC + String("/keepalive")).c_str(), String(mqtt_keepalive_counter).c_str());
Serial.println("MQTT Keep Alive");
}
}
long previousKeepAliveMillis = 0;
void handleKeepAlive() {
if (millis() - previousKeepAliveMillis >= KEEPALIVE) {
previousKeepAliveMillis = millis();
Serial.println("Keep Alive");
}
}
void processEspNowData() {
Serial.print("Last Packet Recv from: "); Serial.println(espnow_macStr);
Serial.print("Last Packet Recv Len: "); Serial.println(espnow_dataLen);
Serial.print("Last Packet Recv Data: ");
for (int i = 0; i < espnow_dataLen; i++) {
Serial.print(espnow_data[i], HEX);
Serial.print(" ");
}
Serial.println();
if (mqtt.connected()) {
mqtt.publish(String(MQTT_ROOT_TOPIC + String("/") + espnow_macStr).c_str(), espnow_data, espnow_dataLen);
}
}
void loop() {
if (espnow_received)
{
lastrxblink = millis();
Serial.println("Received ESP Now data");
processEspNowData();
free(espnow_data);
espnow_received = false;
}
if (eth_connected) {
handleUpdates();
if (!mqtt.connected()) {
MQTT_connect();
}
mqtt.loop();
handleMQTTKeepAlive();
if (CONNLED >= 0) {
digitalWrite(CONNLED, 1);
}
}
else {
if (CONNLED >= 0) {
digitalWrite(CONNLED, 0);
}
}
if (RXLED >= 0) {
if ( millis() < (lastrxblink + RXBLINKDELAY) ) {
digitalWrite(RXLED, 1);
} else {
digitalWrite(RXLED, 0);
}
}
handleKeepAlive();
}