Skip to content

Commit 8b427ed

Browse files
committed
onConnect callback added
1 parent 2d46171 commit 8b427ed

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

examples/TestCallback/TestCallback.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,23 @@ const int ledPin = D4; // Builtin ESP8266 LED
2525
//ModbusIP object
2626
ModbusIP mb;
2727

28-
// Callback function
28+
// Callback function for write (set) Coil. Returns value to store.
2929
uint16_t cbLed(TRegister* reg, uint16_t val) {
3030
//Attach ledPin to LED_COIL register
3131
digitalWrite(ledPin, (val == 0xFF00));
3232
return val;
3333
}
34+
35+
// Callback function for client connect. Returns true to allow connection.
36+
bool cbConn(IPAddress ip) {
37+
Serial.println(ip);
38+
return true;
39+
}
3440

3541
void setup() {
3642
Serial.begin(74880);
3743

38-
WiFi.begin("ssid", "pass");
44+
WiFi.begin("ssid", "password");
3945

4046
while (WiFi.status() != WL_CONNECTED) {
4147
delay(500);
@@ -46,7 +52,8 @@ void setup() {
4652
Serial.println("WiFi connected");
4753
Serial.println("IP address: ");
4854
Serial.println(WiFi.localIP());
49-
55+
56+
mb.onConnect(cbConn); // Add callback on connection event
5057
mb.begin();
5158

5259
pinMode(ledPin, OUTPUT);
@@ -57,4 +64,5 @@ void setup() {
5764
void loop() {
5865
//Call once inside loop() - all magic here
5966
mb.task();
60-
}
67+
yield();
68+
}

src/ModbusIP_ESP8266.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,29 @@
55
*/
66
#include "ModbusIP_ESP8266.h"
77

8-
//ModbusIP::ModbusIP() {
9-
//
10-
//}
11-
128
void ModbusIP::begin() {
139
WiFiServer::begin();
14-
for (uint8_t i = 0; i < TCP_MAX_CLIENTS; i++) {
10+
for (uint8_t i = 0; i < TCP_MAX_CLIENTS; i++)
1511
client[i] = WiFiClient();
16-
}
1712
}
1813

1914
void ModbusIP::task() {
2015
for (uint8_t n = 0; n < TCP_MAX_CLIENTS; n++) {
2116
if (!client[n] || !client[n].connected()) {
2217
client[n] = available();
23-
//if (client[n] && client[n].connected()) {
24-
// Serial.println(client[n].remoteIP());
25-
//}
18+
if (client[n] && client[n].connected()) {
19+
if (cbConnect && !cbConnect(client[n].remoteIP())) {
20+
client[n].stop(); // If callback returns false immediate close connection
21+
continue;
22+
}
23+
}
2624
}
2725

28-
int raw_len = 0;
26+
int raw_len = 0;
2927

30-
//if (client[n]) {
31-
if (client[n] && client[n].connected()) {
32-
// for (int x = 0; x < 300; x++) { // Time to have data available
33-
// if (client[n].available()) {
34-
// while (client[n].available() > raw_len) { //Computes data length
35-
raw_len = client[n].available();
36-
// delay(1);
37-
// }
38-
// break;
39-
// }
40-
// delay(10);
41-
// }
42-
}
43-
28+
if (client[n] && client[n].connected())
29+
raw_len = client[n].available();
30+
4431
if (raw_len > 7) {
4532
for (int i=0; i<7; i++) _MBAP[i] = client[n].read(); //Get MBAP
4633

@@ -69,12 +56,15 @@ void ModbusIP::task() {
6956

7057
client[n].write(sbuf, send_len);
7158
}
72-
#ifndef TCP_KEEP_ALIVE
59+
#ifndef TCP_KEEP_ALIVE
7360
client[n].stop();
74-
#endif
61+
#endif
7562
free(_frame);
7663
_len = 0;
7764
}
78-
//}
65+
}
7966
}
67+
68+
void ModbusIP::onConnect(cbModbusConnect cb = NULL) {
69+
cbConnect = cb;
8070
}

src/ModbusIP_ESP8266.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@
2020
#define TCP_KEEP_ALIVE
2121
#define TCP_MAX_CLIENTS 4
2222

23+
// Callback function Type
24+
typedef bool (*cbModbusConnect)(IPAddress ip);
25+
2326
class ModbusIP : public Modbus, public WiFiServer {
2427
private:
25-
byte _MBAP[7];
28+
byte _MBAP[7];
2629
#ifdef TCP_KEEP_ALIVE
2730
WiFiClient client[TCP_MAX_CLIENTS];
2831
#endif
32+
cbModbusConnect cbConnect = NULL;
2933
public:
3034
ModbusIP() : WiFiServer(MODBUSIP_PORT) {
3135
}
3236
void begin();
33-
void task();
37+
void task();
38+
void onConnect(cbModbusConnect cb);
3439
};
3540

3641
#endif //MODBUSIP_ESP8266_H

0 commit comments

Comments
 (0)