So, I'm trying to setup Arduinos to be Modbus TCP/IP clients. I think I'm having a hard time understanding what code I need to have the modbus work.
One I want to have setup with a switch on it that takes the value of the switch and just 'holds' it in a coil. A server on another machine can suck up that data.
The other arduino I want to have a light attached to it, and a single discrete coil. When the coil gets written to by a server, the light comes on and off.
I think the pieces I need in my code to at least do the first one are:
#include <ArduinoModbus.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
ModbusTCPClient modbusTCPClient(ethClient);
slave.begin();
and then when in my looping code I run:
buttonState = digitalRead(switchPin);
if (buttonState == HIGH) {
modbusTCPClient.coilWrite(0x05, 0x01);
} else {
modbusTCPClient.coilWrite(0x05, 0x00);
}
My thought here is that this should:
- Include the right libraries
- Start modbus TCP client
- Start my gizmo up as a slave
- If the switch is high, write a value of 1 to register 5
- If the switch is low, write a value of 0 to register 5
I've included my whole script below - but its first off ugly because I have a bunch of extra testing code in there. Second, it has some logic to read a switch, and turn on a light, and also write the value of the switch to the coil. It does two things at once.
For reference, I'm using
Server Tester on another PC to test and see if this works. I'm connecting to 10.0.1.88 in that app - but I'm confused past that what I should be setting it as. I've been doing 'Holding Register r03/w16' on device ID 1, Starting register 1 and getting errors.
Any help here would be great! Thanks!
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoModbus.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
//trying to set the IP of the shield
byte mac[] = { 0x88, 0xAB, 0xBE, 0x14, 0x9A, 0xED };
IPAddress local_IP(10,0,1,88);
IPAddress gateway(10,0,1,1);
IPAddress subnet(255,255,255,0);
IPAddress PrimaryDns(10,0,1,1);
//Variables for the switch to function
const int ledPin = 2; // LED connected to digital pin 1
const int switchPin = 5; //Switch Input connected to pin 5
int buttonState = 0;
//Stuff needed to make Modbus work
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);
//1 time run
void setup() {
//set pin modes for the 2 pins defined above
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(switchPin, INPUT);
//turn on serial logging
Serial.begin(9600);
while (!Serial) {
;// wait for serial port to connect. Needed for native USB port only
}
// initialize the Ethernet device
Ethernet.begin(mac, local_IP, PrimaryDns, gateway, subnet);
// Init the ModbusTCPSlave object
slave.begin();
delay(5000);
IPAddress ip = Ethernet.localIP();
Serial.print("My IP address: ");
Serial.println(ip);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Starting Loop");
buttonState = digitalRead(switchPin);
Serial.println("Value of switch:");
Serial.print(buttonState);
delay(1000);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
modbusTCPClient.coilWrite(0x05, 0x01);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
modbusTCPClient.coilWrite(0x05, 0x00);
}
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}