Go Down

Topic: arduino with ethernet shield communicating with PHP (Read 1 time) previous topic - next topic


dear programmers,

for my school project i need to request and receive data from my PHP server to my arduino attached with an ethernet shield
With help from the internet i kinda got the idea on how it works, allthough it doesn't really work yet so this is why i'm asking for help: i can't find the problem!
my arduino code:
Code: [Select]

#include <Ethernet.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte dns[] = { 8, 8, 8, 8 };

EthernetClient client;
char server[] = "lifestyle.daandamhuis.nl";

LiquidCrystal_I2C lcd(0x27, 16, 2);

char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup(){
 Ethernet.begin(mac, ip, dns);

void loop(){
 String pageValue = connectAndRead(); //connect to the server and read the output

 Serial.println(pageValue); //print out the findings.

 delay(500); //wait 5 seconds before connecting again

String connectAndRead(){
 //connect to the server


 //port 80 is typical of a www page
 if (client.connect(server, 80)) {
   client.print("GET /backend/fetchAantalOpen.php HTTP/1.0");
   //Connected - Read the page
   return readPage(); //go and read the output
   return "connection failed";


String readPage(){
 //read the page, and capture & return everything between '<' and '>'

 stringPos = 0;
 memset( &inString, 0, 32 ); //clear inString memory

   if (client.available()) {
     char c = client.read();

     if (c == '<' )
     { //'<' is our begining character
       startRead = true; //Ready to start reading the part
     else if(startRead)

       if(c != '>')
       { //'>' is our ending character
         inString[stringPos] = c;
         stringPos ++;
         //got what we need here! We can disconnect now
         startRead = false;
         return inString;




the line: client.print("GET /backend/fetchAantalOpen.php HTTP/1.0");
/backend/ is the folder and fetchAantalOpen.php is the file in that folder i want to use.

my PHP server code:
Code: [Select]


//Vraag ID
//$id = $_GET['id'];

//Query samen stellen om te kijken of er een antwoord bestaat
//$query = "SELECT * FROM unread_awnsers WHERE gid = '" . $id . "'";
//$result = mysql_query($query);
//$count = mysql_num_rows($result);
$what_the_arduino_reads '1'.base_convert(rand(10000,9999999), 1036);


the serial.monitor on Arduino tells me it can connect, but after that it should be printing random words and numbers (coded in PHP) on my serial monitor, but that aint happening. anyone know what i am doing wrong?

thanks for the help!


Looking at a similar example in my own code (copied no doubt), I note that I'm sending a blank line after the get request. Try changing this:
Code: [Select]
    client.print("GET /backend/fetchAantalOpen.php HTTP/1.0");

to this:
Code: [Select]
    client.println("GET /backend/fetchAantalOpen.php HTTP/1.0");


If you have tested it with a web browser, and it displays what you think, then try this code in the playground. It should display the response correctly.
Here is a subject on the forum covering php and mysql.

Maybe when you see the response in text form it will help you parse it.


thanks for the responses. i changed the print to println wildbill suggested and now i get the following line at my serial monitor: !DOCTYPE HTML PUBLIC "-//IETF//D0
i have tried to google this, but i can't find a solution or explanation of this line.

anyone knows?


That gobbledygook is part of the DOCTYPE declaration that is generally found as the first line of a properly written .html file.  The posted text appears to be missing the first character, which is the opening '<'.



Nov 30, 2012, 01:57 pm Last Edit: Nov 30, 2012, 02:04 pm by SurferTim Reason: 1
@billroy: The OP is parsing off the brackets on both ends of that !DOCTYPE statement.

That is why I wanted you to use my code. That is the first line of most HTML docs, and that is what you asked it to parse. The first thing in brackets.

edit: The rest that don't have that !DOCTYPE statement will have <html> as the first line of the body, and you will get "html".


its been a while but i had a busy schedule. i have tried to use your code surferTim, allthough it still doesnt work.

code arduino:
(cus ur code was based on some kind of temperaturesensor i changed some values (maybe i did something wrong?)
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = {192, 168, 1, 177};
//byte subnet[] = {255, 255, 255, 0};
// byte server1(159, 253, 5, 5);

byte dnsadres[] = { 8, 8, 8, 8 };

char server[] = "lifestyle.daandamhuis.nl";

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

void setup() {
 Ethernet.begin(mac, ip, dnsadres);

char pageAdd[32];
void loop()
 if(loopCount < 30)
   loopCount = 0;
   if(!getPage(server,pageAdd)) Serial.print("Fail ");
   else Serial.print("Pass ");


byte getPage(char *ipBuf,char *page)
 char inChar;
 char outBuf[128];



   sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
   return 0;

 int connectLoop = 0;
     inChar = client.read();
     connectLoop = 0;

   if(connectLoop > 1000)



 return 1;

PHP code
Code: [Select]

<!DOCTYPE html>
$what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);

<? echo '@'.$what_the_arduino_reads.'#'; ?>

what i get on my arduino monitor:
Code: [Select]
HTTP/1.1 404 Not Found
Date: Mon, 03 Dec 2012 21:35:25 GMT
Server: Apache/2
Content-Length: 405
Connection: close
Content-Type: text/html; charset=iso-8859-1

<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL /backend/fetchAantalOpen.php was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<address>Apache/2 Server at localhost Port 80</address>

Pass 1

it somehow can't find the url path, but its right. also it says HTTP/1.1 404 Not Found even though i ask HTTP/1.0?
it really doesn't make much sense too me...


Go Up