Arduino get data from web problem

Hi,

I’ve got a problem with the retrieving data from a webpage.
The connection to the webserver works.
The connection from the webserver works.

I can post some data and retrieve some data.

But I can only retrieve static data from a php page.
This is my php code:

<html>
    <body>
        <?php
        echo "Begin";
        echo $_POST['Commando'];
        echo "End";
        ?>
    </body>
</html>

On the webpage I get this:
BeginCommandoEnd

On my serial monitor on the arduino I get:

Starting connection...
connected
GET /cloud_robotics/index2.php HTTP/1.1
vestuurd
HTTP/1.1 200 OK
Date: Tue, 06 Dec 2016 12:21:18 GMT
Server: Apache/2
X-Powered-By: PHP/5.5.29
Vary: User-Agent
Content-Length: 56
Connection: close
Content-Type: text/html

<html>
    <body>
        BeginEnd    </body>
</html>

So i don’t see the post data on the arduino, while it was posted.
Even when i’m saving it into a session, I never get the data from the post.

Can somebody explain me why this happens and suggest me a fix?

Can somebody explain me why this happens

Sure. When the PHP script ran, there was no POST data for the name Commando.

Why there wasn't is because there is something wrong with the code you didn't post.

Thnks for the quick reply.

Here is my arduino code:

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "WiFiShield";          //  your network SSID (name)
char pass[] = "12345678";   // your network password

int status = WL_IDLE_STATUS;
char server[] = "xxxxxxxx";  // Own webserver
String m1;
String m2;
String m3;
String m4;
String m5;
String m6;
String mclose;
String message;
int loopCount = 0;

// Initialize the client library
WiFiClient client;

void Request() {
  mclose = " HTTP/1.1";
  message = m1 + mclose;
  Serial.println(message);
  client.println(message);
  client.println("Host: xxxxxxxxx");
  client.println("Connection: close");
  Serial.println("vestuurd");
  client.println();
}

void ConnectWiFi() {
  status = WiFi.begin(ssid, pass);
}

void setup() {
  //Start serial to debug in the serial monitor
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);
  //Connect to WiFi
  status = WiFi.begin(ssid, pass);
}

void loop() {
  //Wait for 1 minute
  if (loopCount < 5)
  {
    // if loopCount is less than 60, just delay a second
    delay(1000);
  }
  else {
    loopCount = 0;
    //Check if WIFi is connected
    if ( status != WL_CONNECTED) {
      Serial.println("Couldn't get a wifi connection");
      delay(10000);
      //If it is not connect, retry connection
      ConnectWiFi();
      while (true);
    }
    else {
      Serial.println("Connected to wifi");
      Serial.println("\nStarting connection...");
      // if you get a connection, report back via serial:

      //If there is a connection with the server
      if (client.connect(server, 80)) {
        Serial.println("connected");
        // Make a HTTP request:
        m1 = "GET /cloud_robotics/index2.php"; //Link to visit

        //Execute the request
        Request();
        client.flush();
        //If the delay is not long enough then the request is not sent within time. Results in a crash which causes a 40 second delay
        delay(2000);
        //Read the response from the request
        while (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
        client.stop();
      }
    }

  }

  loopCount++; //Add +1 to the loopCount
}

Here is my full php code again:

<html>
    <body>
        <?php
        echo "Begin";
        echo $_POST['Commando'];
        echo "End";
        ?>
    </body>
</html>

This is the php code from where i send the post data:

<form action="index2.php" method="post">
            Commando:
            <input type="text" name="Commando">
            <input type="submit" name="submit">

        </form>

Thanks

It is hard to tell what you are doing when you appear to have two PHP scripts whose names you have not told us.

It appears that the GET request will cause the index2.php script to be executed. The result of that is some html that includes a form with a text field and a submit button, and some action to be performed when the submit button is pressed. The submit button is not pressed by anything you are doing, so expecting some other PHP script to be executed is unrealistic.

Sorry I did not understand your reply.
Sorry about the lack of the information.

Ok i’ll explain what i do.

So i got this php file named testchart.php:

<form action="index2.php" method="post">
            Commando:
            <input type="text" name="Commando">
            <input type="submit" name="submit">

        </form>

From there, I click on submit. By doing that I send the data, which is filled in at the input of Commando, to the page index2.php.

The code on index2.php:

<html>
    <body>
        <?php
        echo "Begin";
        echo $_POST['Commando'];
        echo "End";
        ?>
    </body>
</html>

When you visit index2.php it will now show:
Begin(Post commando value)End

Meanwhile the arduino is running:

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "WiFiShield";          //  your network SSID (name)
char pass[] = "12345678";   // your network password

int status = WL_IDLE_STATUS;
char server[] = "xxxxxxxx";  // Own webserver
String m1;
String m2;
String m3;
String m4;
String m5;
String m6;
String mclose;
String message;
int loopCount = 0;

// Initialize the client library
WiFiClient client;

void Request() {
  mclose = " HTTP/1.1";
  message = m1 + mclose;
  Serial.println(message);
  client.println(message);
  client.println("Host: xxxxxxxxx");
  client.println("Connection: close");
  Serial.println("vestuurd");
  client.println();
}

void ConnectWiFi() {
  status = WiFi.begin(ssid, pass);
}

void setup() {
  //Start serial to debug in the serial monitor
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);
  //Connect to WiFi
  status = WiFi.begin(ssid, pass);
}

