0

I am attempting to send a POST request to an arduino from a PHP script that is called by a web page. I am using file_get_contents() to accomplish this. I have read the examples from the PHP manual here: Here. It appears that the Arduino is receiving the POST request but there is no content included in the data stream, only headers and content parameters. Here is my relevant php

        // send database updates to the arduino
        $url = 'http://'.$ip;
        $options = array(
            'http' => array(
                'header' => "Content-type: application/x-www/form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($responseText)
            ),
        );
        
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

currently on the Arduino I am just dumping the entire POST request to the terminal so that I can verify that it is being received. Here is the output of the Arduino terminal.

Ethernet WebServer Example
server is at 192.168.3.3
new client
POST / HTTP/1.0
Host: 192.168.3.3
Connection: close
Content-Length: 80
Content-type: application/x-www/form-urlencoded

client disconnected

As you can see the entire POST request is there accept the content. It shows the Content-Length but no actual content.

Here is the Arduino code that produced the terminal output. It is nothing more than the Web Server example that comes with the Arduino IDE.

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 3, 3);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

String postData;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //postData = postData + c;
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          /*client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");*/
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    Serial.println("client disconnected");
  }
}

What is causing the content of the POST request to not be sent from the PHP?

  • 2
    why not curl? its easy and works better – flakerimi Dec 22 '20 at 01:42
  • Does this answer your question? [POST data to a URL in PHP](https://stackoverflow.com/questions/3080146/post-data-to-a-url-in-php) – flakerimi Dec 22 '20 at 01:42
  • @flakerimi I attempted cURL first but didn't get this far. I will try again with the example in the post that you linked – beewrangler Dec 22 '20 at 01:50
  • https://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents ( you may try fopen -- near the end of this post ) – Ken Lee Dec 22 '20 at 01:51
  • `var_dump(http_build_query($responseText))` gives the intended string back? Also I dont see the `Content-Length` header here, is this a more minimal version? – user3783243 Dec 22 '20 at 01:56
  • @user3783243 yes, it does – beewrangler Dec 22 '20 at 01:59
  • @flakerimi When I use the curl example in the post you linked I get a "Call to undefined function curl_init()" error. Is there something that needs to be uncommented in php.ini to make this work? – beewrangler Dec 22 '20 at 02:34
  • @user3783243 is the Content-Length header not the 7th line in the Terminal output in the Original Post? – beewrangler Dec 22 '20 at 02:37
  • @kenLee I saw that post but was not able to get a workable answer out of it – beewrangler Dec 22 '20 at 02:39
  • What php version you have, check phpinfo() if curl is enabled. Check this: https://stackoverflow.com/questions/33993614/php-curl-is-enabled-but-has-no-effect – flakerimi Dec 22 '20 at 02:41
  • @flakerimi I'm using PHP version 7.3.19-1~deb10u1 on a Raspberry Pi VM (for convience). I have enabled cURL in php.ini according to this[link](https://www.geeksforgeeks.org/how-to-enable-curl-in-php/). I have restarted the server. Still have the undefined function curl_init() error – beewrangler Dec 22 '20 at 03:10
  • check if you enabled on loaded php.ini, might be somewhere else, on phpinfo() there is a path, use phpenmod curl on cli to enable. thats how you enable on windows – flakerimi Dec 22 '20 at 03:40
  • sudo apt install php7.3-curl – flakerimi Dec 22 '20 at 03:41
  • @flakerimi after installing the curl module i am receiving the POST headers but still no Content. I am having the same problem weather I use curl or file_get_contents() which leads me to believe there is an issue with the Arduino code. I have no idea what it could be since all it really does is spit out whatever it receives. – beewrangler Dec 22 '20 at 04:00
  • Change `$url` to a PHP page and just have `print_r($_POST);` that will show if PHP is sending data correctly. – user3783243 Dec 22 '20 at 12:23
  • Why not use GET, Also check this https://stackoverflow.com/questions/14944773/receiving-a-http-post-request-on-arduino – flakerimi Dec 22 '20 at 13:18
  • Thanks to everybody that helped me out on this last night, especially @flakerimi. Your help with getting curl working on my server may not have fixed my problem but it was valuable information. For the solution to my problem see my answer below. – beewrangler Dec 22 '20 at 21:17

1 Answers1

0

After I let my brain rest for about half a day I determined the cause of my problem. It was not in the PHP at all. It was being caused by this IF block in the Arduino code

if (c == '\n' && currentLineIsBlank) {
    // send a standard http response header
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    //client.println("Refresh: 5");
    client.println();
    /*client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    // output the value of each analog input pin
    for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
        int sensorReading = analogRead(analogChannel);
        client.print("analog input ");
        client.print(analogChannel);
        client.print(" is ");
        client.print(sensorReading);
        client.println("<br />");
    }
    client.println("</html>");*/
    break;
}

it was causing while loop that it is nested in to terminate at the blank line between the header and the content. My new working code for the Arduino is

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 3, 3);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

String postData;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    
    while (client.connected()) {
      while (client.available()) {
        char c = client.read();
        postData = postData + c;
        // send a standard http response header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
      }
      client.stop();
    }
    // give the web browser time to receive the data
    delay(1);
    
    Serial.println("\r\nclient disconnected");
    Serial.println(postData);
    Serial.println(postData.substring(postData.indexOf("status=")+7, postData.indexOf("status=")+8 ));
  }
}