Other Arduino Codes and Videos by Robojax

IoT Project: Use ESP32 to control DC Motor over WiFi with Relay - Grab-n-Go

دروس آردوینو به فارسی

IoT Project: Use ESP32 to control DC Motor over WiFi with Relay - Grab-n-Go

Using this Arduino code we can DC motor using ESP32 and 2 channel relay over WiFi either from Computer or tablet or mobile phone. this is Grab-n-Go (GNG) video where you can grab the code and run the program.

HTML page interface to control Motor over WiFi

HTML page to control 4 relays

Motor used in this demonstration is HTBRR04206705B

The link to use in the "preferences" of Arduino IDE for ESP32 board https://dl.espressif.com/dl/package_esp32_index.json Watch video for instructions.
Purchas 2 channel relay from Amazon
Purchas 2 channel relay from Canada

Resources for this sketch

Basic code to control direction of rotation of motor with 2 relay


 /*
 * Control DC Motor over WiFi using ESP32   
 * usng dual relay module
 * 
 * Watch Video instrution for this code:https://youtu.be/nNBUWt-zmOM
 * 
 * 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 March 03, 2020 at 22:22
 * 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 <https://www.gnu.org/licenses/>.

   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.
*/

int relayON = LOW;
int relayOFF = HIGH;
const int relay1Pin =12;
const int relay2Pin =14;
int motorDirection = 0;//1=CCW 0=CW
int motorState =0;//initial motor state 0=off, 1=on

String buttonTitle1[] ={"CCW", "CW", "Motor ON"};
String buttonTitle2[] ={"CCW", "CW", "Motor OFF"};
String argId[] ={"ccw", "cw", "do"};


#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "Your WiFi SSID";
const char *password = "Your WiFi password";

WebServer server(80);

void handleRoot() {
   //Robojax.com ESP32 Relay Motor Control
 String HTML ="<!DOCTYPE html>\
  <html>\
  <head>\
  	
<title>Robojax ESP32 Relay Motor Control</title>\
  	
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
  
<style>\
 
html,body{	
width:100%\;
height:100%\;
margin:0}
*{box-sizing:border-box}
.colorAll{
	background-color:#90ee90}
.colorBtn{
	background-color:#add8e6}
.angleButtdon,a{
	font-size:30px\;
border:1px solid #ccc\;
display:table-caption\;
padding:7px 10px\;
text-decoration:none\;
cursor:pointer\;
padding:5px 6px 7px 10px}a{
	display:block}
.btn{
	margin:5px\;
border:none\;
display:inline-block\;
vertical-align:middle\;
text-align:center\;
white-space:nowrap}
";
   
  HTML +="</style>

</head>

<body>
<h1>Robojax ESP32 Relay Motor Control </h1>
";

   if(motorDirection ==0 && motorState){
    HTML +="
	<h2><span style=\"background-color: #FFFF00\">Motor Running in CCW</span></h2>
";    
   }else if(motorDirection ==1 && motorState){
    HTML +="
	<h2><span style=\"background-color: #FFFF00\">Motor Running in CW</span></h2>
";      
   }else{
    HTML +="
	<h2><span style=\"background-color: #FFFF00\">Motor OFF</span></h2>
";    
   }
      if(motorDirection ==0){
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton\" style=\"background-color:#f56464\"  href=\"/motor?";
        HTML += argId[0];
        HTML += "=off\">";
        HTML +=buttonTitle1[0]; //relay ON title
      }else{
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton \" style=\"background-color:#90ee90\"  href=\"/motor?";  
         HTML += argId[0];
        HTML += "=on\">";       
        HTML +=buttonTitle2[0];//relay OFF title  
      }   
     HTML +="</a>	
	</div>

";     

      if(motorDirection ==1){
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton\" style=\"background-color:#f56464\"  href=\"/motor?";
        HTML += argId[1];
        HTML += "=off\">";
        HTML +=buttonTitle1[1]; //relay ON title
      }else{
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton \" style=\"background-color:#90ee90\"  href=\"/motor?";  
         HTML += argId[1];
        HTML += "=on\">";       
        HTML +=buttonTitle2[1];//relay OFF title   
      }   
     HTML +="</a>	
	</div>

";     

      if(motorState ==0 ){
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton\" style=\"background-color:#90ee90\"  href=\"/motor?";
        HTML += argId[2];
        HTML += "=on\">";
        HTML +=buttonTitle1[2]; //relay ON title
      }else{
        HTML +="	<div class=\"btn\">
		<a class=\"angleButton \" style=\"background-color:#f56464\"  href=\"/motor?";
        HTML += argId[2];
        HTML +="=off\">";      
        HTML +=buttonTitle2[2];//relay OFF title  
      }   
     HTML +="</a>	
	</div>

";     
   

  HTML +="	
</body>
</html>
";
  server.send(200, "text/html", HTML);  
}//handleRoot()

void handleNotFound() {
  //Robojax.com ESP32 Relay Motor Control
  String message = "File Not Found

";
  message += "URI: ";
  message += server.uri();
  message += "
Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "
Arguments: ";
  message += server.args();
  message += "
";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "
";
  }

  server.send(404, "text/plain", message);
 //Robojax.com ESP32 Relay Control
}//end of handleNotFound()

void setup(void) {
  //Robojax.com ESP32 Relay Motor Control 
 

    pinMode(relay1Pin, OUTPUT);// define a pin as output for relay
    digitalWrite(relay1Pin, relayOFF);//initial state either ON or OFF
    
    pinMode(relay2Pin, OUTPUT);// define a pin as output for relay
    digitalWrite(relay2Pin, relayOFF);//initial state either ON or OFF

 
  Serial.begin(115200);//initialize the serial monitor
  Serial.println("Robojax ESP32 Relay Motor 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("/motor", HTTP_GET, relayControl);           
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");

}//end of setup

void loop(void) {
  //Robojax ESP32 Relay Motor Control
  server.handleClient();

   if(motorState ==1)
    {
      controlMotor();     
    }else{
      motorStop(); 
    }
 


   delay(100);
 //Robojax ESP32 Relay Motor 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() {


    if(server.arg(argId[0]) == "on")
    {
      motorDirection = 0;// CCW 
               
    }else if(server.arg(argId[1]) == "on"){
      motorDirection = 1;  // CW  
          
    }else if(server.arg(argId[2]) == "on"){
      motorState =1;;  // turn the motor ON   
    }else if(server.arg(argId[2]) == "off"){
      motorState =0;;  // turn the motor OFF    
    }    

  

  handleRoot();
}//relayControl() end

/*
 * controlMotor()
 * controls the relay so the motor rotates in CCW/CW
 */
void controlMotor()
{
  if(motorDirection == 1){
    digitalWrite(relay1Pin, LOW);// turn relay 1 ON
    digitalWrite(relay2Pin, HIGH);// turn relay 2 OFF 
    //Serial.println("Rotating in CCW");      
  }else{
    digitalWrite(relay1Pin, HIGH);// turn relay 1 OFF
    digitalWrite(relay2Pin, LOW);// turn relay 2 ON 
          //Serial.println("Rotating in CW");  
  }
  
}// controlMotor()


/*
 * motorStop()
 * controls the relay so the motor is stopped
 */
void motorStop()
{
  digitalWrite(relay1Pin, relayOFF);// turn relay 1 OFF
  digitalWrite(relay2Pin, relayOFF);// turn relay 2 OFf 
  //Serial.println("Stopped");    
}


   

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal