Introduction

A level mechanism is used to open and close Vigyan Ashram’s main gate, which is IOT-based. Two tanks are present; one is positioned on the ground and the other is fixed to the gate’s rare end.

Water is pumped as soon as the condition is met by a motor, which controls the water level.

Component

  • ESP 32 Arduino
  • Float switch 1(open gate)
  • Float switch 2 (closed gate)
  • Relay 1 (pump in)
  • Wi-Fi indication LED
  • GND (common of all components)
  • VCC (common of all components)

Code

#include <Arduino.h>

#if defined(ESP32)

#include <WiFi.h>

#elif defined(ESP8266)

#include <ESP8266WiFi.h>

#endif

#include <Firebase_ESP_Client.h>

// Provide the token generation process info.

#include “addons/TokenHelper.h”

// Provide the RTDB payload printing info and other helper functions.

#include “addons/RTDBHelper.h”

// Insert your network credentials

#define WIFI_SSID “Workshop”

#define WIFI_PASSWORD “va412403”

//#define WIFI_SSID “VAIBT”

//#define WIFI_PASSWORD “VAIBT@123”

#define WIFI_SSID “Vigyan New Office”

#define WIFI_PASSWORD “rahul123456”

//#define WIFI_SSID “Galaxy balou”

//#define WIFI_PASSWORD “yolesang”

// Insert Firebase project API Key

#define API_KEY “AIzaSyDlTlJtGynzh7CQMMIyhIVQjoqOUA8t0m4”

// Insert Authorized Username and Corresponding Password

#define USER_EMAIL “vigyanashramfablab@gmail.com”

#define USER_PASSWORD “12345678”

-+922// I211/7*8nsert RTDB URLefine the RTDB URL

#define DATABASE_URL “gatecontrol-c8014-default-rtdb.firebaseio.com”

// Define Firebase objects

FirebaseData stream;

FirebaseAuth auth;

FirebaseConfig config;

// Variables to save database paths

String listenerPath = “Gate_State/”;

unsigned long sendDataPrevMillis = 0;

unsigned long timer = 0;

const long timer_reach = 2 * 60000;

bool start_timer = false;

bool open_bool = false;

bool close_bool = false;

unsigned long timer_closing = 0;

unsigned long timer_pump = 0;

bool pump_in_start = false;

bool pump_out_start = false;

// Declare outputs

const int LED = 2;

// Assign output variables to GPIO pins

const2222/ int water_pump_in = 16; //pin D7 //RX2 of ESP32

const int water_pump_out = 5; // pin D5

const int buzzer_indication = 2; // pin RX

// float sensor1

int FloatSensor1 = 21; //D4 pin of NodeMCU //D21 of ESP32

// float sensor2

int FloatSensor2 = 17; //D6 pin of NodeMCU //TX2 of ESP32

int Gate_State = 0;

int Gate_State_test = 0;

bool LED_Status = false;

void open_gate ();

void closed_gate ();

void pump_in();

void pump_out();

void timer_control();

// Initialize WiFi

void initWiFi() {

  Serial.print(“Connecting to WiFi ..”);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {

    Serial.print(‘.’);

    delay(*1000);

  }

  Serial.println(WiFi.localIP());

  Serial.println();

}

void setup() {

  Serial.begin(9600);

  initWiFi();

  // digitalWrite(LED, HIGH);

  // Initialize Outputs

  pinMode(LED, OUTPUT);

  delay(10);

  //motor define

  pinMode(water_pump_in, OUTPUT);

  pinMode(water_pump_out, OUTPUT);

  //wifi indication

  pinMode(buzzer_indication, OUTPUT);

  // motor initially low

  digitalWrite(water_pump_in, HIGH);

  digitalWrite(water_pump_out, HIGH);

  //float define

  pinMode(FloatSensor1, INPUT_PULLUP); //Arduino Internal Resistor 10K

  pinMode(FloatSensor2, INPUT_PULLUP); //Arduino Internal Resistor 10K

  // Assign the api key (required)

  config.api_key = API_KEY;

  // Assign the user sign in credentials

  auth.user.email = USER_EMAIL;

  auth.user.password = USER_PASSWORD;

  // Assign the RTDB URL (required)

  config.database_url = DATABASE_URL;

  Firebase.reconnectWiFi(true);

  // Assign the callback function for the long running token generation task */

  config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

  // Assign the maximum retry of token generation

  config.max_token_generation_retry = 5;

  // Initialize the library with the Firebase authen and config

  Firebase.begin(&config, &auth);

  digitalWrite (buzzer_indication, LOW);

  // digitalWrite(LED, LOW);

  delay(2000);

}

