งานครั้งที่ 44 [iot#19 GoogleSheetAPI] บันทึกค่าลงบน Google Sheet
Google Sheet เป็นโปรแกรมที่มีการทำงานคล้ายกับ Excell เพียงแต่ทำงานจะต้องมีการเชื่อมต่ออินเตอร์เน็ต หากงาน iot สามารถบันทึกข้อมูลต่าง ๆ ของบอร์ดลงใน Google Sheet ได้ผู้ใช้งานสามารถนำข้อมูลไปใช้งานได้อีกมากมายเนื่องจาก Google Sheet สามารถเขียนสูตรคำนวณค่าได้หรือผู้ใช้งานต้องการที่จะนำข้อมูลมาประมวลผลใน Excell ก็สามารถทำได้โดยง่ายการทดลองนี้จะทำการบันทึกค่าจำนวน 4 ค่าด้วยกันได้แก่
- ค่าที่อ่านได้จากพอร์ตแอนะล็อก
- ค่าอุณหภูมิ
- ค่าความชื้น
- ค่าเวลาที่ทำการวัด ซึ่งเป็นเวลาที่อ่านจากเซิร์ฟเวอร์นาฬิกาสากล
ซึ่งการเขียนโค้ดที่บอร์ดควบคุมจำเป็นต้องมีไลบรารี่สำหรับอ่านค่าจากโมดูลอ่านค่าอุณหภูมิและความชื้น ท่านใดยังไม่ได้ดำเนินการให้ศึกษาขั้นตอนได้ในงานครั้งที่ 8 การเขียนโปรแกรมอ่านค่าอุณหภูมิและความชื้นด้วย DHT11 เบื้องต้น (http://www.praphas.com/forum/index.php?topic=311.0)
คอนเซ็ปต์หลักของงาน
บอร์ดส่งค่าข้อมูลทั้ง 4 อย่างขึ้นบันทึกบน Google Sheet
[ขั้นตอนการดำเนินการ]
-สร้างโปรเจคงานใน Google API
-นรหัสไคลเอ็นต์ของโปรเจคงานมาใส่ในโค้ดโปรแกรม
-สร้างไฟล์ชีต Google Sheet เพื่อรับค่า
-เขียนโค้ดเพื่อใช้งาน
ซึ่งรายละเอียดเป็นดังนี้
1. วงจรที่ใช้ทดลอง ทั้งที่เป็นบอร์ด NodeMCU และ WeMOS D1 mini
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/1.png)
2. ผลที่ต้องการในงานครั้งนี้เป็นการนำค่าที่ได้บันทึกลง Google sheet
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/36.png)
ส่วนจัดการ Google API
1. เริ่มขั้นตอนสร้างโปรเจค โดยเข้าไปที่ https://console.developers.google.com
(1) ล็อกอินด้วยอีเมล์ gmail (หากไม่มีให้สมัครใช้งาน gmail ก่อน)
(2) ในกรณีที่เคยสร้างโปรเจคไว้แล้ว (เป็นโปรเจคของงาน Firebase ก็สามารถใช้ได้)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/2.png)
2. ในกรณีที่ยังไม่มีโปรเจคงาน สามารถสร้างโปรเจคได้ 2 จุด (1) หรือ (2)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/3.png)
3. กรณีที่คลิก "เลือกโครงการ" ให้คลิกเพิ่มโปรเจคที่ปุ่ม (+)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/4.png)
4. กรณีที่คลิก "สร้างโครงการ" จะปรากฏหน้าต่างให้คลิก "สร้าง"
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/5.png)
5. ทำการตั้งชื่อโครงการ (1) แล้วคลิกสร้าง (2)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/6.png)
6. ชื่อโครงการจะปรากฏขึ้น (1) แล้วคลิกดูรายการ API ที่ "ดูทั้งหมด..." (2)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/7.png)
7. เลื่อนหา API ที่ชื่อว่า "Google Sheet API" แล้วคลิก
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/8.png)
8. คลิกเปิดใช้
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/9.png)
9. เมื่อสร้างเสร็จจะได้ คลิก "สร้างข้อมูลรองรับ"
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/10.png)
10. คลิก "สร้างข้อมูลรองรับ"
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/11.png)
11. คลิก "รหัสไคลเอ็นต์ OAuth"
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/12.png)
12. คลิกตามรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/13.png)
13. ตั้งชื่อตามต้องการ (ควรเป็นภาษาอังกฤษ)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/14.png)
14. คลิกตามรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/15.png)
15. ผลจะได้ รหัสที่จะนำไปใช้งาน
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/16.png)
16. ผู้ใช้งานสามารถคัดลอกรหัสดังกล่าวได้ในโปรแกรมอื่นก่อนก็ได้ เช่นไปวางไว้ในโปรแกรม Notepad
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/17.png)
17. หรือผู้ใช้สามารถดูรหัสภายหลังได้ โดยคลิกชื่อรหัสไคลเอ็นต์(ที่ได้สร้างไว้)ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/18.png)
18. รหัสจะได้
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/19.png)
19. ทำการสร้างเอกสาร Google Sheet ในกูเกิลไดร์ฟ คลิกที่่ (1)->(2)
https://drive.google.com/drive/my-drive (https://drive.google.com/drive/my-drive)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/20.png)
20. รหัสของไฟล์ Google Sheet จะอยู่ระหว่างเครื่องหมาย / ดังรูปทำการคัดลอกเพื่อนำไปใช้งาน (ชื่อชีตสามารถแก้ไขได้โดยการคลิกที่ชื่อแล้วพิมพ์ชื่อใหม่ตามต้องการ)
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/21.png)
ส่วนจัดการโค้ด
การใช้งาน Google Sheet API จำเป็นต้องใช้ไลบรารี่ช่วยงานโดยดำเนินการดังนี้
21. ดาวน์โหลดไลบรารี่โดยเข้าไปที่เวปไซต์ แล้วคลิกดาวน์โหลดตามลำดับ
https://github.com/KhonKaenMakerClub/EspGoogleApi
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/22.png)
22. ทำการติดตั้งไลบรารี่ที่ดาวน์โหลดมา ดำเนินการดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/23.png)
23. เปิดไฟล์ตัวอย่าง "InsertGoogleSheets" ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/24.png)
24. ใส่รหัสทั้ง 3 อย่างลงโค้ด
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/25.png)
25. ป้อนชื่อไวไฟและพาสเวิร์ดที่จะให้บอร์ดจับสัญญาณ
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/26.png)
26. ทำการอัพโหลด..แล้วเปิด Serial monitor เพื่อดูผล
(1) เข้าเวปไซต์ที่ปรากฏในหน้าจอ
(2) ใช้รหัสที่ปรากฏป้อนลงในเวปไซต์
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/27.png)
27. ป้อนรหัสที่ได้จากหน้าจอ Serial monitor
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/28.png)
28. เลือกบัญชี Gmail
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/29.png)
29. คลิกอนุญาต
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/30.png)
30. การดำเนินการเสร็จสิ้น
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/31.png)
31. เปิดกลับไปดูเอกสาร Google Sheet ที่สร้างไว้จะปรากฏข้อมูล
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/32.png)
32. หน้าต่าง Serial monitor จะปรากฏข้อความดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/33.png)
ข้อผิดพลาดที่อาจเกิดขึ้น
33. หากเกิดข้อผิดพลาดหน้าต่าง Serial monitor จะปรากฏข้อความดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/34.png)
34. ให้ทำการเพิ่มคำสั่ง true ท้ายฟังก์ชั่น gSheets.begin
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/35.png)
ตัวอย่างโปรแกรม
บันทึกค่าแอนะล็อก, อุณหภูมิ, ความชื้น, เวลา, วันเดือนปี ที่บันทึก
โค้ดโปรแกรม
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <GSheets.h>
#include <DHT.h>
#include <time.h>
#include <SimpleTimer.h>
#define DHTPIN D1 //pin connect DHT
#define DHTTYPE DHT11 //DHT11, DHT22 type of Sensor
//------ Config -------//
const String client_id = "------- client id -------";
const String client_secret = "------- secret ------";
const String sheet_id = "----- sheet id -----";
GSheets gSheets;
ESP8266WiFiMulti WiFiMulti;
char ntp_server1[20] = "pool.ntp.org";
char ntp_server2[20] = "time.nist.gov";
char ntp_server3[20] = "time.uni.net.th";
int timezone = 7*3600;
int dst = 0;
float h;
float t;
int adc;
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
void setup(){
Serial.begin(115200);
Serial.println();
for(uint8_t t = 5; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
delay(1000);
}
WiFiMulti.addAP("---ssid---", "--- password ---");
configTime(timezone, dst, ntp_server1, ntp_server2, ntp_server3);
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("");
dht.begin();
gSheets.begin(client_id,client_secret,sheet_id);
// gSheets.begin(client_id,client_secret,sheet_id,true);
timer.setInterval(10000, sendState);
}
void loop() {
Serial.println(NowTime());
Serial.println(NowDate());
delay(2500);
h = dht.readHumidity();
t = dht.readTemperature();
adc=analogRead(A0);
if (isnan(t) || isnan(h))
{
Serial.println("Error reading DHT!");
delay(500);
return;
}
digitalWrite(D0,LOW);
Serial.print("Analog value: ");
Serial.print(adc);
Serial.print("\t");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("\t");
Serial.print("Temp: ");
Serial.println(t);
Serial.println("...............................");
timer.run();
}
void sendState(){
if((WiFiMulti.run() == WL_CONNECTED)) {
gSheets.addColumn("Analog",String(adc));
gSheets.addColumn("Temperature",String(t));
gSheets.addColumn("Humidity",String(h));
gSheets.addColumn("Time",NowTime());
gSheets.addColumn("Date",NowDate());
if(gSheets.insertRow()){
Serial.println("---- inserted row ----");
}
}
}
String NowTime() {
time_t now = time(nullptr);
struct tm* p_tm=localtime(&now);
String timeNow = "";
timeNow += String(p_tm->tm_hour);
timeNow += ":";
timeNow += String(p_tm->tm_min);
timeNow += ":";
timeNow += String(p_tm->tm_sec);
return timeNow;
}
String NowDate() {
time_t now = time(nullptr);
struct tm* p_tm=localtime(&now);
String dateNow = "";
dateNow += String(p_tm->tm_mday);
dateNow += "/";
dateNow += String(p_tm->tm_mon + 1);
dateNow += "/";
dateNow += String(p_tm->tm_year + 1900);
return dateNow;
}
ทดสอบการทำงาน
35. ผลการบันทึกค่าลง Google Sheet
(http://www.praphas.com/PhotoForum/iot/Lab-44-GoogleSheetAPI/36.png)