POST to HTTPS from Arduino MKR1000

Hi, I’m trying to input data from a Arduino MKR1000 into a database. I have a PHP script on a server to do this which is working when I use the command from mac terminal: curl -d “depth=300” https://people.bath.ac.uk/XXX/XXX/test.php

My PHP script is:

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the post data
    $reading = $_POST["depth"];
    $bottleId = 12;

    // Connect to MySQL
    require_once("./connect_to_mysql.php");

    // Insert into raw data table
    $sql = "INSERT INTO tbl_raw
    (
        bottleId,
        reading
    )
    VALUES
    (
        $bottleId,
        $reading
    )";

    if ($mysqli->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "
" . $mysqli->error;
    }

    $mysqli->close();
}

?>

I am trying to now POST to this script using Arduino IDE. For now I am not doing any sensor readings, just sending a value for “depth”. My code isn’t working and after searching online I can’t figure out why. Any help is hugely appreciated!

#include <WiFi101.h>

char ssid[] =      "XXXXX";                  // network to join
char pass[] =      "XXXXX";                  // password for wifi network

int status = WL_IDLE_STATUS;

char server[] = "people.bath.ac.uk";       // server URL

String postData;
String postVariable = "depth=";

WiFiClient client;

void setup() {

  // Start serial connection for feedback
  Serial.begin(9600);

  // Connect to WiFi
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);                         // wait 10 sec for connection
  }

  // Log WiFi details
  Serial.print("Connected to: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}


void loop() {

  // Readings
  int depth = 500;
  int bottleId = 11;

  postData = postVariable + depth;

  // Connect to php script at port 80
  if (client.connect(server, 80)) {
    Serial.println("Connected to server.");

    client.println("POST /XXX/XXX/test.php HTTP/1.1");
    client.println("Host: people.bath.ac.uk");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(postData.length());
    client.println();
    client.print(postData);
    
  } else {
    Serial.println("Failed to connect to server.");
  }

  if (client.connected()) {
    client.stop();
  }
  Serial.println(postData);

  delay(3000);
}

I have a PHP script on a server to do this which is working when I use the command from mac terminal: curl -d "depth=300" https://people.bath.ac.uk/XXX/XXX/test.php

I doubt that. The above curl command sends a GET request to the server, the script does only work for POST requests. So you either used another curl command or a different PHP script if it worked for you.

Also here is discrepance:

https://people.bath.ac.uk/XXX/XXX/test.php

vs.

  if (client.connect(server, 80)) {

HTTPS is handled on port 443 by default and the traffic is encrypted while you're connecting to port 80 (which is the default port for HTTP).

My code isn't working

Which is the worst description of a problem I can think of. No statement of what you'd expect and what you got, no serial output. The only information we get is that you were not confident with what the code did.