Arduio as a wifi client, Unable to connect to Port 80

Hello

I tried WIFI Library example code "WiFi Web Client"
The original code connects to google.com. This worked perfectly fine on my hardware . I can see that in Serial Port.

I changed the code such that it connect to my localhost at Port 80, but it FAILS.
Because my overall goal is that the Arduino will send a floating point value to my server and a script on my server will post it to database.

SO I installed MySQL database, Apache webserver, PHP and PHPMyAdmin and configured everything.

And I checked that my Apache server is running. And in the httpd conf file
Listen to Port 80 is uncommented. Also I used ipconfig to get my ip address and changed the Example code to use my ip address , instead of google's ip address.

So I changed this code to connect to my localhost server (configured through Apache). Also if I type my id address in a browser , it shows me my index.html file. I also tested by typing in 127.0.0.1 that also shows me my index.html file.

How do I go about debugging the error that why it is not able to connect to my localhost.

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using a WiFi shield.
 
 This example is written for a network using WPA encryption. For 
 WEP or WPA, change the Wifi.begin() call accordingly.
 
 This example is written for a network using WPA encryption. For 
 WEP or WPA, change the Wifi.begin() call accordingly.
 
 Circuit:
 * WiFi shield attached
 
 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe
 */


#include <SPI.h>
#include <WiFi.h>

char ssid[] = "MYWIFINAME"; //  your network SSID (name) 
char pass[] = "MYWIFIPASSWORD";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// 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(192,138,26,1);  // numeric IP for my server (no DNS)
//char server[] = "";    // name address for my server
// 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):
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
  
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host:localhost");
    client.println("Connection: close");
    client.println();
  }
}

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

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

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


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

My code shows everything until "Starting connection to server..", but after that it just shows
"disconnecting from server."

It does not show "connected to the server".

Thanks

Are you sure this ip address is correct?

IPAddress server(192,138,26,1);

Most localnet ips are 192.168.x.x. Just checking.

Yes correct, I just changed that before posting to forum. but yes its in the format you mentioned

Thanks

What happens if you type this URL into the browser?

http://192.138.26.1/search?q=arduino

It says
Not Found

The requested URL /search was not found on this server.

What does this do in the code appending this string to URL?

"/search?q=arduino"

To test , I created a folder named "search" under my websites folder and place a file arduino.txt in it.

and Now this url
http://192.138.26.1/search?q=arduino

takes me to

"Index of /search"

and shows me my file under it.

Hmm...may be now if I upload my code and try......

timon1:
Yes correct, I just changed that before posting to forum. but yes its in the format you mentioned

What does that mean? Why would you change that? It is a private ip. The computer I am on is 192.168.1.254. You can't get here from there. It is behind my router and firewall.

You must insure the ip address assigned to your server is within your localnet. Otherwise, it will be unreachable. PeterH gave you the test for that, and it sounds like it failed.

Are you sure this is a correct, valid ip for your network?

192.168.56.1

Do all the other devices on your network have a 192.168.56.x ip address?

Are you trying this using a localnet computer, or on the server?

and Now this url
http://192.168.56.1/search/?q=arduino

Are you sure this is a correct, valid ip for your network?

yes

Are you trying this using a localnet computer, or on the server?

If I understand your question correctly the answer is this is a localnet computer.

Because I am just trying it on my PC.

Thanks

Did you fix the IPAddress code in your sketch?

// change this
IPAddress server(192,138,26,1);
// to this
IPAddress server(192,168,26,1);

I always have been trying with correct ip that is my ip.

thanks

So that is a public ip? You are near Sierra Vista, AZ? Fort Huachuca? If so, my bad. I thought that was a private network ip.

edit: I guess you have me firewalled out. I can't reach that public ip from Florida. :frowning:

Oh no, I confused you.

When I said

Yes correct, I just changed that before posting to forum. but yes its in the format you mentioned

I meant you are correct that my localnet ip is of the format you mentioned, and you mentioned.

Most localnet ips are 192.168.x.x. Just checking.

so yes my localnet ip is like 192.168.X.X as you mentioned.

I'm not sure that I am the one who is confused. This is what you posted earlier. Do you see the ip you used here?

timon1:
To test , I created a folder named "search" under my websites folder and place a file arduino.txt in it.

and Now this url
http://192.138.26.1/search?q=arduino

takes me to

"Index of /search"

and shows me my file under it.

Hmm...may be now if I upload my code and try......

Do you see the ip address you used here?

IPAddress server(192,138,26,1);  // numeric IP for my server (no DNS)

edit: What I am trying to say is you may have the wrong ip assigned to your server. If you tried accessing that ip from your server web browser, it would work with the wrong ip for your localnet. But other computers will not be able to access that, especially you Arduino. It will almost certainly pick up a 192.168.x.x 255.255.255.0 ip/subnet, and the 192.138.26.1 ip would not be localnet. That request would be passed to the router gateway to forward out onto the internet.

timon1:
Yes correct, I just changed that before posting to forum.

I'd like you to make absolutely clear what your web server's IP address is. Is it 192.138.26.1 or 192.168.26.1?

Your sketch appears to print out the Arduino's IP address. What address does it print?

Oh I see that

it is
192.168.26.1

With using the correct ip (as above) I am unable to connect to port 80

and appending /search?q=arduino take me to my folder "search" that i created

Does your wifi gizmo connect to a router, and if so, does logging into the router does the router show the wifi being connected? Also, if using a router, what is the router's lan ip address?

Hi Zoomcat

My Arduino wifi shield successfully connects to the local wifi that I have set up.

I am not very sure if I answer your question correctly regarding router lans ip address. But an ipconfig shows 192.168.2.1