I'm using an ethernet shield for Arduino to connect it to a socket server (different computer) so that I can receive messages from it to activate some routine. Here is my code (ino file has also been uploaded):
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress serverIP(192,168,1,3);
int serverPort=8887;
EthernetClient client;
//--------------------------------------------------------------------------
int msg;
char newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // rfid tag value
//functions
void readTags()
{
if (RFID.available() > 0)
{
// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.
for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
{
int data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
}
}
//--------------------------------------------------------------------------
void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600);
}
//--------------------------------------------------------------------------
void loop()
{
if (client.connect(serverIP, serverPort))
{
Serial.println("server Conected");//report it to the Serial
Serial.println("reading rfid tag:");
readTags();
Serial.print("rfid tag: ");
Serial.println(newtag);
client.print(newtag);//send the message
}
//--------------------------------------------------------------------------
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
client.stop();
}
}
//--------------------------------------------------------------------------
Now, the problem is when I run the code, it just prints "connection failed" forever. How can I do this? Thanks!
sketch_nov09a.ino (1.48 KB)