Using ESP 01 in Access Point (AP) mode to control ESP relay switch | ESP wifi tutorial | Part - 1

Control ESP relay switch using wifi access point on ESP 01

In the last tutorial, we have seen how to control relay using ESP 01 as micro controller. In this tutorial, i will attempt to introduce ESP relay switch designed to work seamlessly with ESP 01.  we will also control the relay in built wifi capability of ESP 01. We will attempt to use Access Point (AP) Mode which allows devices like mobile, laptop and other esps to connect to wifi network created by esp. This mode can be used to create a local network to control and communicate between master and slave nodes. We will setup a web server on ESP 01, use it to change relay state on a android mobile.

ESP Relay Switch

This is a relay switch with inbuilt slot for ESP 01. It has 5v and GND terminal to power relay and ESP 01. It has NC, COM and NO terminal to collect output of the relay.


ESP relay switch front and back view.

Hardware requirements

1. ESP relay switch.
2. Multi output voltage converter power module
3. Led Strip connected to female 3.5 mm connector
4. Jumper wires

Software setup

Code for reference.....
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

#define RELAY 0

const char* ssid = "Sensorism";  // Enter Wifi network name
const char* password = "ESP01WifiRelaySwitch";  //Enter password for network

IPAddress local_ip(192, 168, 1, 5); // IP to access device
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

ESP8266WebServer server(80);
bool relayStatus = LOW;

void setup() {
  pinMode(RELAY, OUTPUT);
  digitalWrite(RELAY, relayStatus); // Relay is active HIGH trigger
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, !relayStatus); // Led is active LOW trigger

  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(local_ip, gateway, subnet);
  delay(2000);

  server.on("/", handleOnConnect);
  server.on("/on", handleRelayOn);
  server.on("/off", handleRelayOff);
  server.onNotFound(handleNotFound);

  server.begin();
}
void loop() {
  server.handleClient();
  digitalWrite(RELAY, relayStatus);
  digitalWrite(LED_BUILTIN, !relayStatus);
}

void handleOnConnect() {
  server.send(200, "text/html", SendHTML());
}

void handleRelayOn() {
  relayStatus = HIGH;
  server.send(200, "text/html", SendHTML());
}

void handleRelayOff() {
  relayStatus = LOW;
  server.send(200, "text/html", SendHTML());
}

void handleNotFound() {
  server.send(404, "text/plain", "Not found");
}

String SendHTML() {
  String html = "<!DOCTYPE html> <html><head><title>ESP 01 Relay Control</title></head><body>\n";
  html += "<h1>ESP 01 Web Server</h1><h3>Access Point(AP) Mode</h3>\n";
  html += "<p>RelayStatus: </p><a href=\"/off\">OFF</a> <a  href=\"/on\">ON</a>\n";
  html += "</body></html>";
  return html;
}

Check this post to compile and  the code to ESP 01.

Hardware setup

ESP relay switch 5v -> power module 5v
ESP relay switch GND -> power module GND
ESP relay NO terminal -> power module DC-IN
ESP relay COM terminal -> male 3.5 mm connector +ve terminal
Male 3.5 connector mm -ve terminal -> power module GND
LED strip with 3.5 mm female connecotr -> 3.5mm male connector



Now turn on the device using 12v adapter. Connect to wifi network ( Wifi name: Sensorism, password: ESP01WifiRelaySwitch) on your mobile. Once connected go to http://192.168.1.5/  in your browser. Now you can control the device using ON and OFF links on the webpage.



We learned about AP mode in this tutorial, we will learn about STAtion (STA) mode in the next tutorial



Comments

Popular posts from this blog

Microphone Sound Sensor explained | HW-484 | Interfacing with ESP 8266

[Experiment] DIY Robotic 2 wheel drive car using L298N motor driver and NodeMCU | ESP 8266