Go Down

Topic: Arduino ethernet webclient php script and mysql (Read 61 times) previous topic - next topic

xsilvergs

Dec 17, 2010, 02:42 pm Last Edit: Dec 17, 2010, 05:00 pm by xsilvergs Reason: 1
Hi,

Can somebody help me out as I can't see the answer to my problem.

I'm running an Ethernet Shield with a thermistor and wish to add the values to a mysql database. I know the php script works as I can insert a number instead of (celsius)"); and then run the script and the number appears in the database. My problem is with the sketch which works accept it wont pass the data to the php file which lives at /var/www/ard_log/update.phpThermometer0()
Here is the sketch:
Code: [Select]
#include <SPI.h>

#include <WString.h>
#include <Ethernet.h>

#define READING_PIN 0
/*
     Simple Ethernet Test

       Arduino server outputs simple text to browser

     The circuit:
     * Arduino Duemilanove
       * Arduino Ethernet shield
       * Basic FTDI breakout 5V
       *LED connected to GND and digital pin 4 via resistor
     
       By Minde
     http://www.sciencprog.com/
*/

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
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
//#################################################
  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 );
   }
//#################################################
void setup(){
//start Ethernet
 Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
 pinMode(ledPin, OUTPUT);  
//enable serial datada print  
 Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
 if (client) {
   while (client.connected()) {
//#############################################
   double adc_raw = analogRead(READING_PIN);
   //Serial.println(adc_raw);
   double V =  adc_raw / 1024 * V_IN;
   //calculate resistance
   double R_th = (R1 * V ) / (V_IN - V);
   double kelvin = SteinhartHart(R_th) - V*V/(K * R_th);
   int celsius = kelvin - 273.15;//was double but changed to int to remove decimal places
   Serial.print(celsius);
   Serial.println();
   client.print("Temp.");
   client.print(" is ");
   client.print(celsius);
   client.println(".C");
   client.println("GET /http://192.168.1.20/ard_log/update_db.php?celsius=");
   client.println(celsius);
   client.println(" HTTP/1.1\r\n");
   client.println("Host: http://192.168.1.20");
   client.println();

   delay(1000);
//##############################################
        //clearing string for next read
         readString="";
         //stopping client
         client.stop();
           }
         }
       }

and here is the php:
Code: [Select]
<?php
//Connect to database
$con mysql_connect("xxxx""xxxx""xxxx");
if(!
$con)
 
   &#123;
 
   die('Could not connect: ' .mysql_error());
 
   &#125;
mysql_select_db("1stClassElec"$con);

mysql_query("INSERT INTO device_1(temp) VALUES (celsius)");

mysql_close($con);
?>

Any help would be appreciated as I've spent hours on this problem

xsilvergs

#1
Dec 17, 2010, 07:19 pm Last Edit: Dec 17, 2010, 07:20 pm by xsilvergs Reason: 1
Here is my latest attempt but it still doesn't work.
database name = 1stClassElec and the table is temp_1
Code: [Select]
#include <SPI.h>
#include <WString.h>
#include <Ethernet.h>
#define READING_PIN 0
/*
     Simple Ethernet Test

       Arduino server outputs simple text to browser

     The circuit:
     * Arduino Duemilanove
       * Arduino Ethernet shield
       * Basic FTDI breakout 5V
       *LED connected to GND and digital pin 4 via resistor
     
       By Minde
     http://www.sciencprog.com/
*/

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
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
//#################################################
   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 );
   }
//#################################################
void setup(){
//start Ethernet
 Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
//  pinMode(ledPin, OUTPUT);  
//enable serial datada print  
 Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
 if (client) {
   while (client.connected()) {
//#############################################
   double adc_raw = analogRead(READING_PIN);
   //Serial.println(adc_raw);
   double V =  adc_raw / 1024 * V_IN;
   //calculate resistance
   double R_th = (R1 * V ) / (V_IN - V);
   double kelvin = SteinhartHart(R_th) - V*V/(K * R_th);
   int celsius = kelvin - 273.15;//was double but changed to int to remove decimal places

   Serial.println();
   client.print("Temp.");
   client.print(" is ");
   client.print(celsius);
   client.println(".C");
   client.print("POST /http://192.168.1.20/ard_log/update_db.php?temp=");
   Serial.print("GET /ard_log/update_db.php?temp=");
   client.print(celsius);
   Serial.print(celsius);
   client.println(" HTTP/1.1");
   Serial.println(" HTTP/1.1");
   client.println("Host: http://192.168.1.20");
   Serial.println("Host: http://192.168.1.20");
   client.println("User-Agent: Arduino");
   Serial.println("User-Agent: Arduino");
   client.println("Accept: text/html");
   Serial.println("Accept: text/html");
   client.println();

   delay(1000);
//##############################################
        //clearing string for next read
         readString="";
         //stopping client
         client.stop();
           }
         }
       }

zoomkat

If the arduino server can pass the info back to a browser, then it is probably a PHP or mysql issue.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

robtillaart

#3
Dec 17, 2010, 08:02 pm Last Edit: Dec 17, 2010, 08:12 pm by robtillaart Reason: 1
Think you are messing up Client and Serial ... Keep them separated!

update: (looking again at your code made me wonder)

Do you intend to let the Arduino act as a server from which data can be GET?
or do you intend to let the Arduino act as a Client that POST data to another webserver.


Code: [Select]

   while (client.connected())
   {
   // keep it simpler ..
   int celsius = 100;

   Serial.println();
   [glow]client[/glow].print("Temp.");  // Serial??
   [glow]client[/glow].print(" is ");
   [glow]client[/glow].print(celsius);
   [glow]client[/glow].println(".C");
   client.print("[glow]POST [/glow]/http://192.168.1.20/ard_log/update_db.php?temp=");
   client.print(celsius);

   Serial.print("[glow]GET [/glow]/ard_log/update_db.php?temp=");  // [glow]GET vs POST difference[/glow]


'
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

xsilvergs

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

Go Up