MKR1000: Failing to send a GET request using WIFI101

Hello everyone,

i've been trying to send a GET request to fill a Google Sheets directly by WIFI using WIFI101 but I can't find the error in my code or in the way i'm doing it.
I used a tutorial on youtube to do that (sorry can't find it at the moment), it was working on a MKR1010 but then I had to convert it to MKR1000 (using Wifi101 instead of Wifinina), I managed to solve some problems but not the last one.
The typical Wifi101 example code for sending a Google request is working.
But the code below does not work (I've tried port = 443 and 80, i've been on forum and posts, and tried things without sucess until now):


#include <SPI.h>
#include <WiFi101.h>
#include "Arduino.h"
#include <Wire.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)                                // your network key index number
int status = WL_IDLE_STATUS;                     // the WiFi radio's status
const char host[] = "script.google.com";

const int httpsPort = 80;  
WiFiClient client;
String GAS_ID = SECRET_GAS;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  
  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WEP network, SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // once you are connected :
  Serial.print("You're connected to the network");
  printWiFiStatus();



}


void loop() {


  uint16_t co2 = 800;
  float temperature = 25;
  float humidity = 100;
  String Temp = "Temperature : " + String(temperature) + " °C";
  String Humi = "Humidity : " + String(humidity) + " %";
  String strco2 = "CO2 : " + String(co2) + " ppm";
  Serial.println(Temp);
  Serial.println(Humi);
  Serial.println(strco2);
  sendData(temperature, humidity, co2);
  delay(10000);
}

void sendData(float tem, float hum, uint16_t coo) {
  Serial.println("==========");
  Serial.print("connecting to ");
  Serial.println(host);
  //----------------------------------------Connect to Google host
 if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
  //----------------------------------------
  //----------------------------------------Processing data and sending data
  String string_temperature =  String(tem);
  // String string_temperature =  String(tem, DEC); 
  String string_humidity =  String(hum); 
  String string_co2 = String(coo);
  String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity + "&co2=" + string_co2;
  Serial.print("requesting URL: ");
  Serial.println(url);

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
         "Host: " + host + "\r\n" +
         "Connection: close\r\n\r\n");
Serial.print(String("GET ") + url + " HTTP/1.1\r\n" +
         "Host: " + host + "\r\n" +
         "Connection: close\r\n\r\n");

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

  //----------------------------------------Checking whether the data was sent successfully or not
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.print("reply was : ");
  Serial.println(line);
  Serial.println("closing connection");
  Serial.println("==========");
  Serial.println();
  //----------------------------------------
}

void printWiFiStatus() {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);

  // print the received signal strength:

  long rssi = WiFi.RSSI();

  Serial.print("signal strength (RSSI):");

  Serial.print(rssi);

  Serial.println(" dBm");
}

void printWifiData() {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);


}

Here is what I get from the client.read that I've added to have some info

HTTP/1.1 301 Moved Permanently

Content-Type: text/html; charset=UTF-8

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

Pragma: no-cache

Expires: Mon, 01 Jan 1990 00:00:00 GMT

Date: Sun, 22 Jan 2023 07:57:54 GMT

Location: https://script.google.com/macros/s/XXX
X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

Content-Security-Policy: frame-ancestors 'self'

X-XSS-Protection: 1; mode=block

Server: GSE

Accept-Ranges: none

Vary: Accept-Encoding

Connection: close

Transfer-Encoding: chunked

headers received
esp8266/Arduino CI has failed
reply was : 15a

closing connection

the site doesn't accept unsecure connection.
use WiFiSSLClient and port 443

Thanks!
But it does not work...
Now, it cannot connect to the server (script.google.com), while it was poossible using WifiClient (with either port 80 or port 443...).

Also, i've tried the simple example replacing WifiClient by WifiSSLClient and the port to 443, it does not work also

Still investigating...
i've tested many combinations of WiFiClient // WiFiSSLClient // client.connect() // client.conectSSL with no sucess.
i've added the domain "script.google.com" to the SSL root certificates of the MKR1000 board, it can now connect to the server as previously without the SSL, but still cannot send the request.
If anyone has a clue, I'll be glad to hear :wink:
Thanks
Have a great week

Still testing here...
I found that post with a similar problem:

They solved it using the WiFiSSLClient_BearSSL library.
I tried without any sucess...
I've modified line 317 of WiFi.cpp. I've added the website in the SSL certificate using the Wifi101 updater (but when I close and reopen again, i cannot see the website i'm trying to connect to...).
And it does not work, impossible to connect to the server.
Any other libraries you think I could try for my MKR1000? (I've also tried HTPPSClient and ArduyinoHttpsClient, not working also...)
Thanks!

I've found that in the "Rezad first" post of this section of the forum (yes, I probably should have read this first...soeey!):

Q: I installed a certificate but I cannot establish an HTTPS connection.
A: Check if the certificate is loaded and is SHA 256. The 348 is still not yet supported. Comodo for example has certificate SHA-348 with RSA encryption

I don't know what it means exactly.
Since I would like to connect to "script.google.com", should this server has a SHA 256 certificate? If not, will that be impossible to connect to it using a MKR 1000? Or could I create one by some means?
Thank you

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