void loop() {
  //Wait for 1 minute
  if (loopCount < 5)
  {
    // if loopCount is less than 60, just delay a second
    delay(1000);
  }
  else {
    loopCount = 0;
    //Check if WIFi is connected
    if ( status != WL_CONNECTED) {
      Serial.println("Couldn't get a wifi connection");
      delay(10000);
      //If it is not connect, retry connection
      ConnectWiFi();
      while (true);
    }
    else {
      Serial.println("Connected to wifi");
      Serial.println("\nStarting connection...");
      // if you get a connection, report back via serial:

      //If there is a connection with the server
      if (client.connect(server, 80)) {
        Serial.println("connected");
        // Make a HTTP request:
        m1 = "GET /cloud_robotics/index2.php"; //Link to visit

        //Execute the request
        Request();
        client.flush();
        //If the delay is not long enough then the request is not sent within time. Results in a crash which causes a 40 second delay
        delay(2000);
        //Read the response from the request
        while (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
        client.stop();
      }
    }

  }

  loopCount++; //Add +1 to the loopCount
}

So when you posted that commando, in my point of view the arduino (which is constantly running) should pick up the value.

But it doesn’t.
It only gets this:

BeginEnd

Thanks

So i got this php file named testchart.php

How do you cause this to be executed?

There really doesn't seem to be a need to have made that a PHP file, since there is nothing for PHP to do. It's just incomplete HTML data.

So when you posted that commando

When you clicked on the button, the script was executed with the POSTed data. It generated some output and ended. The server then sent the output to the client that executed the POST request.

Having the Arduino make a GET request, to execute the index2.php script with no input is not going to get you the data some other execution of the script supplied as input.

You would need to make index2.php (really, that is a dumb name) store the data somewhere on the server, and create another php script that could get the stored data.

The testchart.php has more code in it (which is php code) so it is not useless.
Second, the index2.php was a test page so it had a random name since other names were already in use.

Thanks for all your help.
I save the post data in a .txt file on the server.

I read this data on the index2.php and the arduino reads this data!

IT FINALLY WORKED :smiley:

Thanks dude.

IT FINALLY WORKED

Excellent. Hopefully, you append data to the file in the "save the data" script, and remove the data from the file in the "fetch the data" script. Otherwise, you could have problems with multiple clients trying to get the Arduino to display text on the LCD.