Hello again,,
I'm using this Ethernet Shield
With Arduino Uno.
Basically, i have some values that I will get from sensors and I want to post them to a database using a PHP script that was pre-uploaded to the database. My PHP script works fine, my problem is only with working with the shield.
I tried to combine this example from arduino with my own HTTP GET and every time i get this response: Failed to obtain an IP address using DHCP
I think my problem is with initializing the ethernet connection, I'm using an ethernet cable in my office that works fine when i connect to my laptop, it basically gives me internet when used with the laptop, and all i want is to make the Arduino capable of connection with the online server to send my data to the database
#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "realtimepv.000webhostapp.com";
String PATH_NAME = "Insert.php?";
int voltage = 10;
int current = 10;
int lux = 10;
String queryString = String("voltage="+String(voltage)+String("¤t=")+String(current)+String("&lux=")+String(lux));
//the voltage must be saved into a variable called voltage and before the querystring
//the current must be saved into a variable called current and before the querystring
//the lux must be saved into a variable called lux and before the querystring
void setup() {
Serial.begin(9600);
// initialize the Ethernet shield using DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
// connect to web server on port 80:
if(client.connect(HOST_NAME, HTTP_PORT)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void loop() {
// put your main code here, to run repeatedly:
}