I am using an LDR sensor to store its readings into the database. My code is as follows:

  #include <SPI.h>
  #include <Ethernet.h>

  const int ledPin = 13;   //the number of the LED pin
  const int ldrPin = A0;  //the number of the LDR pin

  byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  IPAddress ip(192, 168, 1, 177); // ip of arduino

  char server[] = ""; // IMPORTANT: If you are using XAMPP you 
  will have to find out the IP address of your computer 

   // Initialize the Ethernet server library
   EthernetClient client;

   void setup() {

   // Serial.begin starts the serial connection between computer and Arduino
   pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
   pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
   // start the Ethernet connection
   Ethernet.begin(mac, ip);


Next, I wrote the setup code:

 void loop() {

  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value

  //check if the LDR status is <= 300
  //if it is, the LED is HIGH

  if (ldrStatus <=300) {

   digitalWrite(ledPin, HIGH);               //turn LED on

 else {

digitalWrite(ledPin, LOW);          //turn LED off


// Connect to the server (your computer or web page)  
if (client.connect(server, 80)) {
client.print("GET /info.php?"); // This
client.print("value="); // This
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Connection: close"); // Part of the GET request telling the 
server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop();    // Closing connection to server


else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");

// Give the server some time to recieve the data and store it. I used 10 
seconds here. Be advised when delaying. If u use a short delay, the server 
might not capture data because of Arduino transmitting new data too soon.

The problem is that when I try connecting to database, it gives a database connection failed. I have crosschecked the ip address of my computer and have even logged into it using xampp to check if it is working. However, the problem is still persistent.

  • Which shield are you using? – Majenko Apr 7 '18 at 14:09
  • 1
    The problem is in your info.php script. It has nothing to do with the Arduino. – tttapa Apr 7 '18 at 16:16
  • you get "--> connection failed" in Serial Monitor? – Juraj Apr 7 '18 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.