Connecting to localhost from Arduino

Viewed 51

I want to send data from arduino to my php file running in the xampp server. But it can't connect to localhost. Shortly, client.connect() gives error.

My code:

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

byte mac[]= { 0x74, 0x12, 0xB3, 0x1D, 0xF3, 0xC7 };
IPAddress ip(192, 168, 1, 35);
IPAddress server(127, 0, 0, 1);
EthernetClient client;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
}

void send(int data) {
  char outbuf[128];
  if(client.connect(server,80)) {  // Problem
    Serial.println("Connected successfully");
    sprintf(outbuf,"POST /dir/get.php?data=%u' HTTP/1.1", data);

    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    Serial.println();

    while(client.connected()) {
       while(client.available()) {
          Serial.write(client.read());
      }
    }
  }
  else {
    Serial.println("Cannot connected :(");
  }

  if(client.connected()) {
    client.stop();
  }
}

void loop() {
  send(1);
  delay(2000);
}

I tried some absurd things but they didn't work. (such as making server ip same with client ip, writing 'localhost' instead of 127.0.0.1, and more) I researched it in the arduino forums but couldn't find the solution I want.

What should I do? or Where is my fault?

0 Answers
Related