/*
ESP32 HTTPClient Example
PVS 2/3
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/***************************************/
String s = " ";
// Google sc cript Ip and reguired credentials
String GOOGLE_SCRIPT_ID = "AKfycbzaGrxlg8paJvkI1qJJIQHxpWJaM4JMn4BWK9GtVQBOMZToFI7SElN500XqOoOEq7fx";// change Google script ID
String GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbzaGrxlg8paJvkI1qJJIQHxpWJaM4JMn4BWK9GtVQBOMZToFI7SElN500XqOoOEq7fx/exec";
unsigned long previousTime1 = millis();
long timeInterval1 = 60000; //10 วินาที
// Object http form HTTPClient
HTTPClient http;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password, 6);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentTime = millis();
if(currentTime - previousTime1 >= timeInterval1){
previousTime1 = currentTime;
dataLogger();
}
delay(200);
}
void dataLogger(){
// Data to Send
float string_temp = 100.00;
float string_humi = 200.00;
int string_lux = 300.00;
// URL & Data to Send
//String urlFinal= "https://script.google.com/macros/s/"+ GOOGLE_SCRIPT_ID + "/exec?FIELD1="+ string_temp +"&FIELD2="+ string_humi +"&FIELD3="+string_lux;
String urlFinal= GOOGLE_SCRIPT_URL+"?FIELD1="+ string_temp +"&FIELD2="+ string_humi +"&FIELD3="+string_lux;
Serial.print("Post data to spreadsheet!");
Serial.println(urlFinal);
// Object http form HTTPClient
//HTTPClient http;
// Connect to WebApp with URL
unsigned long startTime = millis();
http.begin(urlFinal.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
// Get HTTP status Code
int httpcode = http.GET();
Serial.print("HTTP status Code: ");
Serial.println(httpcode);
Serial.println(millis()-startTime);
// Getting response from webApp
String payload;
if (httpcode > 0){
payload = http.getString();
Serial.println("Payload : "+ payload);
s = payload;
}
http.end();
}