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);
}