Go Down

Topic: can not connect to website with EthernetClient (Read 334 times) previous topic - next topic

palassi123

Hello, I 've been searching and trying through the Internet more than six hours and I don't know what elese to try. I am trying with Ehternet shield to connect to my website and with php to send data to a db. I get an IP adrees but the problem is I cannot connect to the website. I tried instead www.google.com , but still nothing. My code is as follows

Code: [Select]

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


byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);


const int temperaturePin = A0;
float temperature; 


char pageName[] = "www.google.com";

// Initialize the Ethernet server library
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
  }

  Ethernet.begin(mac, ip);
  // start the Ethernet connection:
 
 
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      }
      else {
        Serial.println("Ethernet shield OK.");
        if (Ethernet.linkStatus() == LinkOFF) {
            Serial.println("Ethernet cable is not connected.");
        }
        else {
            Serial.println("Ethernet cable is connected.");
        }
      }

  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
 
  temperature =  getDegrees(temperaturePin); //Measure the voltage at the analog pin


  int code = client.connect(pageName, 80);
  Serial.print(code);

 
  if (code) {
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(int(temperature)); // 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: www.google.com"); // 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);
}


float getDegrees(int pin)   //Function to read and return
             
{
    float temp = analogRead(pin);
    return (temp * 0.48828125);
}

 

Juraj

#1
Nov 17, 2019, 06:51 pm Last Edit: Nov 17, 2019, 06:52 pm by Juraj
it prints "--> connection failed"?

what IP address? the one in sketch? .177?


Juraj

Yes and Yes
the you are not connected to network. it only shows the self configured IP address

palassi123

What can I do to be sure that I am connected?

To add, there was no sticker with a mac address on my ethernet shield.

palassi123

Moreover, i connected to the router and I show this same Ip reserved we this general mac address on the LAN network.

mikb55

#6
Nov 18, 2019, 12:39 pm Last Edit: Nov 18, 2019, 12:39 pm by mikb55
Does your Ethernet adaptor have any diagnostic LEDs?
Some have an LED to show the link status and another for network activity (flashes intermittently). Some have Tx and Rx indicators.

palassi123

Both TX and RXare flashing. Rx more often than tx.

palassi123

There ia a LINK led that is constantly on.

mikb55

#9
Nov 18, 2019, 01:54 pm Last Edit: Nov 18, 2019, 01:55 pm by mikb55
Install the Wireshark program on a computer that is on the same network as your Arduino.
In the Arduino program change the destination to point to the computer's IP address.
Run Wireshark and check for data arriving from the IP address assigned to the Arduino.

palassi123

All leds seems to be OK

Power - Power -ON
LINK  - Always ON when link is ok and flashes while in a TX or RX stat -ON
100M - Light indicates the link speed is 100Mbps -ON
FULLD - Light indicates the status of full-duplex mode. -ON
Coll  - Light indicates the presence of collision activity - OFF
Rx  - Light indicates the presence of receiving activity - FLASHING
Tx  - Light indicates the presence of transmitting activity -FLASHING

Juraj



palassi123

Install the Wireshark program on a computer that is on the same network as your Arduino.
In the Arduino program change the destination to point to the computer's IP address.
Run Wireshark and check for data arriving from the IP address assigned to the Arduino.

REplacing  code with  my ip

  int code = client.connect(myip, 80);
  Serial.print(code);

returns code 1, so it can connect

mikb55

Does the Wireshark  display show any packets from the Arduino?


Go Up