Go Down

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

caspervk

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);
 Serial.begin(9600);
 lcd.init();
 lcd.backlight();
 lcd.clear();
 lcd.setCursor(0,0);
}

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

 lcd.print("Connecting...");
  Serial.println("Connecting...");

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

}

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

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

 while(true){
   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 ++;
       }
       else
       {
         //got what we need here! We can disconnect now
         startRead = false;
         client.stop();
         client.flush();
         Serial.println("Disconnecting...");
         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]
<?php

//session_start();
//include('../scripts/php/connect.php');

//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);

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


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!

wildbill

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");

SurferTim

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.
http://www.arduino.cc/playground/Code/WebClient
Here is a subject on the forum covering php and mysql.
http://arduino.cc/forum/index.php/topic,124289.0.html

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

caspervk

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?

billroy

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 '<'.

-br

SurferTim

#5
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.
http://www.w3schools.com/tags/tag_doctype.asp

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".

caspervk

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() {
 Serial.begin(9600);
 Ethernet.begin(mac, ip, dnsadres);
 delay(2000);
 Serial.println("Ready");
}

char pageAdd[32];
void loop()
{
 if(loopCount < 30)
 {
   delay(1000);
 }
 else
 {
   loopCount = 0;
   sprintf(pageAdd,"/backend/fetchAantalOpen.php",totalCount);
   if(!getPage(server,pageAdd)) Serial.print("Fail ");
   else Serial.print("Pass ");
   totalCount++;
   Serial.println(totalCount,DEC);
 }    

 loopCount++;
}

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

 Serial.print("connecting...");

 if(client.connect(ipBuf,80))
 {
   Serial.println("connected");

   sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
   client.write(outBuf);
 }
 else
 {
   Serial.println("failed");
   return 0;
 }

 int connectLoop = 0;
 
 while(client.connected())
 {
   while(client.available())
   {
     inChar = client.read();
     Serial.write(inChar);
     connectLoop = 0;
   }

   delay(10);
   connectLoop++;
   if(connectLoop > 1000)
   {
     Serial.println();
     Serial.println("Timeout");
     client.stop();
   }
   
 }

 Serial.println();

 Serial.println("disconnecting.");
 client.stop();

 return 1;
}


PHP code
Code: [Select]

//session_start();
//include('../scripts/php/connect.php');
<!DOCTYPE html>
<html>
<body>
$what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);

<? echo '@'.$what_the_arduino_reads.'#'; ?>
</body>
</html>


what i get on my arduino monitor:
Code: [Select]
Ready
connecting...connected
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

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<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>
<hr>
<address>Apache/2 Server at localhost Port 80</address>
</body></html>

disconnecting.
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...

caspervk


Go Up