起初,我的问题来自我的小项目 IoT。目的,使用 ESP32 通过 Google App Script 向 Google Sheet 发送数据。
ESP32的任务是取两个随机数,通过http通过Google App Script(Web App)传给Google Sheet。
我的问题是,在 ESP32 和 Google App Script 之间的连接每次都失败之后,我只能向 Google Sheet 插入数据 3-4 条记录。我不确定这个问题。
我的代码有 2 个部分,第一个是 ESP32(Arduino IDE),第二个是 Google App Script
第一部分代码如下,非常简单。
#include <WiFi.h>
#include <WiFiClientSecure.h>
String t;
const char* host = "script.google.com";
const int httpsPort = 443;
const char* ssid = "MY_SSID";
const char* pass = "MY_SSID_PW";
String GAS_ID = "GAS_ID";
String GAS_Sheet = "dht_data";
WiFiClientSecure client;
long now = millis();
long lastMeasure = 0;
void setup() {
Serial.begin(115200); delay(500);
WiFi.begin(ssid, pass);
Serial.print("\nConnecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
}
Serial.print("\nSuccessfully connected to : "); Serial.println(ssid);
Serial.print("\nIP address: "); Serial.println(WiFi.localIP());
Serial.println();
client.setInsecure();
}
void loop() {
now = millis();
if (now - lastMeasure > 30000) {
lastMeasure = now;
float ValueSensor1 = random(2000, 4000) / 100.0;
float ValueSensor2 = random(6000, 8000) / 100.0;
Serial.println();
Serial.print("\ntemperature = "); Serial.print(ValueSensor1, 1);
Serial.print("\thumidity = "); Serial.print(ValueSensor2, 1);
Serial.println();
sendData(ValueSensor1, ValueSensor2);
}
}
void sendData(float SValue1, float SValue2) {
Serial.println("==========");
Serial.print("connecting to "); Serial.println(host);
//---- Connect to Google host
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
//---- Post Data
String url;
url += "/macros/s/" + GAS_ID + "/exec?";
url += "id=" + String(GAS_Sheet);
url += "&temperature=" + String(SValue1, 2);
url += "&humidity=" + String(SValue2, 2);
Serial.print("requesting URL: "); Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
//---- Wait Echo
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("ESP-32/Arduino CI successfull!");
} else {
Serial.println("ESP-32/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
}
第二部分代码如下,
function doGet(e){
// open the spreadsheet
var ss = SpreadsheetApp.getActive();
// use the 'id' parameter to differentiate between sheets
var sheet = ss.getSheetByName(e.parameter["id"]);
// 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] == "Timestamp") {
val = d.toDateString() + ", " + 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');
}
问题的原因是什么?我该如何解决?
非常感谢您提前。