Offline
Newbie
Karma: 0
Posts: 49
|
 |
« on: April 12, 2012, 02:18:44 am » |
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: //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
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #1 on: April 12, 2012, 06:12:53 am » |
Try the code in this post. http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629The 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. getPage(server,"/mypage.php?test=1")
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #2 on: April 12, 2012, 06:40:24 am » |
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"
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #3 on: April 12, 2012, 01:21:33 pm » |
Thanks but I can't get your code to work properly, it keeps telling me the connection failed.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #4 on: April 12, 2012, 01:33:29 pm » |
Did you change the network settings to your localnet?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #5 on: April 12, 2012, 01:41:59 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #6 on: April 12, 2012, 01:46:28 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #7 on: April 12, 2012, 01:59:27 pm » |
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? 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); }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #8 on: April 12, 2012, 02:04:21 pm » |
What happens if you try this from a web browser? http://server/add_db.php?id=12&name=johnReplace "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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #9 on: April 12, 2012, 04:02:57 pm » |
What happens if you try this from a web browser? http://server/add_db.php?id=12&name=johnReplace "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. 
|
|
|
|
« Last Edit: April 12, 2012, 04:07:13 pm by C220amg »
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #10 on: April 12, 2012, 04:29:30 pm » |
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. sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page); client.write(outBuf);
|
|
|
|
« Last Edit: April 12, 2012, 04:31:29 pm by SurferTim »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #11 on: April 12, 2012, 04:42:00 pm » |
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. 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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 49
|
 |
« Reply #12 on: April 13, 2012, 05:24:33 am » |
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 Serial.println(url); . It's just blank. The strcat I've constructed: 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. 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; }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Offline
Faraday Member
Karma: 50
Posts: 3457
|
 |
« Reply #13 on: April 13, 2012, 05:41:24 am » |
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. char* registratiepunt[]={"StalKennetW", "StalKrawall", "Stapmolen", "Dressuurbak"}; // and later you use this strcat(url, registratiepunt);
If you want to use those strings, then use this strcat(url, registratiepunt[0]); // this is StalKennetW strcat(url, registratiepunt[1]); // this is StalKrawall
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #14 on: April 13, 2012, 07:50:08 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
|