HTTP Get request with Arduino MKRGSM 1400

Hi,

I am trying to log data into a google drive spreadsheet, but I can't get the HTTP Get request to work. I read that I should use GSMSSLClient but I always get "connection failed" (from the sendData function). I tried GSMClient too and it does send the request but the spreadsheet never gets updated. The javascript code from google drive used to parse the request is working because the spreadsheets gets updated if I make a request from a browser.

#include <MKRGSM.h>

#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[]      = SECRET_GPRS_APN;
const char GPRS_LOGIN[]    = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

// initialize the library instance
GSMSSLClient client;
GPRS gprs;
GSM gsmAccess;

// Specify IP address or hostname
const char* host = "script.google.com";
const int httpsPort = 443;

String GAS_ID = "AKfycbzBmAlED1QTKNT967CQjJgsMNcxg2GDzKWAOidiK7FR4Ak082j0Y75qVwxfzwponqKy5g";

void setup() {
 // Initialize serial and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }

 Serial.println("Starting Arduino GPRS ping.");
 // connection state
 bool connected = false;

 // After starting the modem with GSM.begin()
 // attach the shield to the GPRS network with the APN, login and password
 while (!connected) {
   if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
       (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
     connected = true;
   } else {
     Serial.println("Not connected");
     delay(1000);
   }
 }
}

void loop() {

  sendData(10,20);
  //Serial.println("output gprs: " + gprs.ping(host));

 
 delay(10000);
}


// Function for Send data into Google Spreadsheet
void sendData(int tem, int hum)
{
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connectSSL(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  String string_temperature =  String(tem, DEC); 
  String string_humidity =  String(hum, DEC); 
  String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity;
  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.println("request sent");

} 

I bet it's a http / https problem. You have to "install" the public key on your board etc.
Check out that your are able to call a HTTPS ressource or use HTPP (for testing)

Hello,

At the moment I am having the exact same problem. I am trying to write data to influxcloud.
When I use GSMSSLClient and port 443, i can not connect to the server, i also get "connection failed". When I use GSMClient and port 80, i get the http response 308 Permanent Redirect.

#include <MKRGSM.h>
#include "arduino_secrets.h"

// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

// initialize the library instance
GPRS gprs;
GSM gsmAccess;
GSMClient client;

/* https://arduinogetstarted.com/tutorials/arduino-http-request */
uint16_t HTTP_PORT    = 80;
char     HOST_NAME[]  = "eu-central-1-1.aws.cloud2.influxdata.com"; // hostname of web server:
String   PATH_NAME    = SECRET_PATH;
String   token        = SECRET_TOKEN;
String   queryString  = "temperature_in_room,room=arduino temp=24";

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect. Needed for native USB port only
  
  Serial.println("\n\n\nStarting Arduino Influxdb-Test.");
  
  // connection state
  bool connected = false;
  
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected)
  {
    Serial.println("Begin GSM Access");
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) && (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
    {
      connected = true;
      Serial.println("GSM Access Success");
    }
    else
    {
      Serial.println("GSM Not connected");
      delay(1000);
    }
  }
  
  Serial.println("connecting...");
  
  // connecting to webserver
  if(client.connect(HOST_NAME, HTTP_PORT))
  {
    Serial.println("Connected to server");
    // send HTTP header
    client.println("POST /api/v2/write?bucket=home&org=sahaysolar HTTP/1.1");
    client.println("Host: eu-central-1-1.aws.cloud2.influxdata.com");
    client.println("Authorization: Token " + token);
    client.println("Content-Length: 40");
    client.println();
    client.println(queryString);
    
    Serial.println("Data sent");
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing forevermore:
    for (;;)
      ;
  }
}

