ด้าน google sheet
สร้างไฟล์ google sheet ที่ต้องการ

ที่ลิ้งค์ด้านบน จะมี Sheet id ตามตัวอย่างด้านล่าง ให้ก็อปปี้เอาไว้

เลือกที่ ส่วนขยาย>Apps Scritp

จะเจอหน้าสำหรับใส่ Script ให้ส่งสคริปด้านล่างนี้

ดัานล่างนี้จะเป็นสคริปสำหรับรับค่า temp และ humid
function doGet(e) {
Logger.log(JSON.stringify(e));
var result = 'Ok';
if (e.parameter == 'undefined') {
result = 'No Parameters';
}
else {
var sheet_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Spreadsheet ID
var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
var newRow = sheet.getLastRow() + 1;
var rowData = [];
var Curr_Date = new Date();
rowData[0] = Curr_Date; // Date in column A
var Curr_Time = Utilities.formatDate(Curr_Date, "Asia/Bangkok", 'HH:mm:ss');
rowData[1] = Curr_Time; // Time in column B
for (var param in e.parameter) {
Logger.log('In for loop, param=' + param);
var value = stripQuotes(e.parameter[param]);
Logger.log(param + ':' + e.parameter[param]);
switch (param) {
case 'temperature':
rowData[2] = value; // Temperature in column C
result = 'Temperature Written on column C';
break;
case 'humidity':
rowData[3] = value; // Humidity in column D
result += ' ,Humidity Written on column D';
break;
default:
result = "unsupported parameter";
}
}
Logger.log(JSON.stringify(rowData));
var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
newRange.setValues([rowData]);
}
return ContentService.createTextOutput(result);
}
function stripQuotes(value) {
return value.replace(/^["']|['"]$/g, "");
}
ในส่วนของ sheet id ให้เอา id จากลิ้งค์ของไฟล์ google sheet ที่เคยก็อปปี้ไว้แล้ว มาใส่ตามตัวอย่างด้านล่าง



เลือกสร้างเป็นเว็บแอป

เลือกสิทธิการเข้าถึงเป็นทุกคน



จะได้ URL ของ google sheet api ให้นำไปใส่ในโค้ด ESP8266

ทดสอบโดยการนำลิ้งค์ที่ได้มาต่อด้วยข้อความด้านล่าง (เป็น api สำหรับ http get)
?temperature=25&humidity=55

จะเห็นว่า api นี้สามารถใช้งานได้กับ google sheet โดยใช้ http get

ด้าน ESP8266
จะใช้ตัวอย่างของ http get ตามรูป

และเพิ่มเติมสำหรับอ่านค่าจาก dht โดยส่งค่าขึ้น google sheet ทุก 10 วินาที
จะได้โค้ดตามด้านล่างนี้
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
BasicHTTPSClient.ino | |
Created on: 20.08.2018 | |
*/ | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClientSecureBearSSL.h> | |
#include "DHT.h" | |
#ifndef STASSID | |
#define STASSID "your-ssid" | |
#define STAPSK "your-password" | |
#endif | |
ESP8266WiFiMulti WiFiMulti; | |
#define DHTPIN D7 | |
#define DHTTYPE DHT11 // DHT 11 | |
// #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
DHT dht(DHTPIN, DHTTYPE); | |
String GSHEET_API = "https://script.google.com/macros/s/AKfycbwnw55UmfUBB3M9ZTPWwmPjoZ1dQ_glG11TgRI94RA980hmTni7dAbR6R782pO46AxS/exec"; | |
void setup() { | |
Serial.begin(115200); | |
// Serial.setDebugOutput(true); | |
Serial.println(); | |
Serial.println(); | |
Serial.println(); | |
WiFi.mode(WIFI_STA); | |
WiFiMulti.addAP(STASSID, STAPSK); | |
Serial.println("setup() done connecting to ssid '" STASSID "'"); | |
dht.begin(); | |
} | |
void loop() { | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
// wait for WiFi connection | |
if ((WiFiMulti.run() == WL_CONNECTED)) { | |
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure); | |
// client->setFingerprint(fingerprint_sni_cloudflaressl_com); | |
// Or, if you happy to ignore the SSL certificate, then use the following line instead: | |
client->setInsecure(); | |
HTTPClient https; | |
String url = GSHEET_API; | |
url += "?temperature="; | |
url += String(t, 1); | |
url += "&humidity="; | |
url += String(h, 1); | |
Serial.print("[HTTPS] begin...\n"); | |
if (https.begin(*client, url)) { // 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"); | |
} | |
} | |
Serial.println("Wait 10s before next round..."); | |
delay(10000); | |
} |
No responses yet