สำหรับโปรเจคนี้ก็ไม่มีอะไรซับซ้อนนะครับเพียงแค่เราต้องการให้ Line notify ที่เขียน script ใน Google Sheet ส่งข้อมูลตามวันเวลาที่เราต้องการเท่านั้นเองยังไงก็ลองเอาไปทดสอบดูนะครับ
Downdload
อธิบายการใช้งานครับ (มีเสียงรบกวนเหมือนเดิม ครับ อย่าว่ากันนะ)
//ใช้กับ ESP8266 board manager Version 2.5.0 เป็นต้นไป
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "Nu-Electronic"; //เปลี่ยนเป็น SSID ของเบียร์
const char* password = "228228228"; //เปลี่ยนเป็น password ของเบียร์
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
String GAS_ID = "AKfycbw4PaIAlTmltzOjLDMgvcXjdWLZ9nAykeLLA4wLvSBIxSaVYho"; //--> spreadsheet script ID
//----------------------------------------Digital fingerprint
// How to get a fingerprint or new fingerprint, see here: https://www.youtube.com/watch?v=yMvFIKJYtjE&feature=youtu.be At minute 2:56
const char* fingerprint = "46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6";
//----------------------------------------
#define ON_Board_LED 2
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED, OUTPUT); //--> On Board LED port Direction output
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
//----------------------------------------
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------
client.setInsecure();
}
void loop() {
delay(20000);// ตั้งเวลาการส่ง ที่ 20 วินาที ถ้าต้องการให้ส่งส่งทุกๆ 5 วิ เราต้องดูเวลาจาก Google sheet
//ว่าเวลาที่แสดงห่างกันกี่ วิ แล้วจึงมาปรับ ค่าเวาลที่นี่เพื่อสอดคล้องกัน ในที่นี้ปรับเป็น 2 วินาที จะส้งเข้า google sheet ประมาณ 5วิ
sendData();
}
void sendData() {
Serial.println("==========");
Serial.print("connecting to ");
Serial.println(host);
//----------------------------------------Connect to Google host
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//----------------------------------------Verify fingerprint
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
//----------------------------------------
//----------------------------------------Processing data and sending data
//Esp8266 ตัวที่ 1 ID01
String url = "/macros/s/" + GAS_ID + "/exec?ID01&"+ String("Electrical_status=Normal") ;//ส่งเป็นภาษาไทยไม่ได้
//Esp8266 ตัวที่ 2 ID02
// String url = "/macros/s/" + GAS_ID + "/exec?ID02&" + "V=" + V_1 + "&I=" + I1 + "&COS=" + COS1 + "&P=" + P1 + "&S=" + S + "&Q=" + Q1;
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");
//----------------------------------------
//----------------------------------------Checking whether the data was sent successfully or not
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("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
//----------------------------------------
}
//==============================================================================
- //ใช้กับ ESP8266 board manager Version 2.5.0 เป็นต้นไป แต่ผมใช้ Version 2.5.0 เท่านั้นนะครับ