Pulling Values from an arduino and putting it into a database

Hi There,

Probably a bit of neiche thing but I am trying to send data from an arduino and put the data into a database called Pre-play with a table called retrieve with column values of volume,gain,treble,bass and contour.

I am trying to add them all in however no matter what I do it just adds the first declared POST value in this case the volume condition and then it just assigns a value of 0 to the other 4. I have tried it various ways and have used code checkers however they haven’t brought up any "common issues that it could be. I imagine the issue is just me. I have used PHP before I used it for some projects in the past and also use Laravel but decided to use standard PHP as I wasn’t sure how I would implement this into laravel.

Please could you have a look at my code and see what I am doing wrong/incorrect. :slight_smile:

First way I tried:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";	
    $dbname = "Pre-Play";
		$conn = mysqli_connect($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Database Connection failed: " . $conn->connect_error);
    }
    if(!empty($_POST['volvalue'] && $_POST['gaivalue']))
    {
		$volvalue = $_POST['volvalue'];
        $gaivalue = $_POST['gaivalue'];
	    $sql = "INSERT INTO retrieve (volume,gain)VALUES ('".$volvalue."','".$gaivalue."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
	$conn->close();
?>

Second way I tried:

   <?php
    $servername = "localhost";
    $username = "root";
    $password = "";	
    $dbname = "Pre-Play";
		$conn = mysqli_connect($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Database Connection failed: " . $conn->connect_error);
    }
    if(!empty($_POST['volvalue']))
    {
		$volvalue = $_POST['volvalue'];
	    $sql = "INSERT INTO retrieve (volume,gain)VALUES ('".$volvalue."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
    if(!empty($_POST['gaivalue']))
    {
		$gaivalue = $_POST['gaivalue'];
	    $sql = "INSERT INTO retrieve (gain)VALUES ('".$gaivalue."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
    if(!empty($_POST['trevalue']))
    {
		$trevalue = $_POST['trevalue'];
	    $sql = "INSERT INTO retrieve (treble)VALUES ('".$trevalue."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
    if(!empty($_POST['basvalue']))
    {
		$basvalue = $_POST['basvalue'];
	    $sql = "INSERT INTO retrieve (bass)VALUES ('".$basvalue."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}
    if(!empty($_POST['convalue']))
    {
		$convalue = $_POST['convalue'];
	    $sql = "INSERT INTO retrieve (contour)VALUES ('".$value."')"; 
		if ($conn->query($sql) === TRUE) {
		    echo "OK";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}
	}

	$conn->close();
?>

If you’re able to help with this then that’d be brilliant :smiley:

Kind Regards,
Ben

Since your arduino is submitting the data to the web server, do you have any way of displaying information that the web server sends back in a reply? The answer to this affects what debugging code needs to do - display vs log information.

In any case, you need to ‘see’ (display or log) what the $_POST data actually is.

Hi I get what you mean and it does display a value of 4 for all of the values present. Here is a serial monitor print from the arduino/node mcu.

Volume send Value=4

Value=
-7

Gain send Value=4
ble Value=
200

Treble send Value=3
Value=
200

Bass send Value=4
tour Value=
200

Contour send Value=3

as you can see it is giving off values at random because nothing is currently plugged into the port being used however these “random” values should get passed through.

In the arduino code they are displayed like this. Ignore the 2 this however doesnt affect the value that it comes across as. Shall I post all the arduino code too?

int Volume = 2;

int Gain = 2;
int Treble = 2;
int Bass = 2;
int Contour = 2;

These are the values printed and so should be the ones being passed to the website and in turn logged in the database.

  Serial.print(volume);
Serial.print(gain);
  Serial.print(treble);
    Serial.print(bass);
      Serial.print(contour);

As you can see they are in the same case as the ones in the database columns so when I did the insert initially I thought it would just go straight in especially since the values are integers and the database has been set up to receive integers.

What do you think?

Sponsor our Newsletter | Privacy Policy | Terms of Service