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,183 @@
/*
Modbus RTU Client Kitchen Sink
This sketch creates a Modbus RTU Client and demonstrates
how to use various Modbus Client APIs.
Circuit:
- MKR board
- MKR 485 shield
- ISO GND connected to GND of the Modbus RTU server
- Y connected to A/Y of the Modbus RTU server
- Z connected to B/Z of the Modbus RTU server
- Jumper positions
- FULL set to OFF
- Z \/\/ Y set to ON
created 18 July 2018
by Sandeep Mistry
*/
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
int counter = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus RTU Client Kitchen Sink");
// start the Modbus RTU client
if (!ModbusRTUClient.begin(9600)) {
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
void loop() {
writeCoilValues();
readCoilValues();
readDiscreteInputValues();
writeHoldingRegisterValues();
readHoldingRegisterValues();
readInputRegisterValues();
counter++;
delay(5000);
Serial.println();
}
void writeCoilValues() {
// set the coils to 1 when counter is odd
byte coilValue = ((counter % 2) == 0) ? 0x00 : 0x01;
Serial.print("Writing Coil values ... ");
// write 10 Coil values to (slave) id 42, address 0x00
ModbusRTUClient.beginTransmission(42, COILS, 0x00, 10);
for (int i = 0; i < 10; i++) {
ModbusRTUClient.write(coilValue);
}
if (!ModbusRTUClient.endTransmission()) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
}
// Alternatively, to write a single Coil value use:
// ModbusRTUClient.coilWrite(...)
}
void readCoilValues() {
Serial.print("Reading Coil values ... ");
// read 10 Coil values from (slave) id 42, address 0x00
if (!ModbusRTUClient.requestFrom(42, COILS, 0x00, 10)) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
while (ModbusRTUClient.available()) {
Serial.print(ModbusRTUClient.read());
Serial.print(' ');
}
Serial.println();
}
// Alternatively, to read a single Coil value use:
// ModbusRTUClient.coilRead(...)
}
void readDiscreteInputValues() {
Serial.print("Reading Discrete Input values ... ");
// read 10 Discrete Input values from (slave) id 42, address 0x00
if (!ModbusRTUClient.requestFrom(42, DISCRETE_INPUTS, 0x00, 10)) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
while (ModbusRTUClient.available()) {
Serial.print(ModbusRTUClient.read());
Serial.print(' ');
}
Serial.println();
}
// Alternatively, to read a single Discrete Input value use:
// ModbusRTUClient.discreteInputRead(...)
}
void writeHoldingRegisterValues() {
// set the Holding Register values to counter
Serial.print("Writing Holding Registers values ... ");
// write 10 coil values to (slave) id 42, address 0x00
ModbusRTUClient.beginTransmission(42, HOLDING_REGISTERS, 0x00, 10);
for (int i = 0; i < 10; i++) {
ModbusRTUClient.write(counter);
}
if (!ModbusRTUClient.endTransmission()) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
}
// Alternatively, to write a single Holding Register value use:
// ModbusRTUClient.holdingRegisterWrite(...)
}
void readHoldingRegisterValues() {
Serial.print("Reading Input Register values ... ");
// read 10 Input Register values from (slave) id 42, address 0x00
if (!ModbusRTUClient.requestFrom(42, HOLDING_REGISTERS, 0x00, 10)) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
while (ModbusRTUClient.available()) {
Serial.print(ModbusRTUClient.read());
Serial.print(' ');
}
Serial.println();
}
// Alternatively, to read a single Holding Register value use:
// ModbusRTUClient.holdingRegisterRead(...)
}
void readInputRegisterValues() {
Serial.print("Reading input register values ... ");
// read 10 discrete input values from (slave) id 42,
if (!ModbusRTUClient.requestFrom(42, INPUT_REGISTERS, 0x00, 10)) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.println("success");
while (ModbusRTUClient.available()) {
Serial.print(ModbusRTUClient.read());
Serial.print(' ');
}
Serial.println();
}
// Alternatively, to read a single Input Register value use:
// ModbusRTUClient.inputRegisterRead(...)
}

View File

