Skip to content

Commit cd7f1dc

Browse files
committed
Add initial examples: RTU + TCP x Client + Server
1 parent e997588 commit cd7f1dc

File tree

6 files changed

+346
-0
lines changed

6 files changed

+346
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Modbus RTU Client Toggle
3+
4+
This sketch toggles the coil of a Modbus RTU server connected via RS485
5+
on and off every second.
6+
7+
Circuit:
8+
- MKR board
9+
- MKR 485 shield
10+
- ISO GND connected to GND of the Modbus RTU server
11+
- Y connected to A/Y of the Modbus RTU server
12+
- Z connected to B/Z of the Modbus RTU server
13+
- Jumper positions
14+
- FULL set to OFF
15+
- Z \/\/ Y set to ON
16+
17+
created 16 July 2018
18+
by Sandeep Mistry
19+
*/
20+
21+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
22+
#include <ArduinoModbus.h>
23+
24+
void setup() {
25+
Serial.begin(9600);
26+
while (!Serial);
27+
28+
Serial.println("Modbus RTU Client Toggle");
29+
30+
// start the Modbus RTU client
31+
if (!ModbusRTUClient.begin(9600)) {
32+
Serial.println("Failed to start Modbus RTU Client!");
33+
while (1);
34+
}
35+
36+
// send requests to (slave) ID 1
37+
ModbusRTUClient.setId(1);
38+
}
39+
40+
void loop() {
41+
// set value of coil at address 0x00 to 0x01
42+
if (!ModbusRTUClient.writeCoil(0x00, 0x01)) {
43+
Serial.print("Failed to write coil! ");
44+
Serial.println(ModbusRTUClient.lastError());
45+
}
46+
47+
// wait for 1 second
48+
delay(1000);
49+
50+
// set value of coil at address 0x00 to 0x00
51+
if (!ModbusRTUClient.writeCoil(0x00, 0x00)) {
52+
Serial.print("Failed to write coil! ");
53+
Serial.println(ModbusRTUClient.lastError());
54+
}
55+
56+
// wait for 1 second
57+
delay(1000);
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Modbus RTU Server LED
3+
4+
This sketch creates a Modbus RTU Server with a simulated coil.
5+
The value of the simulated coil is set on the LED
6+
7+
Circuit:
8+
- MKR board
9+
- MKR 485 shield
10+
- ISO GND connected to GND of the Modbus RTU server
11+
- Y connected to A/Y of the Modbus RTU client
12+
- Z connected to B/Z of the Modbus RTU client
13+
- Jumper positions
14+
- FULL set to OFF
15+
- Z \/\/ Y set to OFF
16+
17+
created 16 July 2018
18+
by Sandeep Mistry
19+
*/
20+
21+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
22+
#include <ArduinoModbus.h>
23+
24+
const int ledPin = LED_BUILTIN;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
29+
Serial.println("Modbus RTU Server LED");
30+
31+
// start the Modbus RTU server
32+
if (!ModbusRTUServer.begin(1, 9600)) {
33+
Serial.println("Failed to start Modbus RTU Server!");
34+
while (1);
35+
}
36+
37+
// configure the LED
38+
pinMode(ledPin, OUTPUT);
39+
digitalWrite(ledPin, LOW);
40+
41+
// configure a single coil at address 0x00
42+
ModbusRTUServer.configureCoils(0x00, 1);
43+
}
44+
45+
void loop() {
46+
// poll for Modbus RTU requests
47+
ModbusRTUServer.poll();
48+
49+
// read the current value of the coil
50+
int coilValue = ModbusRTUServer.readCoil(0x00);
51+
52+
if (coilValue) {
53+
// coil value set, turn LED on
54+
digitalWrite(ledPin, HIGH);
55+
} else {
56+
// coild value clear, turn LED off
57+
digitalWrite(ledPin, LOW);
58+
}
59+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
WiFi 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+
- MKR1000 or MKR WiFi 1010 board
9+
10+
created 16 July 2018
11+
by Sandeep Mistry
12+
*/
13+
14+
#include <SPI.h>
15+
#include <WiFiNINA.h> // for MKR WiFi 1010
16+
// #include <WiFi101.h> // for MKR1000
17+
18+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19+
#include <ArduinoModbus.h>
20+
21+
#include "arduino_secrets.h"
22+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
23+
char ssid[] = SECRET_SSID; // your network SSID (name)
24+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
25+
int keyIndex = 0; // your network key Index number (needed only for WEP)
26+
27+
int status = WL_IDLE_STATUS;
28+
29+
WiFiClient wifiClient;
30+
ModbusTCPClient modbusTCPClient(wifiClient);
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+
Serial.println("Modbus TCP Client Toggle");
42+
43+
// attempt to connect to Wifi network:
44+
while (status != WL_CONNECTED) {
45+
Serial.print("Attempting to connect to SSID: ");
46+
Serial.println(ssid);
47+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
48+
status = WiFi.begin(ssid, pass);
49+
50+
// wait 10 seconds for connection:
51+
delay(10000);
52+
}
53+
54+
// you're connected now, so print out the status:
55+
printWifiStatus();
56+
57+
// start the Modbus RTU client
58+
if (!modbusTCPClient.begin(server)) {
59+
Serial.println("Failed to start Modbus TCP Client!");
60+
while (1);
61+
}
62+
}
63+
64+
void loop() {
65+
// set value of coil at address 0x00 to 0x01
66+
if (!modbusTCPClient.writeCoil(0x00, 0x01)) {
67+
Serial.print("Failed to write coil! ");
68+
Serial.println(modbusTCPClient.lastError());
69+
}
70+
71+
// wait for 1 second
72+
delay(1000);
73+
74+
// set value of coil at address 0x00 to 0x00
75+
if (!modbusTCPClient.writeCoil(0x00, 0x00)) {
76+
Serial.print("Failed to write coil! ");
77+
Serial.println(modbusTCPClient.lastError());
78+
}
79+
80+
// wait for 1 second
81+
delay(1000);
82+
}
83+
84+
void printWifiStatus() {
85+
// print the SSID of the network you're attached to:
86+
Serial.print("SSID: ");
87+
Serial.println(WiFi.SSID());
88+
89+
// print your WiFi shield's IP address:
90+
IPAddress ip = WiFi.localIP();
91+
Serial.print("IP Address: ");
92+
Serial.println(ip);
93+
94+
// print the received signal strength:
95+
long rssi = WiFi.RSSI();
96+
Serial.print("signal strength (RSSI):");
97+
Serial.print(rssi);
98+
Serial.println(" dBm");
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
WiFi 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+
- MKR1000 or MKR WiFi 1010 board
9+
10+
created 16 July 2018
11+
by Sandeep Mistry
12+
*/
13+
14+
#include <SPI.h>
15+
#include <WiFiNINA.h> // for MKR WiFi 1010
16+
// #include <WiFi101.h> // for MKR1000
17+
18+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19+
#include <ArduinoModbus.h>
20+
21+
#include "arduino_secrets.h"
22+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
23+
char ssid[] = SECRET_SSID; // your network SSID (name)
24+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
25+
int keyIndex = 0; // your network key Index number (needed only for WEP)
26+
27+
const int ledPin = LED_BUILTIN;
28+
29+
int status = WL_IDLE_STATUS;
30+
31+
WiFiServer wifiServer(502);
32+
33+
ModbusTCPServer modbusTCPServer;
34+
35+
void setup() {
36+
//Initialize serial and wait for port to open:
37+
Serial.begin(9600);
38+
while (!Serial) {
39+
; // wait for serial port to connect. Needed for native USB port only
40+
}
41+
42+
Serial.println("Modbus TCP Server LED");
43+
44+
// attempt to connect to Wifi network:
45+
while (status != WL_CONNECTED) {
46+
Serial.print("Attempting to connect to SSID: ");
47+
Serial.println(ssid);
48+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
49+
status = WiFi.begin(ssid, pass);
50+
51+
// wait 10 seconds for connection:
52+
delay(10000);
53+
}
54+
55+
// you're connected now, so print out the status:
56+
printWifiStatus();
57+
58+
// start the server
59+
wifiServer.begin();
60+
61+
// start the Modbus TCP client
62+
if (!modbusTCPServer.begin()) {
63+
Serial.println("Failed to start Modbus TCP Server!");
64+
while (1);
65+
}
66+
67+
// configure the LED
68+
pinMode(ledPin, OUTPUT);
69+
digitalWrite(ledPin, LOW);
70+
71+
// configure a single coil at address 0x00
72+
modbusTCPServer.configureCoils(0x00, 1);
73+
}
74+
75+
void loop() {
76+
// listen for incoming clients
77+
WiFiClient client = wifiServer.available();
78+
79+
if (client) {
80+
// a new client connected
81+
Serial.println("new client");
82+
83+
// let the Modbus TCP accept the connection
84+
modbusTCPServer.accept(client);
85+
86+
while (client.connected()) {
87+
// poll for Modbus TCP requests, while client connected
88+
modbusTCPServer.poll();
89+
90+
// update the LED
91+
updateLED();
92+
}
93+
94+
Serial.println("client disconnected");
95+
}
96+
}
97+
98+
void updateLED() {
99+
// read the current value of the coil
100+
int coilValue = ModbusRTUServer.readCoil(0x00);
101+
102+
if (coilValue) {
103+
// coil value set, turn LED on
104+
digitalWrite(ledPin, HIGH);
105+
} else {
106+
// coild value clear, turn LED off
107+
digitalWrite(ledPin, LOW);
108+
}
109+
}
110+
111+
void printWifiStatus() {
112+
// print the SSID of the network you're attached to:
113+
Serial.print("SSID: ");
114+
Serial.println(WiFi.SSID());
115+
116+
// print your WiFi shield's IP address:
117+
IPAddress ip = WiFi.localIP();
118+
Serial.print("IP Address: ");
119+
Serial.println(ip);
120+
121+
// print the received signal strength:
122+
long rssi = WiFi.RSSI();
123+
Serial.print("signal strength (RSSI):");
124+
Serial.print(rssi);
125+
Serial.println(" dBm");
126+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

0 commit comments

Comments
 (0)