1

I want to send my Arduino's sensor data to a MySQL database, using XAMPP and phpMyAdmin. I have been using an ESP8266 for the wifi connection. But I am unable to send the value. And 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 saved in to Apache's htdocs.

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include <SPI.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 the 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:

enter image description here

I am very new to coding in Arduino as well as PHP and databases. So kindly tell me what I am doing wrong. And do I have to provide the IP address of the server, ie. 127.0.0.1? If yes, then where?

16
  • 3
    Nice SQL injection attack vulnerability there. (http://yoursite.com/yourpage.php?value=');+drop+database+connected_car_1;--Will people never learn? Hint : Use PDO.
    – Majenko
    Commented Jul 9, 2016 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
    Commented Jul 9, 2016 at 10:43
  • 1
    You have a piece of PHP code there. That is a web page. It accepts GET parameters.
    – Majenko
    Commented Jul 9, 2016 at 10:44
  • 2
    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
    Commented Jul 9, 2016 at 15:53
  • 2
    Your problem looks to not be anything to do with MySQL, XAMPP, or anything like that. You seem to have a basic inability to communicate on the WiFi network. All those "Timeout" messages should tell you that much at least. Get the network connection working first and then work on interfacing with MySQL.
    – Majenko
    Commented Jul 9, 2016 at 19:16

1 Answer 1

1

You should write the PC IP, for example: 192.168.1.53, because 127.0.0.1 is the IP used only for phpmyadmin and the database inside your computer.

1
  • 3
    Being pedantic: 127.0.0.1 is the internal IP address present in every IP device, and it's use as a shorthand for "this machine". It's use for TCP/IP comms between process running in the same machine, like any program that need to access the local database (as you said). 127.0.0.1 is not routeable, and not accesible from other machines.
    – user31481
    Commented Oct 30, 2017 at 14:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.