title: Problem ESP32 send data to Google Sheet through Google App Script
body: <p>At first, my question is from my little project IoT. Purpose, using ESP32 send data to Google Sheet through Google App Script.</p>
  <p>The ESP32 task is to take two random numbers and pass them to Google Sheet through Google App Script (Web App) by http.</p>
  <p>My problem is, I can insert data to Google Sheet only 3-4 records, after that connection between ESP32 and Google App Script has failed everytime. I am not sure about the problem.</p>
  <p>My codes has 2 parts, first is for ESP32 (Arduino IDE), and second is for Google App Script</p>
  <p>First part code is below, it is very simple.</p>
  <pre><code>#include &lt;WiFi.h&gt;
  #include &lt;WiFiClientSecure.h&gt;
  String t;
  const char* host = &quot;script.google.com&quot;;
  const int httpsPort = 443;
  const char* ssid = &quot;MY_SSID&quot;;
  const char* pass = &quot;MY_SSID_PW&quot;;
  String GAS_ID = &quot;GAS_ID&quot;;
  String GAS_Sheet = &quot;dht_data&quot;;
  WiFiClientSecure client;
  long now = millis();
  long lastMeasure = 0;
  void setup() {
      Serial.begin(115200); delay(500);
      WiFi.begin(ssid, pass);
      Serial.print(&quot;\nConnecting&quot;);
      while (WiFi.status() != WL_CONNECTED) {
          Serial.print(&quot;.&quot;);
      }
      Serial.print(&quot;\nSuccessfully connected to : &quot;); Serial.println(ssid);
      Serial.print(&quot;\nIP address: &quot;); Serial.println(WiFi.localIP());
      Serial.println();
      client.setInsecure();
  }
  void loop() {
      now = millis();
      if (now - lastMeasure &gt; 30000) {
          lastMeasure = now;
          float ValueSensor1 = random(2000, 4000) / 100.0;
          float ValueSensor2 = random(6000, 8000) / 100.0;
          Serial.println();
          Serial.print(&quot;\ntemperature = &quot;); Serial.print(ValueSensor1, 1);
          Serial.print(&quot;\thumidity = &quot;); Serial.print(ValueSensor2, 1);
          Serial.println();
          sendData(ValueSensor1, ValueSensor2);
      }
  }
  void sendData(float SValue1, float SValue2) {
      Serial.println(&quot;==========&quot;);
      Serial.print(&quot;connecting to &quot;); Serial.println(host);
      //---- Connect to Google host
      if (!client.connect(host, httpsPort)) {
          Serial.println(&quot;connection failed&quot;);
          return;
      }
      //---- Post Data
      String url;
      url += &quot;/macros/s/&quot; + GAS_ID + &quot;/exec?&quot;;
      url += &quot;id=&quot; + String(GAS_Sheet);
      url += &quot;&amp;temperature=&quot; + String(SValue1, 2);
      url += &quot;&amp;humidity=&quot; + String(SValue2, 2);
      Serial.print(&quot;requesting URL: &quot;); Serial.println(url);
      client.print(String(&quot;GET &quot;) + url + &quot; HTTP/1.1\r\n&quot; +
              &quot;Host: &quot; + host + &quot;\r\n&quot; +
              &quot;User-Agent: BuildFailureDetectorESP8266\r\n&quot; +
              &quot;Connection: close\r\n\r\n&quot;);
      Serial.println(&quot;request sent&quot;);
      //---- Wait Echo
      while (client.connected()) {
          String line = client.readStringUntil('\n');
          if (line == &quot;\r&quot;) {
              Serial.println(&quot;headers received&quot;);
              break;
          }
      }
      String line = client.readStringUntil('\n');
      if (line.startsWith(&quot;{\&quot;state\&quot;:\&quot;success\&quot;&quot;)) {
          Serial.println(&quot;ESP-32/Arduino CI successfull!&quot;);
      } else {
          Serial.println(&quot;ESP-32/Arduino CI has failed&quot;);
      }
      Serial.print(&quot;reply was : &quot;);
      Serial.println(line);
      Serial.println(&quot;closing connection&quot;);
      Serial.println(&quot;==========&quot;);
      Serial.println();
  }
  </code></pre>
  <p>Second part code is below,</p>
  <pre><code>function doGet(e){
      // open the spreadsheet
      var ss = SpreadsheetApp.getActive();
  
      // use the 'id' parameter to differentiate between sheets
      var sheet = ss.getSheetByName(e.parameter[&quot;id&quot;]);
  
      // extract headers
      // getRange accepts row, col, number_of_rows and num_of_cols as argument
      // getLastColumn returns the position of the last column that has content
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  
      // store the position of the last row
      var lastRow = sheet.getLastRow();
      var cell = sheet.getRange('a1');
      var col = 0;
      var d = new Date();
      for (i in headers){
          // loop through the headers and if a parameter name matches the header name insert the value
          if (headers[i] == &quot;Timestamp&quot;) {
              val = d.toDateString() + &quot;, &quot; + d.toLocaleTimeString();
          } else {
              val = e.parameter[headers[i]];
          }
          // append data to the last row
          cell.offset(lastRow, col).setValue(val);
          col++;
      }
      return ContentService.createTextOutput('success');
  }
  </code></pre>
  <p>What is the cause of problem? and How can I solve it?</p>
  <p>Thank you very much in advance.</p>