@ -0,0 +1,55 @@
/*
Modbus RTU Client Toggle
This sketch toggles the coil of a Modbus RTU server connected via RS485
on and off every second.
Circuit:
- MKR board
- MKR 485 shield
- ISO GND connected to GND of the Modbus RTU server
- Y connected to A/Y of the Modbus RTU server
- Z connected to B/Z of the Modbus RTU server
- Jumper positions
- FULL set to OFF
- Z \/\/ Y set to ON
created 16 July 2018
by Sandeep Mistry
*/
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus RTU Client Toggle");
// start the Modbus RTU client
if (!ModbusRTUClient.begin(9600)) {
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
void loop() {
// for (slave) id 1: write the value of 0x01, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x01)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}
// wait for 1 second
delay(1000);
// for (slave) id 1: write the value of 0x00, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x00)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}
// wait for 1 second
delay(1000);
}

View File

@ -0,0 +1,71 @@
/*
Modbus RTU Server Kitchen Sink
This sketch creates a Modbus RTU Server and demonstrates
how to use various Modbus Server APIs.
Circuit:
- MKR board
- MKR 485 shield
- ISO GND connected to GND of the Modbus RTU server
- Y connected to A/Y of the Modbus RTU client
- Z connected to B/Z of the Modbus RTU client
- Jumper positions
- FULL set to OFF
- Z \/\/ Y set to OFF
created 18 July 2018
by Sandeep Mistry
*/
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
const int numCoils = 10;
const int numDiscreteInputs = 10;
const int numHoldingRegisters = 10;
const int numInputRegisters = 10;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus RTU Server Kitchen Sink");
// start the Modbus RTU server, with (slave) id 42
if (!ModbusRTUServer.begin(42, 9600)) {
Serial.println("Failed to start Modbus RTU Server!");
while (1);
}
// configure coils at address 0x00
ModbusRTUServer.configureCoils(0x00, numCoils);
// configure discrete inputs at address 0x00
ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);
// configure holding registers at address 0x00
ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
// configure input registers at address 0x00
ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
}
void loop() {
// poll for Modbus RTU requests
ModbusRTUServer.poll();
// map the coil values to the discrete input values
for (int i = 0; i < numCoils; i++) {
int coilValue = ModbusRTUServer.coilRead(i);
ModbusRTUServer.discreteInputWrite(i, coilValue);
}
// map the holding register values to the input register values
for (int i = 0; i < numHoldingRegisters; i++) {
long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);
ModbusRTUServer.inputRegisterWrite(i, holdingRegisterValue);
}
}

View File

@ -0,0 +1,61 @@
/*
Modbus RTU Server LED
This sketch creates a Modbus RTU Server with a simulated coil.
The value of the simulated coil is set on the LED
Circuit:
- MKR board
- MKR 485 shield
- ISO GND connected to GND of the Modbus RTU server
- Y connected to A/Y of the Modbus RTU client
- Z connected to B/Z of the Modbus RTU client
- Jumper positions
- FULL set to OFF
- Z \/\/ Y set to OFF
created 16 July 2018
by Sandeep Mistry
*/
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
const int ledPin = LED_BUILTIN;
void setup() {
Serial.begin(9600);
Serial.println("Modbus RTU Server LED");
// start the Modbus RTU server, with (slave) id 1
if (!ModbusRTUServer.begin(1, 9600)) {
Serial.println("Failed to start Modbus RTU Server!");
while (1);
}
// configure the LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// configure a single coil at address 0x00
ModbusRTUServer.configureCoils(0x00, 1);
}
void loop() {
// poll for Modbus RTU requests
int packetReceived = ModbusRTUServer.poll();
if(packetReceived) {
// read the current value of the coil
int coilValue = ModbusRTUServer.coilRead(0x00);
if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coil value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}
}

View File

@ -0,0 +1,65 @@
/*
Modbus RTU Temperature Sensor
This sketch shows you how to interact with a Modbus RTU temperature and humidity sensor.
It reads the temperature and humidity values every 5 seconds and outputs them to the
serial monitor.
Circuit:
- MKR board
- Winners® Modbus RS485 Temperature and Humidity:
https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN
- External 9-36 V power Supply
- MKR 485 shield
- ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-
- Power supply V+ connected to V+ sensor
- Y connected to A/Y of the Modbus RTU sensor
- Z connected to B/Z of the Modbus RTU sensor
- Jumper positions
- FULL set to OFF
- Z \/\/ Y set to ON
created 8 August 2018
by Riccardo Rizzo
*/
#include <ArduinoModbus.h>
float temperature;
float humidity;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus Temperature Humidity Sensor");
// start the Modbus RTU client
if (!ModbusRTUClient.begin(9600)) {
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
void loop() {
// send a Holding registers read request to (slave) id 1, for 2 registers
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) {
Serial.print("failed to read registers! ");
Serial.println(ModbusRTUClient.lastError());
} else {
// If the request succeeds, the sensor sends the readings, that are
// stored in the holding registers. The read() method can be used to
// get the raw temperature and the humidity values.
short rawtemperature = ModbusRTUClient.read();
short rawhumidity = ModbusRTUClient.read();
// To get the temperature in Celsius and the humidity reading as
// a percentage, divide the raw value by 10.0.
temperature = rawtemperature / 10.0;
humidity = rawhumidity / 10.0;
Serial.println(temperature);
Serial.println(humidity);
}
delay(5000);
}

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 ""