#include "WiFi.h"
#include <HTTPClient.h>


const char* ssid = "Wokwi-GUEST";         // change SSID
const char* password = "";    // change password
String GOOGLE_SCRIPT_ID = "AKfycbxnv087qqNTj_dLdMjVwxe0z20sLOknOJXJUnz2Ppt1O2F14fgvuQsB4avjODhwyCDO";

const int sendInterval = 5000;

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  delay(10);

  // connect to WiFi
  Serial.println();
  Serial.print("Connecting to WiFi: ");
  Serial.println(ssid);
  Serial.flush();
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("Connected to WiFi");
}

void loop() {
  spreadsheet_comm();
  delay(sendInterval);
}

void spreadsheet_comm(void) {
  HTTPClient http;
  String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?read";

  Serial.print("Making a request");
  if (http.begin(client, url)) { //Specify the URL and client
    int httpCode = http.GET();
    String payload;
    if (httpCode > 0) { //Check for the returning code
      payload = http.getString();

      Serial.println(httpCode);
      Serial.println(payload);
    } else {
      Serial.println("Error on HTTP request");
    }
    http.end(); //Free the resources
  } else {
    Serial.println("Unable to connect");
  }
}