hello
need help my simple code to send digital signals to server on modbus tcp ip through router
i write my code and no errors changed cable twice cat 6 the lamp green is on also ping to arduino ip is ok from pc i used hercules to test tcp server but the ere is error to connect server
/*
TCPIPClient
Write sine wave data values to the tcpserver object created in MATLAB.
*/
#include <SPI.h>
#include <Ethernet.h>
// Specify the MAC address printed on the Ethernet shield.
// If no MAC address is printed, then use the address shown below.
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
// Specify the server IP address that is used to create the tcpserver object in MATLAB.
// This is the IP address of the router or network adapter that the Arduino Ethernet Shield is connected to.
// In this example, 192.168.1.81 is the IP address for the server.
IPAddress server(192,168,1,81);
// Set the static IP address for the Arduino Ethernet Shield to act as a TCP/IP client.
// Choose an IP address that is in the same subnet or private network as the server IP address.
// In this example, 192.168.1.177 is the IP address for the Arduino Ethernet Shield. It is in the same subnet as the server IP address.
IPAddress ip(192,168,1,177);
IPAddress myDns(192,168,1,81);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
// Ethernet client library.
EthernetClient client;
// Command sent by the server.
byte command;
// digital inputs from fire panel to arduino
const int door_1=7;
const int fire_1=8;
const int battery_1=9;
char x="dooropend";
char y="firealarm";
char z="batterylow";
int state1 = 0;
int state2 = 0;
int state3 = 0;
// The setup routine runs once when you press reset.
void setup()
{
// Initialize serial communication.
Serial.begin(9600);
while (!Serial)
{11111
; // Wait for serial port to connect.
}
Ethernet.begin(mac, ip, myDns, gateway, subnet);
delay(1000);
Serial.print("Manually assigned the following IP address to the Arduino:");
Serial.println();
Serial.println(Ethernet.localIP());
// Check for Ethernet hardware.
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found.");
}
// Check for Ethernet cable connection.
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
}
Serial.print("Attempting connection to ");
Serial.print(server);
Serial.println("...");
// Attempt to connect to the server running at IP address 192.168.1.81 and port 5000.
if (client.connect(server,10002))
{
Serial.print("Connected to server running at ");
Serial.println(client.remoteIP());
}
else
{
Serial.println("Connection to server failed.");
}
// set contacts to arduino
pinMode(door_1, INPUT);
pinMode(fire_1, INPUT);
pinMode(battery_1, INPUT);
}
// Main processing loop
void loop() {
// Block until data is sent by server.
if (client.available() > 0)
{
// Read the command sent by the server.
command = client.read();
// Print the command sent by the server.
Serial.println("The server sent the following command:");
Serial.println(command);
if (client.connected() && command == 1)
{
// send status
state1 = digitalRead(door_1);
if (state1 == HIGH) {
client.write ("x");
delay(1000);
}
state2 = digitalRead(fire_1);
if (state2 == HIGH) {
client.write ("y");
delay(1000);
}
state3 = digitalRead(battery_1);
if (state3 == HIGH) {
client.write ("z");
delay(1000);
}
}
}
}
/*
also i tried test code for w5100 and ok serial gave me the following
Manually assigned the following IP address to the Arduino:
192.168.1.177
Attempting connection to 192.168.1.81...
Connection to server failed.