Go Down

Topic: connection fail from Arduino to database (Read 264 times) previous topic - next topic

helen1802

Dear all,  I am new either on Arduino, Now I follow all the steps on icreateproject.com about save data in local XAMPP database. http://www.icreateproject.info/uncategorized/arduino-save-data-to-database/

It seems everything fine apart from not be able to save the data.

I am able to write data to the database by php (write_data.php),  and Arduino can purge out the data and display from the local network (169.254.189.141) .

My PC IP is, 192.168.0.13
Ethernet IP is: 169.254.189.138
Ardunio Ethernet localIP is: 169.254.189.141


Except IP address, all my coding exactly same, but connection fail, Can anyone help about the case?



Here is the Arduino code:

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
// Enter the IP address for Arduino

IPAddress ip(169,254,189,141);

int photocellPin = 0;  // Analog input pin on Arduino we connected the SIG pin from sensor
int photocellReading;  // Here we will place our reading

char server[] = "192.168.0.13";
// Initialize the Ethernet server library
EthernetClient client;

void setup() {
 
  // Serial.begin starts the serial connection between computer and Arduino
  Serial.begin(9600);
 
  // start the Ethernet connection
  Ethernet.begin(mac, ip);
   
}

void loop() {
 
  photocellReading = analogRead(photocellPin); // Fill the sensorReading with the information from sensor
 
  // Connect to the server (your computer or web page) 
  if (client.connect(server, 80)) {
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.0.13");
    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");
  }
 
  delay(10000);
}


************
I had tried to change the code IP server "192.168.0.13" to "localhost" or char server[] to IPAddress ip, but all those not work for me.

PaulS

Quote
My PC IP is, 192.168.0.13
The 192.168.xxx.xxx IP addresses are local area network addresses. Only devices with in the 192.168.xxx.xxx set of addresses can access your PC. Your Arduino is not on that local area network, so it can't connect to the server software on it.
The art of getting good answers lies in asking good questions.

helen1802

Thanks, Paul,

unfortunately, I still not able to connect to the database
I tried disconnecting my wireless, which IP was  192.168.0.13, so only Ethernet local Area Connection available, IP is 169.254.189.138, this IP can access to my XAMPP database as well, same IP 169.254.189.141 giving to Arduino, which should be under local network 169.254.189.138.

my original Arduino code
char server[] = "192.168.0.13";
changed to
char server[] = "169.254.189.138";
or IPAddress server(169.254.189.138);

They still not work, connection failed
 :smiley-confuse:  :smiley-confuse:  :smiley-confuse:

helen1802

Oh, now I changed

IPAddress ip(169,254,189,141);
to
byte ip(169,254,189,141);

the" connection failed" disappeared.
but I still not able to save data to my database.


I think should be something wrong below these codes:

if (client.connect(server, 80)) {
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 169.254.189.138");
    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

  }


I had added
Serial.println("connection ok");

inside above code,

on the Serial Monitor show
connection OK




PaulS

Quote
Oh, now I changed

IPAddress ip(169,254,189,141);
to
byte ip(169,254,189,141);
but you still have:
Code: [Select]
    client.println("Host: 169.254.189.138");
The art of getting good answers lies in asking good questions.

Go Up