I'm trying to connect to a device using an ethernet cable and the device's fixed IP.
When I've tried to connect to another device using DHCP over the server I was able to get a connection but now I am unsuccessful.
This is the sketch I'm using:
#include <SPI.h>
#include <EthernetClient.h>
#include <Ethernet.h>
#define DEBUG
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,137,5); // numeric IP for Google (no DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 137, 5);
IPAddress myDns(192, 168, 137, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
pinMode(5, OUTPUT); //set pwm fan
digitalWrite(5, HIGH);
// 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
}
// start the Ethernet connection:
#ifdef DEBUG
Serial.println("Initialize Ethernet with DHCP:");
#endif
if (Ethernet.begin(mac, ip, myDns) == 0) {
delay(1000);
#ifdef DEBUG
Serial.println("Failed to configure Ethernet using DHCP");
#endif
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
#ifdef DEBUG
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
#endif
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
#ifdef DEBUG
Serial.println("Ethernet cable is not connected.");
#endif
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
#ifdef DEBUG
Serial.print("DHCP assigned IP ");
Serial.println(Ethernet.localIP());
#endif
}
// give the Ethernet shield a second to initialize:
delay(1000);
#ifdef DEBUG
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
#endif
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
#ifdef DEBUG
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
#endif
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
beginMicros = micros();
}
void loop() {
if (client.connected()){
#ifdef DEBUG
Serial.println("Decipher is connected");
#endif
}
if (!client.connected()){
#ifdef DEBUG
Serial.println("Decipher is not connected");
#endif
digitalWrite(5,LOW);
delay(1000);
digitalWrite(5,HIGH);
}
delay(7000);
}