Wio Terminal buttons to send the date and time and the text

I have a question about a program that uses Wio Terminal buttons to send the date and time and the text corresponding to the buttons to a Google spreadsheet.
Once it succeeds in sending the data to the spreadsheet.
But when I turn off the Wio Terminal and try again, it connects to the wifi but fails to send the data.
Please tell me why this happens and how to fix it.

#include <LovyanGFX.hpp>
static LGFX tft;
#include <HTTPClient.h>
#include "rpcWiFi.h"
#include <ArduinoJson.h>

const char* ssid = "SSID";
const char* password =  "PASSWORD";

const char* host = "https://script.google.com/macros...";

void setup() {
 
  tft.begin();
  tft.setRotation(1);
  tft.setTextSize(2);
  tft.fillScreen(TFT_BLACK);

  Serial.begin(115200);
  pinMode(WIO_KEY_A, INPUT_PULLUP);
  pinMode(WIO_KEY_B, INPUT_PULLUP);
  pinMode(WIO_KEY_C, INPUT_PULLUP);
    
 
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  //connect
  do {
    tft.print(".");
    WiFi.begin(ssid, password);
    delay(500);
  } while (WiFi.status() != WL_CONNECTED);

  tft.println("Behavior Recorder_Wio");
}

void loop() {
  boolean blnSuccess = false;
  if (digitalRead(WIO_KEY_A) == LOW) {
        tft.setCursor(60, 30);
        tft.fillScreen(TFT_BLACK);
        tft.setTextColor(TFT_WHITE);
        tft.setTextSize(2);
        tft.printf("Sending A");


    // JSON

    char json[100];
    sprintf(json, "{\"button\": \"%s\" }","A");
    Serial.println(json);
     
   HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    http.begin(host); 
   
    Serial.print("[HTTP] POST...\n");
    int httpCode = http.POST(json);
   
    if (httpCode > 0) {
     Serial.printf("[HTTP] GET... code: %d\n", httpCode);
   
      if (httpCode == HTTP_CODE_OK) {
        Serial.println("[HTTP] Success!");
        String payload = http.getString();
        Serial.println(payload);
        blnSuccess = true;
         }
     } else {
     Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
     }  
     http.end();
     
    tft.fillScreen(TFT_BLACK);
    if (blnSuccess){
      tft.setTextColor(TFT_WHITE);
      tft.setCursor(60, 30);
      tft.println("Success!");

    }else{
      tft.setTextColor(TFT_RED);
      tft.println("Retry");
    }     
   }
   
  if (digitalRead(WIO_KEY_B) == LOW) {
        tft.setCursor(60, 30);
        tft.fillScreen(TFT_BLACK);
        tft.setTextColor(TFT_WHITE);
        tft.setTextSize(2);
        tft.printf("Sending B");

    // JSON
    char json[100];
     sprintf(json, "{\"button\": \"%s\" }","B");
    Serial.println(json);
     
   HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    http.begin(host); 
   
    Serial.print("[HTTP] POST...\n");
    int httpCode = http.POST(json);
   
    if (httpCode > 0) {
     Serial.printf("[HTTP] GET... code: %d\n", httpCode);
   
      if (httpCode == HTTP_CODE_OK) {
        Serial.println("[HTTP] Success!");
        String payload = http.getString();
        Serial.println(payload);
        blnSuccess = true;
         }
     } else {
     Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
     }  
     http.end();

    tft.fillScreen(TFT_BLACK);
    if (blnSuccess){
      tft.setTextColor(TFT_WHITE);
      tft.setCursor(60, 30);
      tft.println("Success!");

    }else{
      tft.setTextColor(TFT_RED);
      tft.println("Retry");
    }
   }
   
  if (digitalRead(WIO_KEY_C) == LOW) {
        tft.setCursor(60, 30);
        tft.fillScreen(TFT_BLACK);
        tft.setTextColor(TFT_WHITE);
        tft.setTextSize(2);
        tft.printf("Sending C");

    // JSON
    char json[100];

    sprintf(json, "{\"button\": \"%s\" }","C");
    Serial.println(json);
     
   HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    http.begin(host); 
   
    Serial.print("[HTTP] POST...\n");
    int httpCode = http.POST(json);
   
    if (httpCode > 0) {
     Serial.printf("[HTTP] GET... code: %d\n", httpCode);
   
      if (httpCode == HTTP_CODE_OK) {
        Serial.println("[HTTP] Success!");
        String payload = http.getString();
        Serial.println(payload);
        blnSuccess = true;
         }
     } else {
     Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
     }  
 
     http.end();

    tft.fillScreen(TFT_BLACK);
    if (blnSuccess){
      tft.setTextColor(TFT_WHITE);
      tft.setCursor(60, 30);
      tft.println("Success!");

    }else{
      tft.setTextColor(TFT_RED);
      tft.println("Retry");
    }
   }
  }

GAS Google Spreadsheet

function doPost(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var params = JSON.parse(e.postData.getDataAsString());
  var button = params.button;
  var LastRow = sheet.getLastRow();

  sheet.getRange(LastRow + 1, 1).setValue(new Date()); 
  sheet.getRange(LastRow + 1, 2).setValue(button); 
}

After testing, I also found the same problem, which is under study to solve it, please wait a moment.

Hello, Thank you for your reply and studying the problem.

For the sample code we provide:

/**
   BasicHTTPSClient.ino
    Created on: 14.10.2018
*/

#include <Arduino.h>
#include <rpcWiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>

const char* test_root_ca = \
                            "-----BEGIN CERTIFICATE-----\n"
                            "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n"
                            "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
                            "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"
                            "QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n"
                            "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
                            "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n"
                            "9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n"
                            "CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n"
                            "nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n"
                            "43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n"
                            "T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n"
                            "gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n"
                            "BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n"
                            "TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n"
                            "DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n"
                            "hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n"
                            "06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n"
                            "PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n"
                            "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n"
                            "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n"
                            "-----END CERTIFICATE-----\n";

WiFiClientSecure client;

void setup() {

  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.println();

  WiFi.mode(WIFI_STA);
  WiFi.begin("CHCK", "depot0510");

  // wait for WiFi connection
  Serial.print("Waiting for WiFi to connect...");
  while ((WiFi.status() != WL_CONNECTED)) {
    Serial.print(".");
  }
  Serial.println(" connected");
  client.setCACert(test_root_ca);
}

void loop() {
  if(&client) {
    {
      // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is 
      HTTPClient https;
  
      Serial.print("[HTTPS] begin...\n");
      if (https.begin(client, "https://www.example.com/index.html")) {  // HTTPS
        Serial.print("[HTTPS] GET...\n");
        // start connection and send HTTP header
        int httpCode = https.GET();
  
        // httpCode will be negative on error
        if (httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
  
          // file found at server
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            String payload = https.getString();
            Serial.println(payload);
          }
        } else {
          Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
        }
  
        https.end();
      } else {
        Serial.printf("[HTTPS] Unable to connect\n");
      }
      // End extra scoping block
    }
  
  } else {
    Serial.println("Unable to create client");
  }

  Serial.println();
  Serial.println("Waiting 10s before the next round...");
  delay(10000);
}

After testing, we found that "https.begin(client, "https://www.example.com/index.html")" is only suitable for “https://www.example.com/index.html” This site. If you change the website, you will not be able to connect. Since we are currently in China and cannot connect to the Google network, there is no way to test the connection of Google for you, but we have tested several websites. Removing the client parameter can solve the connection problem well, namely:
https.begin(“URL”)

1 Like

For more information, please refer to