void loop() {

  if (pump_in_start == true){

    pump_in();

  }

  if (pump_out_start == true){

    pump_out();

  }

  if (Firebase.ready() && (millis() – sendDataPrevMillis > 1500 || sendDataPrevMillis == 0))            //Update firebase value every 1500ms

  {

  // if (LED_Status == false)

  // {

  //   LED_Status = true;

  // }else{

  //   LED_Status = false;

  // }

  // digitalWrite(LED, LED_Status);

    sendDataPrevMillis = millis();

    Serial.println( “Float 1”);

    Serial.println(digitalRead(FloatSensor1));

    Serial.println( “Float 2”);

    Serial.println(digitalRead(FloatSensor2));

    if (Firebase.RTDB.getBool(&stream, “/Gate_State/Open”)) {         //Get Gate State

      Gate_State_test = stream.boolData();

      Serial.println(Gate_State_test);

    }

    else {

      Serial.println(stream.errorReason());

    }

    Serial.print(“Gate State is :”);

    Serial.print(Gate_State);

    Serial.print(“nn”);

    // timer_control();        //Useless because time is disabled, should be in charge of closing the gate after a certain time — Uncomment if want to use timer

    if (close_bool == true) {   //Change Ack State

      Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

      pump_in_start = true;

    }

    if (open_bool == true) {    //Change Ack State

      Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

      pump_out_start = true;

    }

    if (Gate_State_test != Gate_State)    //Check if gate state has changed

    {

      Gate_State = Gate_State_test;

      if (Gate_State == 1) {    //Gate Should open

        open_gate();

        start_timer = true;   //Timer useless

        timer = millis() + timer_reach;

        Serial.print(“Timer Start”);

        open_bool = true;   //For ack of databse

      }

      if (Gate_State == 0) {    //Gate should close

        closed_gate();

        close_bool = true;    //For ack of database

      }

    }

  }

}

void timer_control() {        //Not called, because timer is disabled

  if (start_timer) {

    Serial.printf(“Set timer… %sn”, Firebase.RTDB.setInt(&stream, F(“/Gate_State/Timer”), (timer – millis())) ? “ok” : stream.errorReason().c_str());

  }

  if (start_timer == true && (millis() >= timer)) {

    Serial.print(“Gate is closing due to timer n”);

    start_timer = false;

    Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Open”), false) ? “ok” : stream.errorReason().c_str());          //Uncomment this line to activate timer !!

  }

  if (start_timer && Gate_State_test == 0) {

    Serial.printf(“Set timer… %sn”, Firebase.RTDB.setInt(&stream, F(“/Gate_State/Timer”), 0) ? “ok” : stream.errorReason().c_str());          //Uncomment this line to activate timer !!

    start_timer = false;

  }

}

void open_gate()    //when open gate

{

  Serial.println(digitalRead (FloatSensor1));

  int counter = 0;

  int Sensor1 = 0;

  while (counter <= 5)        //Float sensor must activated 5 times, to avoid noise

  {

    digitalWrite(water_pump_in, LOW);

    Serial.println( “Float 1”);

    Serial.println(Sensor1);

    Serial.println( “Float 2”);

    Serial.println(digitalRead(FloatSensor2));

    Sensor1 = digitalRead(FloatSensor1);

    if (Sensor1 == 1) {     //Increase counter each time float is activated

      ++counter;

      Serial.print(“Counter = “);

      Serial.println(counter);

    }

  }

  digitalWrite (buzzer_indication, HIGH);   //Activate buzzer to tell gate is opening

  delay (2000);

  digitalWrite(water_pump_in, HIGH);

  delay (4000);

  digitalWrite (buzzer_indication, LOW);

  Serial.println(digitalRead (FloatSensor1));

  delay(100);

  Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

  Serial.println(“Gate Open”);

  digitalWrite(water_pump_out, LOW);      //Start pumping out into to pre-empty

  Serial.println( “Pump out ON”);

  Serial.println(timer_pump);

  timer_pump = millis();

}

void closed_gate ()

