Hello, I am currently working on an Arduino project to remotely control electrovalves for irrigation purposes. I have made a database which I can access perfectly fine in a local server through the Ethernet Shield, with either static or dhcp IP addresses. However, and in order to save myself some money, I tried to also connect through a world time api and save myself buying jumpers and an rtc module. However, when working with the code I ran into a weird problem.
This is the normal WebClient Ethernet example provided in the Arduino IDE (with altered valid MAC and IP Adresses:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
byte mac[] = { 0x1E, 0xE8, 0x09, 0xE7, 0x56, 0xD8 };
char server[] = "www.worldtimeapi.org"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 33);
IPAddress myDns(192, 168, 1, 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() {
// 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:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET /api/timezone/America/Tegucigalpa HTTP/1.1");
client.println("Host: www.worldtimeapi.org");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
For this example, the code works fine. The IP Address is assigned through DHCP (In this case, it normally assigns to 192.168.1.221 ) and the rest of the code works fine (I get a response from the server, which is printed in the Serial Model. However, and for better control purposes, the IT boss wants the Ethernet Shield to be assigned to a static IP (192.168.1.33), so I comment out this whole section of code:
// start the Ethernet connection:
/* Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}*/
Ethernet.begin(mac, ip, myDns);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
And when running the code again, it does not work. It always prints out connection failed.
I've been reading a lot of posts in the forums for an answer, so, this are bits of information I think are important:
- The IP and MAC addresses are valid, unused addresses. Our IT boss has a record of all used and unused IP Address in our workplace and we double checked the list, even pinged the IP address beforehand it is completely free. And I dont think the MAC address is not valid, since when I insert a used MAC, it doesnt even assign a DHCP IP.
- The API was/is accessible at all times during our testing.
- I tried the original example without changing the server (arduino - Google Search) and it still does not communicate.
- Even when assigning the SAME IP that DHCP does automatically, I get no communication.
- The Ethernet cable is not connected to my PC, is directly connected to a repeater connected an Access Point. My Computer is connected to Wi Fi
- I am very new to Arduino and informatic, so anything you share will help me