/*
ESP32 HTTPClient Example
PVS 2/3
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/***************************************/
const int Relay1_Pin = 4;
const int Relay2_Pin = 2;
String s = " ";
/**************************************/
// Google sc cript Ip and reguired credentials
String GOOGLE_SCRIPT_ID = "AKfycbwe_JvEaZY1GHn-l-VdVVlSt0ZuWTqCL_fumnGO-I3m4FlU2xjTSYUWqI8YEPtGz6iv7Q";// change Google script ID
String GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbwe_JvEaZY1GHn-l-VdVVlSt0ZuWTqCL_fumnGO-I3m4FlU2xjTSYUWqI8YEPtGz6iv7Q/exec";
void setup() {
Serial.begin(115200);
pinMode(Relay1_Pin, OUTPUT);
pinMode(Relay2_Pin, OUTPUT);
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() {
/*-------------ส่งข้อมูลไป google sheet-----------------------*/
// Data to Send
float string_temp = 10.00;
float string_humi = 20.00;
int string_lux = 30.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
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);
// Getting response from webApp
String payload;
if (httpcode > 0){
payload = http.getString();
Serial.println("Payload : "+ payload);
s = payload;
}
http.end();
controlRelay();
delay(6000);
}
void controlRelay(){
String R1 = s.substring(0, 1);
String R2 = s.substring(3, 2);
Serial.println(R1);
Serial.println(R2);
int relay_number1 = R1.toInt();
int relay_number2 = R2.toInt();
digitalWrite(Relay1_Pin,relay_number1);
digitalWrite(Relay2_Pin,relay_number2);
}