problem with POST sent from microcontroller to web server PHP

Hello to eveybody,

I'm buidling a robot that measures the available networks and send this information to a web server that I have also created in my local network, using Apache+PHP+MySQL. The problem is that when I send a simple POST message to test the system, the PHP I've programmed (addNewNetworks.php) does not receive the message or does not process it, I don't know where the problems is. I've searched for many samples, fórums, etc.., and to me it seems everything is ok, but the connection is not working at all.

The connection with the database is working as I can see the content by using another php file (index.php):

the code i'm using in my microprocessor to send the data is:

client.stop();
if (client.connect(serverObj, port)) { /where serverObj=192.168.1.41 that is my PC IP (server), and port=80/

client.println("POST /addNewNetworks.php HTTP/1.1");
client.print("Host: "); // SERVER ADDRESS HERE TOO
client.println(serverObj);
//client.print("Connection: close\n"); /i've tried both, using this line and without using it, and nothing changes/
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data); /*where String data="idNetw=1&netwName=pepito\0";

} else { Serial.println("\nFailed when connecting to the server");}

If instead of printing it to the client, I print it to the Serial (terminal), I see:

POST /addNewNetworks.php HTTP/1.1
Host: 192.168.1.41
Content-Type: application/x-www-form-urlencoded
Content-Length: 25

idNetw=1&netwName=pepito

thus, I understand this should be correct

The file in which I receive those data and process it is addNewNetworks.php:

<? //function that adds the information received to the table of networks in the database include("connect.php"); $link=Connection(); //this function works fine as I'm using it in the index.php file and it shows the content of the database //while the cue is not empty, receives pairs of Id_networks and name and stores it in the database if (!empty($_POST)) { $idNetw=$_POST["idNetw"]; $netwName=$_POST["netwName"]; $query = "INSERT INTO `wifi_networks` VALUES ('".$idNetw."','".$netwName."')"; } mysqli_query($query,$link); mysqli_close($link); ?>

Can you help me to solve this problem?? I don't have more ideas to solve it and I have even tried to install wireshark and I can see the POST message, so it seems as the problem could be in the addNewNetworks.php file, but I cannot find it.

Add an else block after the if block. Is $_POST actually empty?

If not, iterate through the $_POST array and see what is actually in it.

what is serverObj?

it should be client.print("Connection: close\r\n")

Juraj,

serverObj is the IP where my server is installed, that is: the IP of my PC in my internal wifi network.


PaulS,

I'm not sure if $_POST is empty, I thought that it was a variable for a single connection, I didn't imagine that it could contain all the POST commands flying on the network. How can I show the data contained in that variable if I'm connecting from the microcontroller? maybe writing all the data inside $_POST to a txt file? or how should I sent that information to the microcontroller to show it throught the terminal? what do you recommend me?

In case I do an iteration to that variable, how is the information stored in there? or it would be just to search for idNetw and netwName inside the variable?

thanks

maybe writing all the data inside $_POST to a txt file?

Yes. When I was doing PHP development, I created a library of useful functions. One of them was:

function PrintPostVars($caller)
{
	$received = "Post vars\r\n\r\n";
	
	foreach($_POST as $key=>$value)
	{
		$received .= "$key = [$value]\r\n";
	}
	
	$printout = fopen("C:\\tmp\\post_variables.txt",'a');
	fwrite($printout, "Number of post variables in $caller: ");
	fwrite($printout, count($_POST));
	fwrite($printout, "\r\n");
	fwrite($printout, $received);
	fwrite($printout, "\r\n");
	fclose($printout);
}

Then, in the php script, I could put:

	require_once 'library/functions.php';
//	PrintPostVars();

Uncomment the function call to write the data to the file.

tanzak:
serverObj is the IP where my server is installed, that is: the IP of my PC in my internal wifi network.

IPAddress serverObj(192, 168, x, x); ?

strange variable name

I finally was able to solve it, it was some problem in the php code. The final code that Works was:

<?php //function that adds the information received to the table of networks in the database include("connect.php"); //file that manages the connection settings to the database $link=Connection(); //function that connects with the database (specified at connect.php) //while the cue is not empty, receives pairs of Id_networks and name and stores it in the database if (!empty($_POST)) { $idNetw=$_POST["idNetw"]; $netwName=$_POST["netwName"]; $query = "INSERT INTO `wifi_networks` VALUES ('".$idNetw."','".$netwName."')"; print_r($_POST); } else { echo("No data has been sent"); } $result=mysqli_query($link,$query) or die('Error:'.mysqli_error($link)); mysqli_close($link); //header("Location: index.php"); ?>