{

  Serial.println(digitalRead (FloatSensor2));

  int counter = 0;

  int Sensor2 = 1;

  while (counter <= 25) //Float sensor must activated 25 times, to avoid noise : 25 because movement in water can activated it

  {

    Sensor2 =  (digitalRead(FloatSensor2));

    digitalWrite(water_pump_out, LOW);

    Serial.println( “Float 1”);

    Serial.println(digitalRead(FloatSensor1));

    Serial.println( “Float 2”);

    Serial.println(Sensor2);

    if (Sensor2 == 0) {     //Increase counter each time float is activated

      ++counter;

      Serial.print(“Counter = “);

      Serial.println(counter);

    }

  }

  digitalWrite (buzzer_indication, HIGH);   //Activate buzzer to tell gate is closing

  delay (10000);

  digitalWrite (buzzer_indication, LOW);

  delay (25000);

  digitalWrite(water_pump_out, HIGH);

  Serial.println(digitalRead (FloatSensor2));

  digitalWrite(water_pump_in, LOW);       //Start pumping back into to prefill

  Serial.println( “Pump in ON”);

  Serial.println(timer_pump);

  timer_pump = millis();

}

void pump_in()        //Pre-fill tank before next opening

{

  // water pump in ON

  Serial.println( “Float 1”);

  Serial.println(digitalRead(FloatSensor1));

  Serial.println( “Float 2”);

  Serial.println(digitalRead(FloatSensor2));

  Serial.print(“Timer is =”);

  Serial.println(millis() – timer_pump);

  if ((millis() – timer_pump) >=  (70*1000)) {

    digitalWrite(water_pump_in, HIGH);

    Serial.println( “Pump in OFF”);

    timer_pump = 0;

    pump_in_start = false;

    delay(10);

    close_bool = false;

  }

}

void pump_out()        //Pre-empty tank before next closing

{

  // water pump out ON

  Serial.println( “Float 1”);

  Serial.println(digitalRead(FloatSensor1));

  Serial.println( “Float 2”);

  Serial.println(digitalRead(FloatSensor2));

  Serial.print(“Timer is =”);

  Serial.println(millis() – timer_pump);

  if ((millis() – timer_pump) >=  (80*1000)) {

    digitalWrite(water_pump_out, HIGH);

    Serial.println( “Pump out OFF”);

    timer_pump = 0;

    pump_out_start = false;

    delay(10);

    open_bool = false;

  }

}

#include <Arduino.h>

#if defined(ESP32)

#include <WiFi.h>

#elif defined(ESP8266)

#include <ESP8266WiFi.h>

#endif

#include <Firebase_ESP_Client.h>

// Provide the token generation process info.

#include “addons/TokenHelper.h”

// Provide the RTDB payload printing info and other helper functions.

#include “addons/RTDBHelper.h”

// Insert your network credentials

#define WIFI_SSID “Workshop”

#define WIFI_PASSWORD “va412403”

//#define WIFI_SSID “VAIBT”

//#define WIFI_PASSWORD “VAIBT@123”

#define WIFI_SSID “Vigyan New Office”

#define WIFI_PASSWORD “rahul123456”

//#define WIFI_SSID “Galaxy balou”

//#define WIFI_PASSWORD “yolesang”

// Insert Firebase project API Key

#define API_KEY “AIzaSyDlTlJtGynzh7CQMMIyhIVQjoqOUA8t0m4”

// Insert Authorized Username and Corresponding Password

#define USER_EMAIL “vigyanashramfablab@gmail.com”

#define USER_PASSWORD “12345678”

-+922// I211/7*8nsert RTDB URLefine the RTDB URL

#define DATABASE_URL “gatecontrol-c8014-default-rtdb.firebaseio.com”

// Define Firebase objects

FirebaseData stream;

FirebaseAuth auth;

FirebaseConfig config;

// Variables to save database paths

String listenerPath = “Gate_State/”;

unsigned long sendDataPrevMillis = 0;

unsigned long timer = 0;

const long timer_reach = 2 * 60000;

bool start_timer = false;

bool open_bool = false;

bool close_bool = false;

unsigned long timer_closing = 0;

unsigned long timer_pump = 0;

bool pump_in_start = false;

bool pump_out_start = false;

// Declare outputs

const int LED = 2;

// Assign output variables to GPIO pins

const2222/ int water_pump_in = 16; //pin D7 //RX2 of ESP32

const int water_pump_out = 5; // pin D5

const int buzzer_indication = 2; // pin RX

// float sensor1

int FloatSensor1 = 21; //D4 pin of NodeMCU //D21 of ESP32

// float sensor2

int FloatSensor2 = 17; //D6 pin of NodeMCU //TX2 of ESP32

int Gate_State = 0;

int Gate_State_test = 0;

bool LED_Status = false;

void open_gate ();

