2

I am trying to access a PHP file in my server with an Arduino and the Ethernet shield. This file captures the URL parameters "Sensor" and "Value" and stores the read data into a database.

This is my code:

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

EthernetClient client;

byte MACaddress[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte IPaddress[] = {10,0,0,178};
byte DNSserverIPaddress[] = {4,4,4,4};
byte gatewayIPaddress[] = { 10, 0, 0, 100 };
byte subnetMask[] = { 255, 255, 255, 0 };
char serverName[] = "log.server.com";


void setup() {
    Serial.begin(9600);
    Ethernet.begin(MACaddress, IPaddress, DNSserverIPaddress, gatewayIPaddress, subnetMask);
}


void loop()
{
    delay (5000);

    Serial.println("connecting to server...");
    client.connect(serverName, 80);
    Serial.println("making HTTP request...");
    client.println("GET /logger.php?sensor=temp&value=19 HTTP/1.1");
    client.println("HOST: log.server.com");
    client.println();
}

After uploading this code to my Arduino Mega + Ethernet shield, nothing changes in my database...

What is wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fuco
  • 223
  • 3
  • 11
  • Does your arduino connect to the web server at all? Test that by setting up i.e. a python http://docs.python.org/2/library/simplehttpserver.html SimpleHTTPServer and then connecting to that using the arduino. Or check the server logs. And check whether your database does behacve correctly when you call the URL from a "normal" browser from your PC. You'll need to find out which part exactly is broken. – til_b Jul 02 '13 at 11:14
  • If I acces directly to the url with my navigator, it works. – fuco Jul 02 '13 at 11:22
  • Have you read this similar question, where the problem was with DHCP? http://stackoverflow.com/questions/13122958/arduino-ethernet-shield-not-connecting-to-webserver?rq=1 – til_b Jul 02 '13 at 11:47

1 Answers1

1

Well, first you should add a condition check to know if the Arduino think it worked or not:

if (client.connect(...)) { 
    /* Stuff you do on success */
} 
else { 
    Serial.println("failure! :-(") 
}
  • If it does print failure! you got a network configuration problem on the Arduino side. If it does work, the problem is after the Arduino.

Then try opening a server with nc -kl 42000 and change the port you connect to on 42000 in the Arduino sketch, to be sure whether the network connection works.

  • If it does work, then you've got a problem on your host side (the webserver), if it does not, you may have a problem on the network between the Arduino and the host.

You should then try to connect to a server's IP address instead of a fully qualified domain name (FQDN). If that works, it may be the DNS server that is unreachable, and you should try to use 8.8.8.8 instead (or your local network's DNS server).

Also check that the IP address you're using is indeed free of use (and not assigned by a DHCP or used by another computer), as well as the MAC address is really unused... (addresses like {0xDE,0xAD,0xBE,0xEF,0xFE,0xED} tend to be used a lot in hacks...).

My opinion, is that your bug is the DNS server that is unreachable, as there's no DNS resolver opened on 4.4.4.4.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zmo
  • 24,463
  • 4
  • 54
  • 90
  • Done! I found a connection problem ,then I have mounted a local server and it works. The problem is relative to the DHCP. Thanks!!! – fuco Jul 03 '13 at 10:01