/*
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 Relay3_Pin = 18;
const int Relay4_Pin = 19;
const int SW1_Pin = 14;
const int SW2_Pin = 12;
const int SW3_Pin = 27;
const int SW4_Pin = 26;
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(Relay3_Pin, OUTPUT);
pinMode(Relay4_Pin, OUTPUT);
pinMode(SW1_Pin, INPUT_PULLUP);
pinMode(SW2_Pin, INPUT_PULLUP);
pinMode(SW3_Pin, INPUT_PULLUP);
pinMode(SW4_Pin, INPUT_PULLUP);
attachInterrupt(SW1_Pin, IO_INT_ISR1, RISING);
attachInterrupt(SW2_Pin, IO_INT_ISR2, RISING);
attachInterrupt(SW3_Pin, IO_INT_ISR3, RISING);
attachInterrupt(SW4_Pin, IO_INT_ISR4, 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 IO_INT_ISR3(){
//Serial.println("SW3");
digitalWrite(Relay3_Pin,!digitalRead(Relay3_Pin));
if(!manualSW){
http.end();
}
manualSW = 1;
}
void IO_INT_ISR4(){
//Serial.println("SW4");
digitalWrite(Relay4_Pin,!digitalRead(Relay4_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);
String R3 = s.substring(3, 2);
String R4 = s.substring(3, 2);
Serial.println(R1);
Serial.println(R2);
int relay_number1 = R1.toInt();
int relay_number2 = R2.toInt();
int relay_number3 = R2.toInt();
int relay_number4 = R2.toInt();
digitalWrite(Relay1_Pin,relay_number1);
digitalWrite(Relay2_Pin,relay_number2);
digitalWrite(Relay3_Pin,relay_number3);
digitalWrite(Relay4_Pin,relay_number4);
}
}
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);
bool Relay3 = digitalRead(Relay3_Pin);
bool Relay4 = digitalRead(Relay4_Pin);
String urlFinal= manualMode_GOOGLE_SCRIPT_URL+"?SW1="+ Relay1 +"&SW2="+ Relay2+"&SW3="+ Relay3+"&SW4="+ Relay4;
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();
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
relay2:VCC
relay2:GND
relay2:IN
relay2:NC
relay2:COM
relay2:NO
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
relay3:VCC
relay3:GND
relay3:IN
relay3:NC
relay3:COM
relay3:NO
relay4:VCC
relay4:GND
relay4:IN
relay4:NC
relay4:COM
relay4:NO