void closed_gate ();

void pump_in();

void pump_out();

void timer_control();

// Initialize WiFi

void initWiFi() {

  Serial.print(“Connecting to WiFi ..”);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {

    Serial.print(‘.’);

    delay(*1000);

  }

  Serial.println(WiFi.localIP());

  Serial.println();

}

void setup() {

  Serial.begin(9600);

  initWiFi();

  // digitalWrite(LED, HIGH);

  // Initialize Outputs

  pinMode(LED, OUTPUT);

  delay(10);

  //motor define

  pinMode(water_pump_in, OUTPUT);

  pinMode(water_pump_out, OUTPUT);

  //wifi indication

  pinMode(buzzer_indication, OUTPUT);

  // motor initially low

  digitalWrite(water_pump_in, HIGH);

  digitalWrite(water_pump_out, HIGH);

  //float define

  pinMode(FloatSensor1, INPUT_PULLUP); //Arduino Internal Resistor 10K

  pinMode(FloatSensor2, INPUT_PULLUP); //Arduino Internal Resistor 10K

  // Assign the api key (required)

  config.api_key = API_KEY;

  // Assign the user sign in credentials

  auth.user.email = USER_EMAIL;

  auth.user.password = USER_PASSWORD;

  // Assign the RTDB URL (required)

  config.database_url = DATABASE_URL;

  Firebase.reconnectWiFi(true);

  // Assign the callback function for the long running token generation task */

  config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

  // Assign the maximum retry of token generation

  config.max_token_generation_retry = 5;

  // Initialize the library with the Firebase authen and config

  Firebase.begin(&config, &auth);

  digitalWrite (buzzer_indication, LOW);

  // digitalWrite(LED, LOW);

  delay(2000);

}

void loop() {

  if (pump_in_start == true){

    pump_in();

  }

  if (pump_out_start == true){

    pump_out();

  }

  if (Firebase.ready() && (millis() – sendDataPrevMillis > 1500 || sendDataPrevMillis == 0))            //Update firebase value every 1500ms

  {

  // if (LED_Status == false)

  // {

  //   LED_Status = true;

  // }else{

  //   LED_Status = false;

  // }

  // digitalWrite(LED, LED_Status);

    sendDataPrevMillis = millis();

    Serial.println( “Float 1”);

    Serial.println(digitalRead(FloatSensor1));

    Serial.println( “Float 2”);

    Serial.println(digitalRead(FloatSensor2));

    if (Firebase.RTDB.getBool(&stream, “/Gate_State/Open”)) {         //Get Gate State

      Gate_State_test = stream.boolData();

      Serial.println(Gate_State_test);

    }

    else {

      Serial.println(stream.errorReason());

    }

    Serial.print(“Gate State is :”);

    Serial.print(Gate_State);

    Serial.print(“nn”);

    // timer_control();        //Useless because time is disabled, should be in charge of closing the gate after a certain time — Uncomment if want to use timer

    if (close_bool == true) {   //Change Ack State

      Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

      pump_in_start = true;

    }

    if (open_bool == true) {    //Change Ack State

      Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

      pump_out_start = true;

    }

    if (Gate_State_test != Gate_State)    //Check if gate state has changed

    {

      Gate_State = Gate_State_test;

      if (Gate_State == 1) {    //Gate Should open

        open_gate();

        start_timer = true;   //Timer useless

        timer = millis() + timer_reach;

        Serial.print(“Timer Start”);

        open_bool = true;   //For ack of databse

      }

      if (Gate_State == 0) {    //Gate should close

        closed_gate();

        close_bool = true;    //For ack of database

      }

    }

  }

}

void timer_control() {        //Not called, because timer is disabled

  if (start_timer) {

    Serial.printf(“Set timer… %sn”, Firebase.RTDB.setInt(&stream, F(“/Gate_State/Timer”), (timer – millis())) ? “ok” : stream.errorReason().c_str());

  }

  if (start_timer == true && (millis() >= timer)) {

    Serial.print(“Gate is closing due to timer n”);

    start_timer = false;

    Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Open”), false) ? “ok” : stream.errorReason().c_str());          //Uncomment this line to activate timer !!

  }

  if (start_timer && Gate_State_test == 0) {

    Serial.printf(“Set timer… %sn”, Firebase.RTDB.setInt(&stream, F(“/Gate_State/Timer”), 0) ? “ok” : stream.errorReason().c_str());          //Uncomment this line to activate timer !!

    start_timer = false;

  }

}

void open_gate()    //when open gate

