ESP01 Relay Control With a WebPage (alternate Way)

by rightbass in Circuits > Arduino

218 Views, 1 Favorites, 0 Comments

ESP01 Relay Control With a WebPage (alternate Way)

in a box.jpeg
assembly.jpg
ftdi.jpg
schematic.PNG
webpage.jpg

First of all i have to point out that this instructable's goal is to show alternative ESP01 driving method. As you know ESP01 relay module comes with its own firmware and you don't need to write any code whatsoever. Well i didn't know that. And first thing i did was to flash my own code using ftrl device. Of course it didn't work. After a research i found out that is doesn't work that way and i couldn't undo what i did. So i dived deep into the rabbit hole :)

Instead of ordering new relay module, i opted out to use what i already had at hand. That is Arduino Nano, ESP-01, 5V relay module, and couple of 10k resistors.

In this project, ESP-01 acts as a sensor unit, and Arduino nano acts as a switch to drive relay module. Both codes for mcu's are attached. Some parts of the code are in Turkish but they are non essential and shouldn't be able to keep you understand the code.

On final note, i didn't intend to create this instructable. Therefore there isn't much documentation. Sole purpose of this instructable is powering ESP-01 as a standalone unit in a very primitive way.

Supplies

This project relies heavily on article below.

https://randomnerdtutorials.com/esp32-esp8266-input-data-html-form/

Preparing Circuit Board & Programming Nano

pins.jpg
schematic.PNG

The whole setup is drawn in the schematic. Arduino's usb connection powers the whole thing. Esp-01 and relay board get their 3,3v and 5v power supply from arduino respectively.

+ESP's output enters arduinos analog pin because it outputs 3,3v it must be read as an analog input.

+ESP must be powered on with its CH_PD,RST,GPIO0 pins HIGH. Since this project uses GPIO0 pin as an output, it is connected to arduino's D5 (pwm) pin. This is pretty much why this project needs an arduino. Relay board's 5V input doesn't help things either :) In code while mcu's are prepping, pin D5 pulled high(3,3v hence the pwm pin). Once it is done it leaves the GPIO0 to do its job.

Arduino Nano's code goes like this:

int tresh = 0;  


void setup() {
  // put your setup code here, to run once:
analogWrite(5,167);    // pulling GPIO0 to high(3,3v) on startup
pinMode(A5, OUTPUT);   // 
digitalWrite(A5, HIGH);// closes relay
delay(2000);
analogWrite(5,0);   // leaving GPIO0 to itself.
}


void loop() {
  // put your main code here, to run repeatedly:
tresh = analogRead(A7);   // GPIO0's voltage


if(tresh <= 400)   // reads esp's output
do{
  delay(100);
digitalWrite(A5, LOW);
tresh = analogRead(A7);
}
while(tresh<400);
digitalWrite(A5, HIGH);
}

Prepping ESP-01

I don't have any experinence with coding for ESP-01. So i heavily relied on article aforementioned above. Since it is well documented and with tons of examples online i don't have a need to explain the whole code. (And probably i wouldn't explain well :) )

Project's web page deals with 3 input parameters which are a string, an integer, and a float. I used the integer and float as a input variable, and the string as a modifier parameter.

When the time (only the hours) hits the input parameters (with ceartain parameters of course) the relay activates. Relay can only operate on beginnings of the hours.

This project reeks amateurism all over. But it does all i want it from. I'm using this project to operate my gas heater to work in certain hours.

Below code is rudimentary, not elegant, but it works and it works well for me. Whole code is attached in the project folder. So the controlling parameters is like this:

void loop() {
  // put your main code here, to run repeatedly:


timeClient.update();
int curr = timeClient.getHours(); // current time in hours (00-24)
int swmod1;
String mod1 = readFile(SPIFFS, "/inputString.txt");
int set2 = readFile(SPIFFS, "/inputInt.txt").toInt();
int set3 = readFile(SPIFFS, "/inputFloat.txt").toInt();
if (mod1 == "A"){  swmod1=1;} // since mod1 is a string, i can't use it in switch-case statements.
if (mod1 == "B"){  swmod1=2;} // so this is my workaround.
if (mod1 == "C"){  swmod1=3;}
if (mod1 == "D"){  swmod1=4;}
switch(swmod1){
  case 1: 
  if( set2 == curr || set3 == curr )
{delay(500);
digitalWrite(0, LOW); // arduino sees no voltage from now on
delay(100);}
else{
  digitalWrite(0, HIGH);}
  break;
  case 2: 
  if( set2 == curr || set3 == curr || set3+1 == curr ) // with modifier parameters i can manipulate "on" times
{
delay(500);
digitalWrite(0, LOW);
delay(100);}
else{
  digitalWrite(0, HIGH);}
  break;
  case 3: 
  if( set2 == curr || set3 == curr || set3+1 == curr || set2+1 == curr )
{
delay(500);
digitalWrite(0, LOW);
delay(100);}
else{
  digitalWrite(0, HIGH);}
  break;
  case 4: 
  if( set2 == curr || set3 == curr || set2+1 == curr )
{
delay(500);
digitalWrite(0, LOW);
delay(100);}
else{
  digitalWrite(0, HIGH);}
  break;
  default: // if there is no modifier parameter, relay will be open for only one hour
  if( set2 == curr || set3 == curr ) // like parameter A
{
delay(500);
digitalWrite(0, LOW);
delay(100);}
else{
  digitalWrite(0, HIGH);}
  break;
  
}
  
  }