/* * Contol a relay over WiFi using ESP32 to * turn ON or OFF 4 AC bulb or 4 fan or other load using 4 channel relay * * Watch Video instrution for this code:https://youtu.be/o6j_Qy3JTvU * * Full explanation of this code and wiring diagram is available at * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62 * Written by Ahmad Shamshiri on Feb 26, 2020 at 06:20 * in Ajax, Ontario, Canada. www.robojax.com * * Get this code and other Arduino codes from Robojax.com Learn Arduino step by step in structured course with all material, wiring diagram and library all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62 If you found this tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon http://robojax.com/L/?id=63 or make donation using PayPal http://robojax.com/L/?id=64 * * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* * This code has been download from Robojax.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Copyright (c) 2015, Majenko Technologies All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * Neither the name of Majenko Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const int relayCount =4;//total number of relays int relayPin[relayCount] ={12, 14, 27, 26};// int relayState[relayCount]={1, 1, 1, 1};//initial state . 1 OFF, 0 ON String buttonTitle1[relayCount] ={"Light 1 ON", "Light 2 ON", "Light 3 ON", "Light 4 ON"}; String buttonTitle2[relayCount] ={"Light 1 OFF", "Light 2 OFF", "Light 3 OFF", "Light 4 OFF"}; String argId[relayCount] ={"r1", "r2", "r3", "r4"}; #include #include #include #include const char *ssid = "YOUR WIFI SSID"; const char *password = "YOUR WIFI PASSWORD"; WebServer server(80); void handleRoot() { //Robojax.com ESP32 2 Relay Control String HTML ="\ \ \ \t\nRobojax ESP32 2 Relay Control\ \t\n\ \n\n\n\n\n\n

Robojax ESP32 Relay Control

\n"; for(int i=0; i< relayCount; i++){ if(relayState[i]){ HTML +="\t
\n\t\t"; HTML +=buttonTitle1[i]; //Light ON title }else{ HTML +="\t\n\n"; } HTML +="\t\n\n\n"; server.send(200, "text/html", HTML); }//handleRoot() void handleNotFound() { //Robojax.com ESP32 Relay Control String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); //Robojax.com ESP32 Relay Control }//end of handleNotFound() void setup(void) { //Robojax.com ESP32 Relay Control for(int i=0; i< relayCount; i++){ pinMode(relayPin[i], OUTPUT);// define a pin as output for relay digitalWrite(relayPin[i], relayState[i]);//initial state either ON or OFF } Serial.begin(115200);//initialize the serial monitor Serial.println("Robojax ESP32 Relay Control"); //Relay control ON OFF by Robojax.com WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: http://"); Serial.println(WiFi.localIP()); //multicast DNS //Robojax.com ESP32 Relay Control if (MDNS.begin("robojaxESP32")) { Serial.println("MDNS responder started"); Serial.println("access via http://robojaxESP32"); } server.on("/", handleRoot); server.on("/relay", HTTP_GET, relayControl); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); }//end of setup void loop(void) { //Robojax ESP32 Relay Control server.handleClient(); for(int i=0; i< relayCount; i++){ if(relayState[i] ==1) { digitalWrite(relayPin[i], HIGH); }else{ digitalWrite(relayPin[i], LOW); } //printRelayState(i);//prints for debugging purpose //will slow your ESP32. Do not use it for normal operation } delay(100); //Robojax ESP32 Relay Control }//end of loop /* * relayControl() * updates the value of "relayState" varible to 1 or zero * returns nothing * written by Ahmad Shamshiri * on Wednesday Feb 22, 2020 at 16:20 in Ajax, Ontario, Canada * www.robojax.com */ void relayControl() { for(int i=0; i< relayCount; i++){ if(server.arg(argId[i]) == "on") { relayState[i] = 0;// set state of relay to ON }else if(server.arg(argId[i]) == "off"){ relayState[i] = 1; // set state of relay to OFF } } handleRoot(); }//relayControl() end /* * printRelayState() * prints state of each relay for degubbing purpose * returns nothing * written by Ahmad Shamshiri * on Wednesday Feb 26, 2020 at 06:10 in Ajax, Ontario, Canada * www.robojax.com */ void printRelayState(int id) { Serial.print(buttonTitle1[id]); Serial.print(" : "); Serial.println (relayState[id]); delay(500); }//printRelayState end