{

  Serial.println(digitalRead (FloatSensor1));

  int counter = 0;

  int Sensor1 = 0;

  while (counter <= 5)        //Float sensor must activated 5 times, to avoid noise

  {

    digitalWrite(water_pump_in, LOW);

    Serial.println( “Float 1”);

    Serial.println(Sensor1);

    Serial.println( “Float 2”);

    Serial.println(digitalRead(FloatSensor2));

    Sensor1 = digitalRead(FloatSensor1);

    if (Sensor1 == 1) {     //Increase counter each time float is activated

      ++counter;

      Serial.print(“Counter = “);

      Serial.println(counter);

    }

  }

  digitalWrite (buzzer_indication, HIGH);   //Activate buzzer to tell gate is opening

  delay (2000);

  digitalWrite(water_pump_in, HIGH);

  delay (4000);

  digitalWrite (buzzer_indication, LOW);

  Serial.println(digitalRead (FloatSensor1));

  delay(100);

  Serial.printf(“Set bool… %sn”, Firebase.RTDB.setBool(&stream, F(“/Gate_State/Ack”), Gate_State) ? “ok” : stream.errorReason().c_str());

  Serial.println(“Gate Open”);

  digitalWrite(water_pump_out, LOW);      //Start pumping out into to pre-empty

  Serial.println( “Pump out ON”);

  Serial.println(timer_pump);

  timer_pump = millis();

}

void closed_gate ()

{

  Serial.println(digitalRead (FloatSensor2));

  int counter = 0;

  int Sensor2 = 1;

  while (counter <= 25) //Float sensor must activated 25 times, to avoid noise : 25 because movement in water can activated it

  {

    Sensor2 =  (digitalRead(FloatSensor2));

    digitalWrite(water_pump_out, LOW);

    Serial.println( “Float 1”);

    Serial.println(digitalRead(FloatSensor1));

    Serial.println( “Float 2”);

    Serial.println(Sensor2);

    if (Sensor2 == 0) {     //Increase counter each time float is activated

      ++counter;

      Serial.print(“Counter = “);

      Serial.println(counter);

    }

  }

  digitalWrite (buzzer_indication, HIGH);   //Activate buzzer to tell gate is closing

  delay (10000);

  digitalWrite (buzzer_indication, LOW);

  delay (25000);

  digitalWrite(water_pump_out, HIGH);

  Serial.println(digitalRead (FloatSensor2));

  digitalWrite(water_pump_in, LOW);       //Start pumping back into to prefill

  Serial.println( “Pump in ON”);

  Serial.println(timer_pump);

  timer_pump = millis();

}

void pump_in()        //Pre-fill tank before next opening

{

  // water pump in ON

  Serial.println( “Float 1”);

  Serial.println(digitalRead(FloatSensor1));

  Serial.println( “Float 2”);

  Serial.println(digitalRead(FloatSensor2));

  Serial.print(“Timer is =”);

  Serial.println(millis() – timer_pump);

  if ((millis() – timer_pump) >=  (70*1000)) {

    digitalWrite(water_pump_in, HIGH);

    Serial.println( “Pump in OFF”);

    timer_pump = 0;

    pump_in_start = false;

    delay(10);

    close_bool = false;

  }

}

void pump_out()        //Pre-empty tank before next closing

{

  // water pump out ON

  Serial.println( “Float 1”);

  Serial.println(digitalRead(FloatSensor1));

  Serial.println( “Float 2”);

  Serial.println(digitalRead(FloatSensor2));

  Serial.print(“Timer is =”);

  Serial.println(millis() – timer_pump);

  if ((millis() – timer_pump) >=  (80*1000)) {

    digitalWrite(water_pump_out, HIGH);

    Serial.println( “Pump out OFF”);

    timer_pump = 0;

    pump_out_start = false;

    delay(10);

    open_bool = false;

  }

}

Problem

gate not open and close until user not give the some manual force .

because of water speed on and off of gate is very time consuming process.

multiple user can not access the gate remote

some mechanical problem

Problem solved

Mechanical problem solved

All electronic component is troubleshot

Software and Program is updated

working of system

one to check the open gate water level & another one to check the closed gate water level.

when the motor start to fill the water to the attached gate tank. if the water touches the float switch he sends a signal 1 to the ESP32 & stopped a motor. Then the gate is opened.

when the motor start to fill out water to the gate tank . Then if the water below the float switch he sends a signal 0 to the Arduino & stopped the motor. Then the gate is closed.

System Flow Chart