I'm having difficulty getting my Arduino Mkr 1010 to send http request

I have a sketch which I'm using to connect to a Google Apps Script URL (desired URL: https://script.google.com/a/macros/edwardmarno.com/s/XXXXXX/exec?Weight=3450*) in order to pass a weight parameter to a Google Sheet. The sketch connects to my wifi fine but then it doesn't go to the URL correctly. Please could someone point me in the right direction?

Thanks!

(* Script ID redacted)

#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

int status = WL_IDLE_STATUS;         // the WiFi connection status

void setup() {
  Serial.begin(9600);  // initialize serial communication
  while (!Serial);

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);  // connect to WPA/WPA2 network
    delay(10000);  // wait 10 seconds for connection
  }

  // print connection details
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // establish a new client connection
  WiFiClient client;
  const int httpPort = 443;
  if (!client.connect("script.google.com", httpPort)) {
    Serial.println("Connection failed");
    return;
  }

  // send the HTTP request
  client.print(String("GET ") + "/a/macros/edmarno.com/s/XXXXXXXXXXXXXX/exec?Weight=3450" + " HTTP/1.1\r\n" +
               "Host: " + "script.google.com" + "\r\n" +
               "Connection: close\r\n\r\n");

  // wait for response
  while (!client.available()) {
    delay(10);
  }

  // read response
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // disconnect
  client.stop();
  delay(600000);  // wait 10 minutes before repeating
}

Did you add SSL certificates to you board for your endpoint (https://script.google.com) ?

Thank you, I have done so now, but it's still not working. The serial monitor just stops after printing the IP address.

I have just tried changing the port (line 44) to 80 and I now get a 301 error in the serial monitor:

12:11:37.125 -> WiFi connected
12:11:37.125 -> IP address:
12:11:37.126 -> 192.168.1.163
12:11:37.485 -> HTTP/1.1 301 Moved Permanently
12:11:37.485 -> Content-Type: text/html; charset=UTF-8
12:11:37.486 -> <HTML>
12:11:37.486 -> <HEAD>
12:11:37.486 -> <TITLE>Moved Permanently</TITLE>
12:11:37.486 -> </HEAD>
12:11:37.486 -> <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
12:11:37.486 -> <H1>Moved Permanently</H1>
12:11:37.486 -> The document has moved <A HREF="https://script.google.com/a/macros/edwardmarno.com/s/XXXXXXXXXXX/exec?Weight=999">here</A>.
12:11:37.537 -> </BODY>
12:11:37.537 -> </HTML>
12:11:37.537 ->
12:11:37.537 -> 0
12:11:37.537 ->

When I go to the URL in my browser, I get the parameter logged in the sheet immediately so perhaps my code for the GET request is wrong? (Should I be using a GET request?)

This is normal. Your browser can follow the redirect automatically, while Arduino don't.

You can't use the url script.google.com without SSL handshaking on port 443, as far as I know.

OK thank you. I'll set the port back to 443 then.

I'm following this methodology (ESP32 Data Logging to Google Sheets with Google Scripts) and they accomplish what I am trying to do using an ESP32 instead of mkr1010. Do you know what I'd need to do on my 1010 to accomplish this?

Thanks and sorry for being dense!

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