Skip to content

Commit 62098a6

Browse files
authored
Merge pull request #22 from AdamCummick/master
Adds Ethernet Examples
2 parents c49d7ab + 10a0b06 commit 62098a6

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Ethernet Modbus TCP Client Toggle
3+
4+
This sketch toggles the coil of a Modbus TCP server connected
5+
on and off every second.
6+
7+
Circuit:
8+
- Any Arduino MKR Board
9+
- MKR ETH Shield
10+
11+
created 16 July 2018
12+
by Sandeep Mistry
13+
*/
14+
15+
#include <SPI.h>
16+
#include <Ethernet.h>
17+
18+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19+
#include <ArduinoModbus.h>
20+
21+
// Enter a MAC address for your controller below.
22+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
23+
// The IP address will be dependent on your local network:
24+
byte mac[] = {
25+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
26+
};
27+
IPAddress ip(192, 168, 1, 177);
28+
29+
EthernetClient ethClient;
30+
ModbusTCPClient modbusTCPClient(ethClient);
31+
32+
IPAddress server(192, 168, 1, 10); // update with the IP Address of your Modbus server
33+
34+
void setup() {
35+
//Initialize serial and wait for port to open:
36+
Serial.begin(9600);
37+
while (!Serial) {
38+
; // wait for serial port to connect. Needed for native USB port only
39+
}
40+
41+
// start the Ethernet connection and the server:
42+
Ethernet.begin(mac, ip);
43+
44+
// Check for Ethernet hardware present
45+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
46+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
47+
while (true) {
48+
delay(1); // do nothing, no point running without Ethernet hardware
49+
}
50+
}
51+
if (Ethernet.linkStatus() == LinkOFF) {
52+
Serial.println("Ethernet cable is not connected.");
53+
}
54+
}
55+
56+
void loop() {
57+
if (!modbusTCPClient.connected()) {
58+
// client not connected, start the Modbus TCP client
59+
Serial.println("Attempting to connect to Modbus TCP server");
60+
61+
if (!modbusTCPClient.begin(server, 502)) {
62+
Serial.println("Modbus TCP Client failed to connect!");
63+
} else {
64+
Serial.println("Modbus TCP Client connected");
65+
}
66+
} else {
67+
// client connected
68+
69+
// write the value of 0x01, to the coil at address 0x00
70+
if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
71+
Serial.print("Failed to write coil! ");
72+
Serial.println(modbusTCPClient.lastError());
73+
}
74+
75+
// wait for 1 second
76+
delay(1000);
77+
78+
// write the value of 0x00, to the coil at address 0x00
79+
if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
80+
Serial.print("Failed to write coil! ");
81+
Serial.println(modbusTCPClient.lastError());
82+
}
83+
84+
// wait for 1 second
85+
delay(1000);
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Ethernet Modbus TCP Server LED
3+
4+
This sketch creates a Modbus TCP Server with a simulated coil.
5+
The value of the simulated coil is set on the LED
6+
7+
Circuit:
8+
- Any Arduino MKR Board
9+
- MKR ETH Shield
10+
11+
created 16 July 2018
12+
by Sandeep Mistry
13+
*/
14+
15+
#include <SPI.h>
16+
#include <Ethernet.h>
17+
18+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19+
#include <ArduinoModbus.h>
20+
21+
// Enter a MAC address for your controller below.
22+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
23+
// The IP address will be dependent on your local network:
24+
byte mac[] = {
25+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
26+
};
27+
IPAddress ip(192, 168, 1, 177);
28+
29+
EthernetServer ethServer(502);
30+
31+
ModbusTCPServer modbusTCPServer;
32+
33+
const int ledPin = LED_BUILTIN;
34+
35+
void setup() {
36+
// You can use Ethernet.init(pin) to configure the CS pin
37+
//Ethernet.init(10); // Most Arduino shields
38+
//Ethernet.init(5); // MKR ETH shield
39+
//Ethernet.init(0); // Teensy 2.0
40+
//Ethernet.init(20); // Teensy++ 2.0
41+
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
42+
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
43+
44+
// Open serial communications and wait for port to open:
45+
Serial.begin(9600);
46+
while (!Serial) {
47+
; // wait for serial port to connect. Needed for native USB port only
48+
}
49+
Serial.println("Ethernet Modbus TCP Example");
50+
51+
// start the Ethernet connection and the server:
52+
Ethernet.begin(mac, ip);
53+
54+
// Check for Ethernet hardware present
55+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
56+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
57+
while (true) {
58+
delay(1); // do nothing, no point running without Ethernet hardware
59+
}
60+
}
61+
if (Ethernet.linkStatus() == LinkOFF) {
62+
Serial.println("Ethernet cable is not connected.");
63+
}
64+
65+
// start the server
66+
ethServer.begin();
67+
68+
// start the Modbus TCP server
69+
if (!modbusTCPServer.begin()) {
70+
Serial.println("Failed to start Modbus TCP Server!");
71+
while (1);
72+
}
73+
74+
// configure the LED
75+
pinMode(ledPin, OUTPUT);
76+
digitalWrite(ledPin, LOW);
77+
78+
// configure a single coil at address 0x00
79+
modbusTCPServer.configureCoils(0x00, 1);
80+
}
81+
82+
void loop() {
83+
// listen for incoming clients
84+
EthernetClient client = ethServer.available();
85+
86+
if (client) {
87+
// a new client connected
88+
Serial.println("new client");
89+
90+
// let the Modbus TCP accept the connection
91+
modbusTCPServer.accept(client);
92+
93+
while (client.connected()) {
94+
// poll for Modbus TCP requests, while client connected
95+
modbusTCPServer.poll();
96+
97+
// update the LED
98+
updateLED();
99+
}
100+
101+
Serial.println("client disconnected");
102+
}
103+
}
104+
105+
void updateLED() {
106+
// read the current value of the coil
107+
int coilValue = modbusTCPServer.coilRead(0x00);
108+
109+
if (coilValue) {
110+
// coil value set, turn LED on
111+
digitalWrite(ledPin, HIGH);
112+
} else {
113+
// coild value clear, turn LED off
114+
digitalWrite(ledPin, LOW);
115+
}
116+
}

0 commit comments

Comments
 (0)