I am having the exact same issue. I can use GSMClient on port 80 for HTTP but HTTPS doesnt work for some reason(made a post about it here https://forum.arduino.cc/t/arduino-mkr-1400-https-certificate-issue/975268 )
You get permanent redirect cause Influxdb Cloud redirects everything to their HTTPS.

Hi, i did some more research into this matter.

I figured out that GsmSSLWebClient.ino loads certificates(can be seen in modem debug with GSM gsmAccess(true); ). Those certificates are saved in the GSMRootCerts.h file in the MKRGSM library folder, more specifically MKRGSM/src/utility.

According to my browser, the InfluxDB uses root certificate called "ISRG Root X1" which isnt included in the default library:

image

I went over to the main website https://letsencrypt.org/certificates/ and downloaded the ISRG Root X1 .der certificate. Then printed it out in hex with hexdump and modyfied it into arduino format so it matches the other ones in the GSMRootCerts.h.

The modem loads it with OK but it still doesnt work...
Maybe my findings help someone figure out whats wrong.
Here is what i added to the file:

{
    "ISRG Root X1",
    (const uint8_t[]){
      0x30, 0x82, 0x05, 0x16, 0x30, 0x82, 0x02, 0xfe, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x11, 0x00,
      0x91, 0x2b, 0x08, 0x4a, 0xcf, 0x0c, 0x18, 0xa7, 0x53, 0xf6, 0xd6, 0x2e, 0x25, 0xa7, 0x5f, 0x5a,
      0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
      0x4f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x29,
      0x30, 0x27, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
      0x74, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x20, 0x52, 0x65, 0x73, 0x65, 0x61,
      0x72, 0x63, 0x68, 0x20, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55,
      0x04, 0x03, 0x13, 0x0c, 0x49, 0x53, 0x52, 0x47, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x58, 0x31,
      0x30, 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x39, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
      0x5a, 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x35, 0x31, 0x36, 0x30, 0x30, 0x30, 0x30, 0x5a,
      0x30, 0x32, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
      0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0d, 0x4c, 0x65, 0x74, 0x27, 0x73, 0x20,
      0x45, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x03,
      0x13, 0x02, 0x52, 0x33, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
      0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a,
      0x02, 0x82, 0x01, 0x01, 0x00, 0xbb, 0x02, 0x15, 0x28, 0xcc, 0xf6, 0xa0, 0x94, 0xd3, 0x0f, 0x12,
      0xec, 0x8d, 0x55, 0x92, 0xc3, 0xf8, 0x82, 0xf1, 0x99, 0xa6, 0x7a, 0x42, 0x88, 0xa7, 0x5d, 0x26,
      0xaa, 0xb5, 0x2b, 0xb9, 0xc5, 0x4c, 0xb1, 0xaf, 0x8e, 0x6b, 0xf9, 0x75, 0xc8, 0xa3, 0xd7, 0x0f,
      0x47, 0x94, 0x14, 0x55, 0x35, 0x57, 0x8c, 0x9e, 0xa8, 0xa2, 0x39, 0x19, 0xf5, 0x82, 0x3c, 0x42,
      0xa9, 0x4e, 0x6e, 0xf5, 0x3b, 0xc3, 0x2e, 0xdb, 0x8d, 0xc0, 0xb0, 0x5c, 0xf3, 0x59, 0x38, 0xe7,
      0xed, 0xcf, 0x69, 0xf0, 0x5a, 0x0b, 0x1b, 0xbe, 0xc0, 0x94, 0x24, 0x25, 0x87, 0xfa, 0x37, 0x71,
      0xb3, 0x13, 0xe7, 0x1c, 0xac, 0xe1, 0x9b, 0xef, 0xdb, 0xe4, 0x3b, 0x45, 0x52, 0x45, 0x96, 0xa9,
      0xc1, 0x53, 0xce, 0x34, 0xc8, 0x52, 0xee, 0xb5, 0xae, 0xed, 0x8f, 0xde, 0x60, 0x70, 0xe2, 0xa5,
      0x54, 0xab, 0xb6, 0x6d, 0x0e, 0x97, 0xa5, 0x40, 0x34, 0x6b, 0x2b, 0xd3, 0xbc, 0x66, 0xeb, 0x66,
      0x34, 0x7c, 0xfa, 0x6b, 0x8b, 0x8f, 0x57, 0x29, 0x99, 0xf8, 0x30, 0x17, 0x5d, 0xba, 0x72, 0x6f,
      0xfb, 0x81, 0xc5, 0xad, 0xd2, 0x86, 0x58, 0x3d, 0x17, 0xc7, 0xe7, 0x09, 0xbb, 0xf1, 0x2b, 0xf7,
      0x86, 0xdc, 0xc1, 0xda, 0x71, 0x5d, 0xd4, 0x46, 0xe3, 0xcc, 0xad, 0x25, 0xc1, 0x88, 0xbc, 0x60,
      0x67, 0x75, 0x66, 0xb3, 0xf1, 0x18, 0xf7, 0xa2, 0x5c, 0xe6, 0x53, 0xff, 0x3a, 0x88, 0xb6, 0x47,
      0xa5, 0xff, 0x13, 0x18, 0xea, 0x98, 0x09, 0x77, 0x3f, 0x9d, 0x53, 0xf9, 0xcf, 0x01, 0xe5, 0xf5,
      0xa6, 0x70, 0x17, 0x14, 0xaf, 0x63, 0xa4, 0xff, 0x99, 0xb3, 0x93, 0x9d, 0xdc, 0x53, 0xa7, 0x06,
      0xfe, 0x48, 0x85, 0x1d, 0xa1, 0x69, 0xae, 0x25, 0x75, 0xbb, 0x13, 0xcc, 0x52, 0x03, 0xf5, 0xed,
      0x51, 0xa1, 0x8b, 0xdb, 0x15, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x08, 0x30, 0x82,
      0x01, 0x04, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02,
      0x01, 0x86, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2b,
      0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03,
      0x01, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01,
      0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
      0x14, 0x2e, 0xb3, 0x17, 0xb7, 0x58, 0x56, 0xcb, 0xae, 0x50, 0x09, 0x40, 0xe6, 0x1f, 0xaf, 0x9d,
      0x8b, 0x14, 0xc2, 0xc6, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
      0x14, 0x79, 0xb4, 0x59, 0xe6, 0x7b, 0xb6, 0xe5, 0xe4, 0x01, 0x73, 0x80, 0x08, 0x88, 0xc8, 0x1a,
      0x58, 0xf6, 0xe9, 0x9b, 0x6e, 0x30, 0x32, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01,
      0x01, 0x04, 0x26, 0x30, 0x24, 0x30, 0x22, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
      0x02, 0x86, 0x16, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x78, 0x31, 0x2e, 0x69, 0x2e, 0x6c,
      0x65, 0x6e, 0x63, 0x72, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x30, 0x27, 0x06, 0x03, 0x55, 0x1d, 0x1f,
      0x04, 0x20, 0x30, 0x1e, 0x30, 0x1c, 0xa0, 0x1a, 0xa0, 0x18, 0x86, 0x16, 0x68, 0x74, 0x74, 0x70,
      0x3a, 0x2f, 0x2f, 0x78, 0x31, 0x2e, 0x63, 0x2e, 0x6c, 0x65, 0x6e, 0x63, 0x72, 0x2e, 0x6f, 0x72,
      0x67, 0x2f, 0x30, 0x22, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04, 0x1b, 0x30, 0x19, 0x30, 0x08, 0x06,
      0x06, 0x67, 0x81, 0x0c, 0x01, 0x02, 0x01, 0x30, 0x0d, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01,
      0x82, 0xdf, 0x13, 0x01, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
      0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x02, 0x01, 0x00, 0x85, 0xca, 0x4e, 0x47, 0x3e, 0xa3,
      0xf7, 0x85, 0x44, 0x85, 0xbc, 0xd5, 0x67, 0x78, 0xb2, 0x98, 0x63, 0xad, 0x75, 0x4d, 0x1e, 0x96,
      0x3d, 0x33, 0x65, 0x72, 0x54, 0x2d, 0x81, 0xa0, 0xea, 0xc3, 0xed, 0xf8, 0x20, 0xbf, 0x5f, 0xcc,
      0xb7, 0x70, 0x00, 0xb7, 0x6e, 0x3b, 0xf6, 0x5e, 0x94, 0xde, 0xe4, 0x20, 0x9f, 0xa6, 0xef, 0x8b,
      0xb2, 0x03, 0xe7, 0xa2, 0xb5, 0x16, 0x3c, 0x91, 0xce, 0xb4, 0xed, 0x39, 0x02, 0xe7, 0x7c, 0x25,
      0x8a, 0x47, 0xe6, 0x65, 0x6e, 0x3f, 0x46, 0xf4, 0xd9, 0xf0, 0xce, 0x94, 0x2b, 0xee, 0x54, 0xce,
      0x12, 0xbc, 0x8c, 0x27, 0x4b, 0xb8, 0xc1, 0x98, 0x2f, 0xa2, 0xaf, 0xcd, 0x71, 0x91, 0x4a, 0x08,
      0xb7, 0xc8, 0xb8, 0x23, 0x7b, 0x04, 0x2d, 0x08, 0xf9, 0x08, 0x57, 0x3e, 0x83, 0xd9, 0x04, 0x33,
      0x0a, 0x47, 0x21, 0x78, 0x09, 0x82, 0x27, 0xc3, 0x2a, 0xc8, 0x9b, 0xb9, 0xce, 0x5c, 0xf2, 0x64,
      0xc8, 0xc0, 0xbe, 0x79, 0xc0, 0x4f, 0x8e, 0x6d, 0x44, 0x0c, 0x5e, 0x92, 0xbb, 0x2e, 0xf7, 0x8b,
      0x10, 0xe1, 0xe8, 0x1d, 0x44, 0x29, 0xdb, 0x59, 0x20, 0xed, 0x63, 0xb9, 0x21, 0xf8, 0x12, 0x26,
      0x94, 0x93, 0x57, 0xa0, 0x1d, 0x65, 0x04, 0xc1, 0x0a, 0x22, 0xae, 0x10, 0x0d, 0x43, 0x97, 0xa1,
      0x18, 0x1f, 0x7e, 0xe0, 0xe0, 0x86, 0x37, 0xb5, 0x5a, 0xb1, 0xbd, 0x30, 0xbf, 0x87, 0x6e, 0x2b,
      0x2a, 0xff, 0x21, 0x4e, 0x1b, 0x05, 0xc3, 0xf5, 0x18, 0x97, 0xf0, 0x5e, 0xac, 0xc3, 0xa5, 0xb8,
      0x6a, 0xf0, 0x2e, 0xbc, 0x3b, 0x33, 0xb9, 0xee, 0x4b, 0xde, 0xcc, 0xfc, 0xe4, 0xaf, 0x84, 0x0b,
      0x86, 0x3f, 0xc0, 0x55, 0x43, 0x36, 0xf6, 0x68, 0xe1, 0x36, 0x17, 0x6a, 0x8e, 0x99, 0xd1, 0xff,
      0xa5, 0x40, 0xa7, 0x34, 0xb7, 0xc0, 0xd0, 0x63, 0x39, 0x35, 0x39, 0x75, 0x6e, 0xf2, 0xba, 0x76,
      0xc8, 0x93, 0x02, 0xe9, 0xa9, 0x4b, 0x6c, 0x17, 0xce, 0x0c, 0x02, 0xd9, 0xbd, 0x81, 0xfb, 0x9f,
      0xb7, 0x68, 0xd4, 0x06, 0x65, 0xb3, 0x82, 0x3d, 0x77, 0x53, 0xf8, 0x8e, 0x79, 0x03, 0xad, 0x0a,
      0x31, 0x07, 0x75, 0x2a, 0x43, 0xd8, 0x55, 0x97, 0x72, 0xc4, 0x29, 0x0e, 0xf7, 0xc4, 0x5d, 0x4e,
      0xc8, 0xae, 0x46, 0x84, 0x30, 0xd7, 0xf2, 0x85, 0x5f, 0x18, 0xa1, 0x79, 0xbb, 0xe7, 0x5e, 0x70,
      0x8b, 0x07, 0xe1, 0x86, 0x93, 0xc3, 0xb9, 0x8f, 0xdc, 0x61, 0x71, 0x25, 0x2a, 0xaf, 0xdf, 0xed,
      0x25, 0x50, 0x52, 0x68, 0x8b, 0x92, 0xdc, 0xe5, 0xd6, 0xb5, 0xe3, 0xda, 0x7d, 0xd0, 0x87, 0x6c,
      0x84, 0x21, 0x31, 0xae, 0x82, 0xf5, 0xfb, 0xb9, 0xab, 0xc8, 0x89, 0x17, 0x3d, 0xe1, 0x4c, 0xe5,
      0x38, 0x0e, 0xf6, 0xbd, 0x2b, 0xbd, 0x96, 0x81, 0x14, 0xeb, 0xd5, 0xdb, 0x3d, 0x20, 0xa7, 0x7e,
      0x59, 0xd3, 0xe2, 0xf8, 0x58, 0xf9, 0x5b, 0xb8, 0x48, 0xcd, 0xfe, 0x5c, 0x4f, 0x16, 0x29, 0xfe,
      0x1e, 0x55, 0x23, 0xaf, 0xc8, 0x11, 0xb0, 0x8d, 0xea, 0x7c, 0x93, 0x90, 0x17, 0x2f, 0xfd, 0xac,
      0xa2, 0x09, 0x47, 0x46, 0x3f, 0xf0, 0xe9, 0xb0, 0xb7, 0xff, 0x28, 0x4d, 0x68, 0x32, 0xd6, 0x67,
      0x5e, 0x1e, 0x69, 0xa3, 0x93, 0xb8, 0xf5, 0x9d, 0x8b, 0x2f, 0x0b, 0xd2, 0x52, 0x43, 0xa6, 0x6f,
      0x32, 0x57, 0x65, 0x4d, 0x32, 0x81, 0xdf, 0x38, 0x53, 0x85, 0x5d, 0x7e, 0x5d, 0x66, 0x29, 0xea,
      0xb8, 0xdd, 0xe4, 0x95, 0xb5, 0xcd, 0xb5, 0x56, 0x12, 0x42, 0xcd, 0xc4, 0x4e, 0xc6, 0x25, 0x38,
      0x44, 0x50, 0x6d, 0xec, 0xce, 0x00, 0x55, 0x18, 0xfe, 0xe9, 0x49, 0x64, 0xd4, 0x4e, 0xca, 0x97,
      0x9c, 0xb4, 0x5b, 0xc0, 0x73, 0xa8, 0xab, 0xb8, 0x47, 0xc2



    },
    1306
  },

I also tried the same thing with the Let’s Encrypt R3 .der certificate but as its not a root certificate it wont load into the modem.

hi,
I now used this library ArduinoBearSSL.h (GitHub - arduino-libraries/ArduinoBearSSL: Port of BearSSL to Arduino), and now its working! :slight_smile:

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