NodeMCU 센서 데이터 구글 스프레드시트 연동
구글 앱 스크립트 코드
NodeMCU 소스코드
//----------------------------------------Include the NodeMCU ESP8266 Library
//----------------------------------------see here: https://www.youtube.com/watch?v=8jMr94B8iN0 to add NodeMCU ESP12E ESP8266 library and board (ESP8266 Core SDK)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
//----------------------------------------
//----------------------------------------SSID and Password of your WiFi router.
const char* ssid = "KT_GiGA_2G_EA84";
const char* password = "7bcb1hx383";
//----------------------------------------
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
String GAS_ID = "AKfycbxkeWmLaQHqwRQAjLVYKyhnWgP0luxl-3M8d0qZrbIMvB3ljL9d_sQcRiTvQS9U7sA2"; //--> spreadsheet script ID
//============================================================================== void setup
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(500);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
}
//----------------------------------------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
void loop() {
float t = analogRead(A0);
Serial.println("==========");
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String string_temperature = String(t);
String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature;
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();
//----------------------------------------
}
검색에 검색..
삽질을 해서 간신히 통신 성공..
코드는 좀 더 정리가 필요하다.