This commit is contained in:
2022-12-15 15:26:57 +01:00
commit 0939c32c58
56 changed files with 10650 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/*
Ethernet Modbus TCP Client Toggle
This sketch toggles the coil of a Modbus TCP server connected
on and off every second.
Circuit:
- Any Arduino MKR Board
- MKR ETH Shield
created 16 July 2018
by Sandeep Mistry
*/
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);
IPAddress server(192, 168, 1, 10); // update with the IP Address of your Modbus server
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
}
void loop() {
if (!modbusTCPClient.connected()) {
// client not connected, start the Modbus TCP client
Serial.println("Attempting to connect to Modbus TCP server");
if (!modbusTCPClient.begin(server, 502)) {
Serial.println("Modbus TCP Client failed to connect!");
} else {
Serial.println("Modbus TCP Client connected");
}
} else {
// client connected
// write the value of 0x01, to the coil at address 0x00
if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
Serial.print("Failed to write coil! ");
Serial.println(modbusTCPClient.lastError());
}
// wait for 1 second
delay(1000);
// write the value of 0x00, to the coil at address 0x00
if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
Serial.print("Failed to write coil! ");
Serial.println(modbusTCPClient.lastError());
}
// wait for 1 second
delay(1000);
}
}

View File

@ -0,0 +1,116 @@
/*
Ethernet Modbus TCP Server LED
This sketch creates a Modbus TCP Server with a simulated coil.
The value of the simulated coil is set on the LED
Circuit:
- Any Arduino MKR Board
- MKR ETH Shield
created 16 July 2018
by Sandeep Mistry
*/
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer ethServer(502);
ModbusTCPServer modbusTCPServer;
const int ledPin = LED_BUILTIN;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet Modbus TCP Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
ethServer.begin();
// start the Modbus TCP server
if (!modbusTCPServer.begin()) {
Serial.println("Failed to start Modbus TCP Server!");
while (1);
}
// configure the LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// configure a single coil at address 0x00
modbusTCPServer.configureCoils(0x00, 1);
}
void loop() {
// listen for incoming clients
EthernetClient client = ethServer.available();
if (client) {
// a new client connected
Serial.println("new client");
// let the Modbus TCP accept the connection
modbusTCPServer.accept(client);
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
// update the LED
updateLED();
}
Serial.println("client disconnected");
}
}
void updateLED() {
// read the current value of the coil
int coilValue = modbusTCPServer.coilRead(0x00);
if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coild value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}

View File

@ -0,0 +1,106 @@
/*
WiFi Modbus TCP Client Toggle
This sketch toggles the coil of a Modbus TCP server connected
on and off every second.
Circuit:
- MKR1000 or MKR WiFi 1010 board
created 16 July 2018
by Sandeep Mistry
*/
#include <SPI.h>
#include <WiFiNINA.h> // for MKR WiFi 1010
// #include <WiFi101.h> // for MKR1000
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiClient wifiClient;
ModbusTCPClient modbusTCPClient(wifiClient);
IPAddress server(192, 168, 1, 10); // update with the IP Address of your Modbus server
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Modbus TCP Client Toggle");
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
if (!modbusTCPClient.connected()) {
// client not connected, start the Modbus TCP client
Serial.println("Attempting to connect to Modbus TCP server");
if (!modbusTCPClient.begin(server)) {
Serial.println("Modbus TCP Client failed to connect!");
} else {
Serial.println("Modbus TCP Client connected");
}
} else {
// client connected
// write the value of 0x01, to the coil at address 0x00
if (!modbusTCPClient.coilWrite(0x00, 0x01)) {
Serial.print("Failed to write coil! ");
Serial.println(modbusTCPClient.lastError());
}
// wait for 1 second
delay(1000);
// write the value of 0x00, to the coil at address 0x00
if (!modbusTCPClient.coilWrite(0x00, 0x00)) {
Serial.print("Failed to write coil! ");
Serial.println(modbusTCPClient.lastError());
}
// wait for 1 second
delay(1000);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

View File

@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""

View File

@ -0,0 +1,126 @@
/*
WiFi Modbus TCP Server LED
This sketch creates a Modbus TCP Server with a simulated coil.
The value of the simulated coil is set on the LED
Circuit:
- MKR1000 or MKR WiFi 1010 board
created 16 July 2018
by Sandeep Mistry
*/
#include <SPI.h>
#include <WiFiNINA.h> // for MKR WiFi 1010
// #include <WiFi101.h> // for MKR1000
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
const int ledPin = LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer wifiServer(502);
ModbusTCPServer modbusTCPServer;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Modbus TCP Server LED");
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
// start the server
wifiServer.begin();
// start the Modbus TCP server
if (!modbusTCPServer.begin()) {
Serial.println("Failed to start Modbus TCP Server!");
while (1);
}
// configure the LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// configure a single coil at address 0x00
modbusTCPServer.configureCoils(0x00, 1);
}
void loop() {
// listen for incoming clients
WiFiClient client = wifiServer.available();
if (client) {
// a new client connected
Serial.println("new client");
// let the Modbus TCP accept the connection
modbusTCPServer.accept(client);
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
// update the LED
updateLED();
}
Serial.println("client disconnected");
}
}
void updateLED() {
// read the current value of the coil
int coilValue = modbusTCPServer.coilRead(0x00);
if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coild value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

View File

@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""