Go Down

Topic: Ethernet shield PHP communication (Read 2070 times) previous topic - next topic

C220amg

Hello Arduino fans!

Currently I'm working on a new project which should be able of sending data to a mysql database through a php code. The connection between the php and the mysql is no problem, I am able to add content to my database by writing the data in my url bar. i.e. http://***********.com/add_db.php?id=12&name=john

Though, the connection between my arduino and the php code on the server side seems to me impossible, I have already searched on the forum and tried several codes and modified them a bit, especially those of zoomkat. Unfortunatelly, it seems impossible to make this work. I am currently experimenting with the following code, which is made by zoomkat:

Code: [Select]
//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = {0x90, 0xA2, 0xDA, 0x**, 0x**, 0x** };
byte ip[] = { ***, ***, ***, *** };
byte server[] = { **,**,**,**};

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    //client.println("GET /arduino.txt HTTP/1.0");
    client.println("GET /add_db.php?id12&name=john HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
   
   
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}


Thanks, Niels

SurferTim

Try the code in this post.
http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629

The getPage() call takes two parameters. The first is the server ip, and the second is the page. The page requested is the default page, but you could change that to your php code page.

Code: [Select]
getPage(server,"/mypage.php?test=1")

Kuzito

Hi,

I think in your Arduino code is error:
"client.println("GET /add_db.php?id12&name=john HTTP/1.0");"

PHP code waiting for "add_db.php?id=12&name=john"

C220amg


Try the code in this post.
http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629

The getPage() call takes two parameters. The first is the server ip, and the second is the page. The page requested is the default page, but you could change that to your php code page.

Code: [Select]
getPage(server,"/mypage.php?test=1")



Thanks but I can't get your code to work properly, it keeps telling me the connection failed.

SurferTim

Did you change the network settings to your localnet?

C220amg

Yeah you're right, i forgot to change my gateway, it connects to my website! Still, my php page isn't loaded by just replacing the %s path with mine right?


SurferTim

This was just an example, but close to what you need.

The %s is replaced by the "page" variable passed to that function. In the loop, look at the getPage() function. The second parameter should be replaced with the page and variables you want to send. You will need to construct that request to meet your requirements.

C220amg


This was just an example, but close to what you need.

The %s is replaced by the "page" variable passed to that function. In the loop, look at the getPage() function. The second parameter should be replaced with the page and variables you want to send. You will need to construct that request to meet your requirements.



Hmm, i'm still a noob when it comes to coding and understanding what's written down but I did add my page but it won't work at all.

This is the code within the loop though, this is when the page can't be reached I guess?

Code: [Select]
void loop()
{
  if(loopCount < 10)
  {
    delay(1000);
  }
  else
  {
    loopCount = 0;

    if(!getPage(server,"/add_db.php?id=12&name=john")) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }

SurferTim

What happens if you try this from a web browser?
http://server/add_db.php?id=12&name=john
Replace "server" with the server ip in your code. Do you get a response?

Insure there is a page with the name add_db.php in your server's home directory.

C220amg

#9
Apr 12, 2012, 11:02 pm Last Edit: Apr 12, 2012, 11:07 pm by C220amg Reason: 1

What happens if you try this from a web browser?
http://server/add_db.php?id=12&name=john
Replace "server" with the server ip in your code. Do you get a response?

Insure there is a page with the name add_db.php in your server's home directory.



I actually am able to connect to my server, though when I try to connect to my php page it tells me it can't find the page, supported by a 404 error code in the serial monitor. And yes, i'm sure the page is on my server.



SurferTim

#10
Apr 12, 2012, 11:29 pm Last Edit: Apr 12, 2012, 11:31 pm by SurferTim Reason: 1
Does your web server use virtual hosting? If you can't access it with the ip (not the domain name), then it  probably is using virtual hosting.

You will probably need to send a Host in the header.

Code: [Select]
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);

C220amg


Does your web server use virtual hosting? If you can't access it with the ip (not the domain name), then it  probably is using virtual hosting.

