Tring to send arduino data to a php page and it doesn't seem to be working

Trying to get an arduino feather to send data it collects to a php page that records it. Think it connects to the page, the data doesn’t get recorded though. If the url is accessed in a browser, the data gets recorded

Arduino Code

    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>
     
    const char* ssid     = "test";
    const char* password = "password";
     
    const char* host = "foobar.com";
    String data;
     
    void setup() {
      Serial.begin(115200);
      delay(100);
     
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      
      WiFi.begin(ssid, password);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
     
      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
     
    int value = 0;
     
    void loop() {
      delay(5000);
      ++value;
     
      Serial.print("connecting to ");
      Serial.println(host);
      
      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      const int httpPort = 80;
      if (client.connect(host, httpPort)) 
      {
        // We now create a URI for the request
        String url = "/log.php";
        Serial.print("Requesting URL: ");
        Serial.println(url);
    
        //
        data = "inputData0=test&inputData1=74&inputData2=20&inputData3=901";
        // This will send the request to the server
        client.println("GET " + url + "?" + data + " HTTP/1.1");
        client.println("Host: " + host);
        client.println("Content-Type: application/x-www-form-urlencoded");
        client.print("Content-Length: ");
        client.println(data.length());
        client.println();
    
        delay(1000);
      
        // Read all the lines of the reply from server and print them to Serial
        while(client.available())
        {
          String line = client.readStringUntil('\r');
          Serial.print(line);
        }
      
        Serial.println();
        Serial.println("closing connection");
      }
      else
      {
        Serial.println("connection failed");
        return;
      }
    }

PHP Code

    <?php
    if (isset($_GET["inputData0"]) && isset($_GET["inputData1"]) && isset($_GET["inputData2"]) && isset($_GET["inputData3"]))
    {
      $t = time();
      $inputData0 = $_GET["inputData0"];
      $inputData1 =   $_GET["inputData1"];
      $inputData2 = $_GET["inputData2"];
      $inputData3 =  $_GET["inputData3"];
      $data =  date("m/d/Y",$t) . ',' . date("H:i:s",$t) . ',' . $inputData0 . ',' . $tinputData1 . ',' . $inputData2 . ',' . $inputData3 . "\n";
      $file =  './data.txt';
      file_put_contents($file, $data, FILE_APPEND);
      echo $data;
    }
    else
      echo "no good\n";
    ?>

The serial port shows this

connecting to foobar.com
Requesting URL: /log.php

closing connection

The ‘data.txt’ seems to only get appended when the page is accessed through a browser. Added the else echo "no good"; to the php page to see if that would show up in the serial, to no eval. Also tried commenting out the if isset on the php page.

What am I missing or doing wrong?

try to get rid of

        client.println("Content-Type: application/x-www-form-urlencoded");
        client.print("Content-Length: ");
        client.println(data.length());

and replace it with

      client.println(F("Connection: close"));

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.