Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to send my arduino sensor data to mysql databse using XAMPP and PHPmyadmin. I have been using esp8266 for the wifi connection. But i am unable to send the value. And in actually i have to send four values but for the time being i am sending only one value. So also please tell how to send multiple values. I have written the php file and save in to htdocs of apache.

 #include <WiFiEsp.h>
  #include <WiFiEspClient.h>
 #include <WiFiEspUdp.h>
  #include <SoftwareSerial.h>
 #include <PubSubClient.h>
 #include <SPI.h>
//#include <Ethernet.h>
//#include <espduino.h>
 IPAddress ip(192,168,8,1);
 char ssid[] = "ZONG MBB-E8372-B67D";           // your network SSID (name)
 char pass[] = "08522547";           // your network password
int status = WL_IDLE_STATUS;   // the Wifi radio's status
int photocellPin = 4;  // Analog input pin on Arduino we connected the SIG      pin from sensor
 int photocellReading;  // Here we will place our reading

char server[] =  "192.168.8.1";
// Initialize the Ethernet client object
 WiFiEspClient espclient;

SoftwareSerial soft(2,3); // RX, TX
void setup() {
 // initialize serial for debugging
 Serial.begin(115200);
 // initialize serial for ESP module
 soft.begin(115200);
 // initialize ESP module
 WiFi.init(&soft);

 // check for the presence of the shield
 if (WiFi.status() == WL_NO_SHIELD) {
  Serial.println("WiFi shield not present");
  // don't continue
  while (true);
  }

 // attempt to connect to WiFi network
 while ( status != WL_CONNECTED) {
 Serial.print("Attempting to connect to WPA SSID: ");
 Serial.println(ssid);
 // Connect to WPA/WPA2 network
 status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
 Serial.println("You're connected to the network");
  }

 void loop() {

   photocellReading = analogRead(photocellPin); // Fill the sensorReading with the information from sensor

  // Connect to the server (your computer or web page)  
 if (espclient.connect(server, 80)) {
  espclient.println("GET /write_data7.php?"); // This
  espclient.println("value="); // This

  espclient.println(photocellReading); // And this is what we did in the     testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
   espclient.println(" HTTP/1.1"); // Part of the GET request
   espclient.println("Host: 192.168.8.1"); // IMPORTANT: If you are using     XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    espclient.println("Connection: close"); // Part of the GET request    telling the server that we are over transmitting the message
   espclient.println(); // Empty line
   espclient.println(); // Empty line
    espclient.stop();    // Closing connection to server

   }

     else {
 // If Arduino can't connect to the server (your computer or web page)
   Serial.println("--> connection failed\n");
    }

  // Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
  delay(10000);
   }

following is php code:

<?php
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'connected_car_1');
define('DB_USER','Maryam1');
define('DB_PASSWORD','telecom1213');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

 $sql = "INSERT INTO connected_car_1.sensor (value) VALUES ('".$_GET["value"]."')";    

   // Execute SQL statement

  mysql_query($sql,$con);
mysql_close($con);
?>

and this is my serial monitor output :[serial monitor]1

I am very new to coding in arduino as well as php and databases. So kindly help me where i am doing wrong. And do i have to provide the ip address of php ie. 127.0.0.1. if yes. then where?

share|improve this question
1  
Nice SQL injection attack vulnerability there. (http://yoursite.com/yourpage.php?value=');+drop+database+co‌​nnected_car_1;--Will people never learn? Hint : Use PDO. – Majenko Jul 9 at 10:14
    
I am not uploading on a webpage. I am uploading on mysql. That is the comment in the code for if you want to upload it on a webpage, but iam not doing that. – Maryam Jul 9 at 10:43
    
You have a piece of PHP code there. That is a web page. It accepts GET parameters. – Majenko Jul 9 at 10:44
    
then how should i upload the arduino reading on mysql? – Maryam Jul 9 at 11:29
    
Through a web page, like you have created. Just learn, right from the beginning, best practices. Your web page, if it were ever public, would be a huge security risk. – Majenko Jul 9 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.