You will probably need to send a Host in the header.

Code: [Select]
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);



Thats the Holy Grail! I'm now able to add data to the mysql database through php, next step is to make the input a variable related to (in this case an rfid tag). Thus,a RFID tag is read, and the name of the person is put into the db.

Many thanks for your help with the connection to my php.

C220amg

Well, till now it worked though I'm trying to make my requested url variable in order to send data from the RFID. since the input is a char array, I tried to construct it in that way, by making use of strcat to join multiple parameters. Unfortunate enough, my returning char doesn't give any value when
Code: [Select]
Serial.println(url);. It's just blank.

The strcat I've constructed:

Code: [Select]
char* pre_url[]={"/add_db.php?Registratiepunt="};
char* registratiepunt[]={"StalKennetW", "StalKrawall", "Stapmolen", "Dressuurbak"};
char* mid_url1[]={"&Paard="};
char* paard[]={"KennetW", "Krawall"};
char* mid_url2[]={"&Datum="};
char* datum[]={"lipsum"};
char* mid_url3[]={"&Tijd="};
char* tijd[]={"dolor"};
char url[300];  // enough room for all strings together


char* concat(char* pre_url, char* registratiepunt, char* mid_url1, char* paard, char* mid_url2, char* datum, char* mid_url3, char* tijd) {
  url[0] = 0;          // start with a null string:
  strcat(url, pre_url);   // add first string
  strcat(url, registratiepunt);
  strcat(url, mid_url1); 
  strcat (url, paard);
  strcat(url, mid_url2);
  strcat (url, datum);
  strcat(url, mid_url3);
  strcat (url, tijd);

  return url;
}



And when I try to tell the strcat to only get the char which meets to an int value, it tells me it isn't declared, which is weird because it is. Though I guess I'm constructing my strcat wrong.

Code: [Select]
char* pre_url[]={"/add_db.php?Registratiepunt="};
char* registratiepunt[]={"StalKennetW", "StalKrawall", "Stapmolen", "Dressuurbak"};
char* mid_url1[]={"&Paard="};
char* paard[]={"KennetW", "Krawall"};
char* mid_url2[]={"&Datum="};
char* datum[]={"lipsum"};
char* mid_url3[]={"&Tijd="};
char* tijd[]={"dolor"};
char url[300];  // enough room for all strings together


char* concat(char* pre_url, char* registratiepunt[id], char* mid_url1, char* paard[name], char* mid_url2, char* datum[date], char* mid_url3, char* tijd[time]) {
  url[0] = 0;          // start with a null string:
  strcat(url, pre_url);   // add first string
  strcat(url, registratiepunt[id]);
  strcat(url, mid_url1); 
  strcat (url, paard[name]);
  strcat(url, mid_url2);
  strcat (url, datum[date]);
  strcat(url, mid_url3);
  strcat (url, tijd[time]);

  return url;
}



SurferTim

You would probably be better off starting a new post, but the first thing that I see is your variables are declared as arrays of character pointers, and you are using them like arrays of characters.

Code: [Select]
char* registratiepunt[]={"StalKennetW", "StalKrawall", "Stapmolen", "Dressuurbak"};
// and later you use this
strcat(url, registratiepunt);


If you want to use those strings, then use this
Code: [Select]
strcat(url, registratiepunt[0]); // this is StalKennetW
strcat(url, registratiepunt[1]); // this is StalKrawall



PaulS

Code: [Select]
char* concat(char* pre_url, char* registratiepunt, char* mid_url1, char* paard, char* mid_url2, char* datum, char* mid_url3, char* tijd) {
  url[0] = 0;          // start with a null string:
  strcat(url, pre_url);   // add first string
  strcat(url, registratiepunt);
  strcat(url, mid_url1); 
  strcat (url, paard);
  strcat(url, mid_url2);
  strcat (url, datum);
  strcat(url, mid_url3);
  strcat (url, tijd);

  return url;
}

Why does a function need to return a pointer to a global variable?

Go Up