Send GET PHP to XAMPP not working

Hello all,

I’m trying send GET PHP to MySQL (XAMPP), this is RFID system to store UID with username and password, the XAMPP is installed on local LAN, but still cannot success, I already tried GET URL: http://192.168.1.83/savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567 on browser, it work! please see the following Ardunio first:

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
// Enter the IP address for Arduino, as mentioned we will use 192.168.1.177
IPAddress ip(192,168,1,177);

char server[] = "192.168.1.83"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")

// 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() {
 

  // Connect to the server (your computer or web page)  
  if (client.connect(server, 80)) {
    client.print("GET /savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567"); // This
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.1.83"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    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.
  delay(10000);
}

PHP code:

<?php

include("db.php");

$uid = $_GET['uid'];
$username = $_GET['username'];
$password = $_GET['password'];

$result = mysqli_query($conn, "insert into user (uid, username, password) values ('$uid', '$username', '$password')");

?>

Thank all!

Just checked the serial console displaying "--> connection failed"

Just checked Apache side has 408 bad request.

Try using an example which attempts to get an IP address from the gateway: https://www.arduino.cc/en/Tutorial/WebClient

I'd also, for a test, remove the embedded blanks in the uid you are submitting.

I guess everything is in the same network: The arduino/ethernet card, the server, the pc you issued the test URL on etc.

    client.print("GET /savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567"); // This

GET requests do NOT have spaces in them. You must replace the space with &20.

Making a GET request, and then ignoring what the server says is not a smart thing to do. Would you fire up google, and ask it to search for something, if you weren't interested in the results? Well, why are you having the Arduino make a request and ignoring the reply?

PaulS:
. .
GET requests do NOT have spaces in them. You must replace the space with &20.
. . .

In some conventions, a %20 is also accepted as valid substitute for a blank.

I think same LAN no need gateway, right?

The space already tested on brower, is it different on Arduino?

I have searched Google already before come to here, someone said have to add \r\n before HTTP/1.1 etc, but still have this problem.

Thanks

leiw: . . . The space already tested on brower, is it different on Arduino? . . .

Instead of posing rhetorical questions, why not try it ? I guess the browser does a string substitution before forming its GET command.

6v6gt:
Instead of posing rhetorical questions, why not try it ?
I guess the browser does a string substitution before forming its GET command.

No space still not work.

OK. When you issued the following command in a browser, was the browser running on the XAMPP server itself ? Or did you do it from a completely separate PC ?

http://192.168.1.83/savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567

Are all the network devices ( arduino with ethernet card, XAMPP server, maybe seperate PC for the browser test ? ) connected directly to a router ? Can you login to the LAN router and see all the connected devices ?

6v6gt:
In some conventions, a %20 is also accepted as valid substitute for a blank.

Thank you. Damned fat fingers…

6v6gt: OK. When you issued the following command in a browser, was the browser running on the XAMPP server itself ? Or did you do it from a completely separate PC ?

http://192.168.1.83/savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567

Are all the network devices ( arduino with ethernet card, XAMPP server, maybe seperate PC for the browser test ? ) connected directly to a router ? Can you login to the LAN router and see all the connected devices ?

  1. separate PC.

  2. yes, if not same network, I can't see 408 request timeout on XAMPP, ping is work to Ethernet Shield from PC, not a router problem.

If you have an internet connection from your LAN, you can test if there is a problem with your Ethernet card/arduino by the following:

A) change your sketch so it is based on https://www.arduino.cc/en/Tutorial/WebClient . This also makes the data sent back from the webserver visible in the serial console.

B) replace the hostname of your xampp server in the sketch with "wifitest.adafruit.com" and change the url string to "/testwifi/index.html" . You should see this in the serial console "This is a test of the CC3000 module! If you can read this, its working". It is the same message as if you would enter http://wifitest.adafruit.com/testwifi/index.html in a web browser.

If that works, you must suspect your xampp server (access restriction, firewall rule etc.)-

OK, I can see correct message:

connecting...
connected
HTTP/1.1 200 OK
Server: nginx/1.10.0 (Ubuntu)
Date: Wed, 24 Jan 2018 03:50:30 GMT
Content-Type: text/html
Content-Length: 73
Last-Modified: Thu, 16 Feb 2017 17:42:29 GMT
Connection: close
ETag: "58a5e485-49"
Accept-Ranges: bytes

This is a test of the CC3000 module!
If you can read this, its working :)
disconnecting.

But I changed the domain and GET URL to XAMMP server 192.168.1.83, I got no message:

Arduino code:

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

// 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(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "192.168.1.83";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// 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;

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:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /rfid/savedata.php?uid=DQ12FA00&username=sam&password=123456");
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.83");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

Console message:

connecting...
connected
H
disconnecting.

OK, I didn't change the port number, and the UID cannot have spaces, now it work!

HTTP/1.1 200 OK
Date: Wed, 24 Jan 2018 05:25:57 GMT
Server: Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.1
X-Powered-By: PHP/7.2.1
Content-Length: 7
Connection: close
Content-Type: text/html; charset=UTF-8

But now I have to solve UID with spaces, just tried %20 not work.

Thank all.

leiw: OK, I didn't change the port number, and the UID cannot have spaces, now it work! . . .

The port number ? So the URL which you mentioned in post 1 as working . . .

http://192.168.1.83/savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567 on browser, it work!

was more like say:

http://192.168.1.83:8080/savedata.php?uid=DD 12 67 PL&username=oliver&password=1234567

or whatever the port number is.

anyway, good that it is now fixed.

char server[] = "192.168.1.83";    // name address for Google (using DNS)

I KNOW, with absolute certainty, that that is NOT google's IP address. Delete stupid comments when you change the value.

The server NAME has nothing to do with the IP address, in the code. The IP address is NOT a string.

But now I have to solve UID with spaces, just tried %20 not work.

Why? You are sending 4 values, separated by spaces. You could as easily send 4 values separated by commas, semicolons, colons, pipe symbols, etc. Don't make life more difficult than it needs to be.