HTTP GET request with Arduino MKR WiFi 1010

I am doing a project that logs data onto MySQL using and Arduino MKR WiFi 1010 via an HTTP request with the aid of XAMPP. I have tried the following code below and I can not get a response on the serial monitor to confirm connection with the web server. The code was initially for an Ethernet connection but I tried to modify it to a Wifi connection. I already have a php script (insert_temp.php) that logs the data onto MySQL and it works ok, I'm just struggling with the communication between the Arduino and the web server. I would really appreciate any help. Apologies if I haven't followed most of the guidelines required when posting, it is my first time posting on the forum.

Code:

#include <WiFiNINA.h>

#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

WiFiClient client;


int    HTTP_PORT   = 80;
String HTTP_METHOD = "GET";
char   HOST_NAME[] = "10.0.0.249"; // change to your PC's IP address
String PATH_NAME   = "/insert_temp.php";
String queryString = "?temperature=29.1";

void setup() {
  Serial.begin(9600);

  // initialize the Ethernet shield using DHCP:
  if (WiFi.begin(ssid, pass) == WL_CONNECTED) {
    Serial.println("Failed to obtaining an IP address using DHCP");
    while(true);
  }

  // connect to web server on port 80:
  if(client.connect(HOST_NAME, HTTP_PORT)) {
    // if connected:
    Serial.println("Connected to server");
    // make a HTTP request:
    // send HTTP header
    client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
    client.println("Host: " + String(HOST_NAME));
    client.println("Connection: close");
    client.println(); // end HTTP header

    while(client.connected()) {
      if(client.available()){
        // read an incoming byte from the server and print it to serial monitor:
        char c = client.read();
        Serial.print(c);
      }
    }

    // the server's disconnected, stop the client:
    client.stop();
    Serial.println();
    Serial.println("disconnected");
  } else {// if not connected:
    Serial.println("connection failed");
  }
}

void loop() {

}








and what does it print to Serial Monitor?

It is supposes to print the successful "connection to the server" and the successful creation of a record on the web server as a sentence that reads " New record created successfully" which confirms communication with the server. I should be able to see the record on my database after this command appears on the Serial Monitor.

Another challenge I will probably face if I get the architecture to work is how to continuously log data from the serial monitor from a button/sensor onto the web server as a 'querystring' since that is the only way (I think) that data can be sent to the web server and then logged into the MySQL database. Any ideas on that part would also be greatly appreciated.

so it doesn't print anything?

add

  while (!Serial);

after Serial.begin

Just did, it worked , thank you so much :+1:t4: . I guess it was not initiating when I opened the monitor after uploading the code.

only boards with separate USB chip have auto-reset on new USB connection

I am not worried about the board auto resetting since I am planning to use a battery to power it up so I wont be using the USB port for data transfer. I will post the final product once I am done with the current project I am attempting.

for boards with auto-reset opening Serial Monitor resets the board so you can always see prints from setup, while for boards with native USB the sketch is already in loop() at the time you open Serial Monitor after upload. using while (!Serial); causes the execution of setup() to wait for the Serial Monitor. so remove the line if you want to run the sketch without Serial Monitor

Thank you for the help, I appreciate it very much.