Other Arduino Codes and Videos by Robojax

How to control AC bulb or load using NodeMCU ESP8266 over WiFi with Relay

Download Arduino Sketch File

How to control AC bulb or load using NodeMCU ESP8266 over WiFi

Using this Arduino code we can control AC load such as AC bulb or fan using ESP8266 NodeMCU or D1 Mini over WiFi either from Computer or tablet or mobile phone.

Your Ardino IDE (Arduino Program) must be ready for ESP8266 (NodeMCU or D1 Mini). Else this code will not work.

Resources for this sketch

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


 /*
 * Contol a relay over WiFi using ESP8266 NodeMCU, D1 Mini to 
 * turn ON or OFF AC bulb or fan or other load
 * 
 * Watch Video instrution for this code: https://youtu.be/wdgWdxV60dg
 
 * 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 20, 2020 
 * 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.
*/

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

int relayPin = D1;//define a pin for relay
String buttonTitle1 ="Light ON";
String buttonTitle2 ="Light OFF";

int relayStat = 1;//initial state . 1 ON, 0 OFF


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>



#ifndef STASSID
#define STASSID "Robojax" // your WiFi SSID
#define STAPSK  "YourWiFiPassword"
#endif

const char *ssid = STASSID;
const char *password = STAPSK;

ESP8266WebServer server(80);

void handleRoot() {
 String HTML ="<!DOCTYPE html>\
  <html>\
  <head>\
  <title>Robojax ESP8266 Relay 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 ESP8266 Relay Control </h1>
";
  
  if(relayStat){
    HTML +="<div class=\"btn\"><a class=\"angleButton\" style=\"background-color:#90ee90\"  href=\"/relay?do=off\">";
    HTML +=buttonTitle1;  
  }else{
    HTML +="<div class=\"btn\"><a class=\"angleButton \" style=\"background-color:#f56464\"  href=\"/relay?do=on\">";
    HTML +=buttonTitle2;    
  }
  HTML +="</a></div>
</body>
</html>
";
  server.send(200, "text/html", HTML);  
}//handleRoot()

void handleNotFound() {

  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);

}

void setup(void) {
  pinMode(relayPin, OUTPUT);// define a pin as output for relay
  digitalWrite(relayPin, relayStat);//initial state either ON or OFF
  Serial.begin(115200);//initialize the serial monitor

  //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: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("robojaxESP8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.on("/relay", HTTP_GET, relayControl);         
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");

}

void loop(void) {
  
  server.handleClient();
  MDNS.update();

  if(relayStat)
  {
    digitalWrite(relayPin, LOW);
  }else{
    digitalWrite(relayPin, HIGH);    
  }

 //Serial.print(relayStat);

  delay(100);
}



   
Download Arduino Sketch File

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