/*
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;
const int SW1_Pin = 14;
const int SW2_Pin = 12;
String s = " ";
int manualSW = 0; // 0 : ไม่กด 1 : กด
/**************************************/
// Google sc cript Ip and reguired credentials
String GOOGLE_SCRIPT_ID = "AKfycbyGWrhahjMqbbDnh4Htx5w0SP2TcnVOTRMo7ECFjfvyv3Ja2Wu5SDqmrDblI1dkPFQM";// change Google script ID
String GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbz9vsScL3yPAR3eFw3CNalZgYQWX9d2Nv6TTjyOVxHaDFmb-81G4fEOSwAxi6M2pswY/exec";
String manualMode_GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbyqwvjF6Vr4n3o837I_D5vHDh8oZ6-h6Y0r3HlWh1QCcb_8bfFQO2LSUCs2JLLs2mjj/exec";
unsigned long previousTime1 = millis();
long timeInterval1 = 10000; //10 วินาที
// Object http form HTTPClient
HTTPClient http;
void setup() {
Serial.begin(115200);
pinMode(Relay1_Pin, OUTPUT);
pinMode(Relay2_Pin, OUTPUT);
pinMode(SW1_Pin, INPUT_PULLUP);
pinMode(SW2_Pin, INPUT_PULLUP);
attachInterrupt(SW1_Pin, IO_INT_ISR1, RISING);
attachInterrupt(SW2_Pin, IO_INT_ISR2, RISING);
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 IO_INT_ISR1(){
//Serial.println("SW1");
digitalWrite(Relay1_Pin,!digitalRead(Relay1_Pin));
if(!manualSW){
http.end();
}
manualSW = 1;
}
void IO_INT_ISR2(){
//Serial.println("SW2");
digitalWrite(Relay2_Pin,!digitalRead(Relay2_Pin));
if(!manualSW){
http.end();
}
manualSW = 1;
}
void loop() {
unsigned long currentTime = millis();
if(currentTime - previousTime1 >= timeInterval1){
previousTime1 = currentTime;
dataLogger();
controlRelay();
}
if(manualSW){
manualSW = 0;
manualMode();
controlRelay();
}
delay(200);
}
void controlRelay(){
if(!manualSW){
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);
}
}
void dataLogger(){
// 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
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();
}
void manualMode(){
bool Relay1 = digitalRead(Relay1_Pin);
bool Relay2 = digitalRead(Relay2_Pin);
String urlFinal= manualMode_GOOGLE_SCRIPT_URL+"?SW1="+ Relay1 +"&SW2="+ Relay2;
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();
previousTime1 = millis();
}