Show Posts
|
|
Pages: 1 [2] 3
|
|
19
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 20, 2010, 05:46:09 pm
|
zoomkat. Thanks for your help but I'm getting nowhere. I've tried putting in just the code to turn the LED ON/OFF but it doesn't work and the serial monitor chucks out tons of stuff. I've been on this since 7am and it's now 10:45pm so here's my code: //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, 0x00, 0x11, 0xAC }; byte ip[] = { 192, 168, 1, 19 }; byte server[] = { 192,168,1,20 };
Client client(server, 80); String readString = String(30); //string for fetching data from address
void setup() { Ethernet.begin(mac, ip); Serial.begin(9600); //pin mode pinMode(7,OUTPUT); digitalWrite(7,LOW); // I use this pin as GND for the LED. pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
Serial.println("starting simple arduino client test"); Serial.println();
delay(1000);
Serial.println("connecting...");
if (client.connect()) { Serial.println("connected"); client.println("GET /~shb/arduino.txt HTTP/1.0"); client.println(); } else { Serial.println("connection failed"); } }
void loop() { if (client.available()) { //########################################################### char c = client.read(); //read char by char HTTP request if (readString.length() < 30) { //store characters to string readString.concat(c); Serial.println(readString); } //output chars to serial port Serial.print(c); //if HTTP request has ended if (c == '\n') { //Turn on LED Serial.println(readString.substring(6,9));//for test to allow readString to be viewed if(readString.substring(6,9)=="7=1") {//read the last 3 information characters from URL // Serial.println(readString); //set digital 7 HIGH Serial.println("HIGH"); digitalWrite(7, HIGH); // set the LED on readString="";//clear readString } if(readString.substring(6,9)=="7=0") { //set digital 7 LOW Serial.println("LOW"); digitalWrite(7, LOW); // set the LED OFF readString="";//clear readString } } //###########################################################
if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); Serial.println("=================================="); Serial.println(""); client.stop(); // for(;;); } } }
I'll have another go tomorrow perhaps! 
|
|
|
|
|
20
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 20, 2010, 04:39:05 pm
|
according to the .ini Yes. And this is what is sent to the php file: GET /update_db.php?celsius=45 HTTP/1.1
by this line client.println("GET /update_db.php?celsius=45 HTTP/1.1"); If this arduino is set up as a web-server why can I use this client.println("GET /update_db.php?celsius=45 HTTP/1.1"); Should it not be Server.print which gives errors when I try to Compile?
|
|
|
|
|
21
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 20, 2010, 03:19:20 pm
|
Well in my endeavour to learn Arduino Ethernet I'm still unable to get the Arduino web-client to accept commands from a server PC. Also I'm unable to get web-server to send data to a mysql database. So I've written (probably poorly) code which I 95% understand that accepts commands from a browser. http://192.168.1.19/?7=1 turns digital 7 on http://192.168.1.19/?7=0 turns digital 7 off http://192.168.1.19/?A=0 gets the value from analogue 0, prints what looks like the correct string in the web-browser but it won't write to a mysql. #include <SPI.h> //#include <WString.h> #include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC }; //physical mac address byte ip[] = { 192, 168, 1, 19 }; // ip in lan byte gateway[] = { 192, 168, 1, 1 }; // internet access via router byte subnet[] = { 255, 255, 255, 0 }; //subnet mask //byte server[] = { 192,168,1,20 }; Server server(80); //server port byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC) int ledPin = 4; // LED pin char link[]="http://www.scienceprog.com/"; //link data String readString = String(30); //string for fetching data from address boolean LEDON = false; //LED status flag //####################################### #define READING_PIN 0 double R1 = 10000.0; //resistance put in parallel double V_IN = 5.0; double A = 1.129148e-3; double B = 2.34125e-4; double C = 8.76741e-8; double K = 9.5; // mW/dec C – dissipation factor double SteinhartHart(double R) { // calculate temperature double logR = log(R); double logR3 = logR * logR * logR; return 1.0 / (A + B * logR + C * logR3 ); } int celsius;//Global declare celsius //####################################### void setup(){ //start Ethernet Ethernet.begin(mac, ip, gateway, subnet); //pinMode; pinMode(7,OUTPUT); digitalWrite(7,LOW); // I use this pin as GND for the LED. pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13. //enable serial datada print Serial.begin(9600); } void loop(){ readString="";//Clear readString before filling // Create a client connection Client client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read(); //read char by char HTTP request if (readString.length() < 30) { //store characters to string readString.concat(c); Serial.println(readString); } //output chars to serial port Serial.print(c); //if HTTP request has ended if (c == '\n') { //lets check if LED should be lighted Serial.println(readString.substring(6,9));//for test to allow readString to be viewed if(readString.substring(6,9)=="7=1") {//read the last 3 information characters from URL // Serial.println(readString); //set digital 7 HIGH Serial.println("HIGH"); digitalWrite(7, HIGH); // set the LED on readString="";//clear readString } if(readString.substring(6,9)=="7=0") { //set digital 7 LOW Serial.println("LOW"); digitalWrite(7, LOW); // set the LED OFF readString="";//clear readString } if(readString.substring(6,9)=="A=0") { //readAnal 0 temp ReadTemp(); Serial.println("for test"); client.print("GET http://192.168.1.20/update_db.php?celsius="); client.print(celsius); client.println(" HTTP/1.0"); client.println("Host: http://192.168.1.20"); client.println(); readString="";//clear readString } //clearing string for next read readString=""; //stopping client client.stop(); } } } } } //######################################################################## void ReadTemp() { //Serial.print("I'm at ReadTemp "); double adc_raw = analogRead(READING_PIN); double V = adc_raw / 1024 * V_IN;//Serial.println(adc_raw); double R_th = (R1 * V ) / (V_IN - V);//calculate resistance double kelvin = SteinhartHart(R_th) - V*V/(K * R_th); celsius = kelvin - 273.15;//was double but changed to int to remove decimal places Serial.println(); delay(1000); return; } //#######################################################################
Can anyone see and explain what's wrong as I've spent all day on this with no answer. All I want is an Arduino with Ethernet Shield that can accept commands from a web-server PC and send values to a mysql database on the same to the web-server PC. Thanks for any help. Tony
|
|
|
|
|
22
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 19, 2010, 03:36:35 pm
|
|
Ok back again. I would like my Arduino web-client to write data to the mysql database every 15 minutes.
What would be the best way to do the time interval counting?
As I see it, it could be done by the Arduino but could my web server poll the Arduino or what about the timer_ntp. I'd need some examples with the last two suggestions if they are good ideas.
|
|
|
|
|
23
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 18, 2010, 01:52:57 pm
|
@ PaulS I was just reading a web page which is similar to your explanation. My PHP script is working and now looks like this: <?php //echo ("update_db.php indicator"); $celsius = $_GET['celsius']; //Connect to database $con = mysql_connect("localhost", "xxxx", "xxxx"); if(!$con) { die('Could not connect: ' .mysql_error()); } mysql_select_db("1stClassElec", $con);
mysql_query("INSERT INTO device_1(temp) VALUES ('$celsius')"); mysql_close($con); ?> Thanks go to all of you for your help, and I expect I'll be back soon with another question 
|
|
|
|
|
24
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 18, 2010, 12:42:55 pm
|
Well this is what I'm trying to run. The Arduino code is yours, modified to update the database with a value. //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, 0x00, 0x11, 0xAC }; byte ip[] = { 192, 168, 1, 19 }; byte server[] = { 192,168,1,20 }; // zoomkat
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 /update_db.php?celsius=18 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(;;); } }
And the PHP script <?php //Connect to database $con = mysql_connect("localhost", "xxxx", "xxxx"); if(!$con) { die('Could not connect: ' .mysql_error()); } mysql_select_db("1stClassElec", $con);
mysql_query("INSERT INTO device_1(temp) VALUES (celsius)"); mysql_close($con); ?> NOTE: If I change mysql_query("INSERT INTO device_1(temp) VALUES (celsius)"); to mysql_query("INSERT INTO device_1(temp) VALUES (19)"); and then run Arduino the value of 19 gets written into the database. Why mysql_query("INSERT INTO device_1(temp) VALUES (celsius)"); doesn't pass its value of celsius=18 from the Arduino script is a mystery to me at the moment.
|
|
|
|
|
26
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 18, 2010, 09:00:53 am
|
@ PaulS. If I pasta http://192.168.1.20/arduino.txt into a browser it is found and shows my inserted text. @robtillart. client.println("GET /arduino.txt HTTP/1.0"); gives: starting simple arduino client test
connecting...
connected
HTTP/1.1 200 OK
Date: Sat, 18 Dec 2010 13:48:52 GMT
Server: Apache/2.2.14 (Ubuntu)
Last-Modified: Sat, 18 Dec 2010 08:55:24 GMT
ETag: "1fbee-5-497ab729b1eef"
Accept-Ranges: bytes
Content-Length: 5
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain
test
disconnecting.
So is this working? If so what is wrong with my code which stops me writing to the database?
|
|
|
|
|
27
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 18, 2010, 04:01:36 am
|
zoomkat, thanks for info. I've created file below, please check it through in case I'm making a mistake. //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, 0x00, 0x11, 0xAC }; byte ip[] = { 192, 168, 1, 19 }; byte server[] = { 192,168,1,20 }; // zoomkat
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 /http://192.168.1.20/var/www/arduino.txt 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(;;); } }
and the responce I get back starting simple arduino client test
connecting...
connected
HTTP/1.1 404 Not Found
Date: Sat, 18 Dec 2010 08:57:55 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 312
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 /http://192.168.1.20/var/www/arduino.txt was not found on this server.</p> <hr> <address>Apache/2.2.14 (Ubuntu) Server at 127.0.1.1 Port 80</address> </body></html>
disconnecting.
==================================
The arduino.txt file does exist. I hope someone can help, thanks.
|
|
|
|
|
28
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 17, 2010, 06:05:15 pm
|
|
robtillaart
Perhaps I've confused things more than I'm confused.
I'm trying to send data using client.print via ethernet from an Arduino client to my home web server which runs on a laptop. The serial.print is just so I can see what's happening using the "Serial Monitor".
I'm trying to get the Arduino to pass data to update_db.php which should load it into the database 1stClassElec table device_1.
Sorry, and grateful for your help
|
|
|
|
|
30
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql
|
on: December 17, 2010, 02:28:02 pm
|
|
I imagine I need a client as my idea was to have the Arduino write to the mysql database, there could be more than one Arduino sending the same sort of data. When I wish to view any results I would just read the results from the database in a web page.
The Serial.print was only to check to right data was being sent but I've just tried GET and Post but still nothing appears in the database.
I still don't understand why nothing is written to the database. :'(
|
|
|
|
|