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