BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Automatic water pump - Help


#1

Hi all blynk-er.
I’m a newbie.
I’m having trouble with source code. Can I get advice from you?
I came to Blynk in 3 days. I just want to say that this is a great thing. It is a pity to know Blynk too late.

  • I am building an automatic water pump system. I want to add a flow switch, when the water system is out of pause and the error light is on. When the water level drops below 30, relay works. NodeMCU will wait for 20 seconds. If the flow switch is activated (NO), relay will remain active. And if the flow switch is open (NC) , the system will report an error and send a notification via the Blynk application.
  • When the well is out of water, the water switch is open, the system is in standby mode, only kích hoạt khi bấm nút trên Blynk application. The click button resets the state to its original state.
  • It’s too complicated for a newbie.
  • Hope this theme will inspire others.
    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    //#include <ArduinoOTA.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <BlynkSimpleEsp8266.h>
    #include <ESP8266WiFi.h>
    #include <SimpleTimer.h>
    //#include "DHT.h"
    #include "Settings.h"
    /* Blynk */
    WidgetLCD lcd(V0);
    WidgetLCD status(V1);
    SimpleTimer timer;

    //#define dhtPin 12  
    //#define DHTTYPE DHT11 
    #define echoPin 13 // Echo Pin
    #define trigPin 15 // Trigger Pin
    #define pumpPin 5 // Relay
    #define pumpButton 4
    long duration, distance; // Duration used to calculate distance
    void setup() {
    // PINMODES
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(pumpPin, OUTPUT);
    pinMode(pumpButton, INPUT_PULLUP);
    // COMMUNICATIONS
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    // CONNECT TO BLYNK
    #ifdef LOCAL_SERVER
    Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, LOCAL_SERVER);
    #else
    Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
    #endif
    while (Blynk.connect() == false) {}
    Blynk.syncVirtual(vPIN_LIMITMENU, vPIN_WATER_COST, vPIN_WATER_TOTAL_MONTH, vPIN_RESSET);
    // OVER THE AIR UPDATES
    //ArduinoOTA.setHostname(OTA_HOSTNAME);
    //ArduinoOTA.begin();

     timer.setInterval(1000, MeasureCm);
     }

     BLYNK_READ(V8)
     {
     Blynk.virtualWrite(8,distance);// virtualpin 8 
     }
      BLYNK_READ(V7)
     {
     Blynk.virtualWrite(7,duration);// virtualpin 8 
     }

    /****************************************************************
    *     Calculation of ultrasonic sensor and relay control        :
    ****************************************************************/

      
    void MeasureCm()
    {

     digitalWrite(trigPin, LOW); 
     delayMicroseconds(2); 

     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10); 

     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);

     //Calculate the distance (in cm) based on the speed of sound.
     distance = duration/58.2;

     Serial.print("Distance :" + distance);
     Serial.println(distance);
     lcd.clear();


    {
      // thiet lap muc nuoc su dung xuong 30 cm thi tu dong bat relay
      if (distance >= 30 ){
       Blynk.notify("GioLangLe-IoT : He thong bom nuoc da duoc khoi dong!"); 
       digitalWrite (pumpPin, LOW);
       Serial.print("Relay On");
      }
      // thiet lap muc nuoc len den 10 cm thi tu dong tat relay
       if (distance < 10 ){
      Blynk.notify("GioLangLe-IoT : Nuoc da day, he thong tro ve trang thai cho..."); 
      digitalWrite (pumpPin, HIGH);
      Serial.print("Relay Off ");

      }
     }
    }

    void loop() {
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer
    }

And Settings.h

#define AUTH                      "xxxx"
#define WIFI_SSID               "xxxx"
#define WIFI_PASS               "xxxx"
//#define OTA_HOSTNAME              "GIOLANGLE-IOT"
//#define LOCAL_SERVER              IPAddress(192, 168, 1, 22)

#2

I am trying… i using base ArduFarmBot2
The compilation happens without error … But it still does not work. Functions do not work.

Main

     #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
     //#include <ArduinoOTA.h>
     #include <Wire.h>
     #include <SPI.h>
     #include <BlynkSimpleEsp8266.h>
     #include <ESP8266WiFi.h>  
     #include "Defines.h"
     #include "Settings.h"

     /* Blynk */
     WidgetLCD lcd(V0);
     WidgetLCD status(V1);
     WidgetLED PUMPs(V0);  // Echo signal to Sensors Tab at Blynk App
     WidgetLED PUMPa(V1); // Echo signal to Actuators Tab at Blynk App
     WidgetLED LAMPs(V1);  // Echo signal to Sensors Tab at Blynk App
     WidgetLED LAMPa(V6); // Echo signal to Actuators Tab at Blynk App
     /* DHT11 */
     #include "DHT.h"
     DHT dht(DHTPIN, DHTTYPE);
     /* TIMER */
     #include <SimpleTimer.h>
     SimpleTimer timer;
     /* UltraSonic */
     int maximumRange = 150;
     // int distance;
     long duration, distance; // Duration used to calculate distance
     
    void setup() {
   // PINMODES
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(pumpPin, OUTPUT);
   pinMode(pumpButton, INPUT_PULLUP);
   //pinMode(lampPin, INPUT_PULLUP);

   // COMMUNICATIONS
   Serial.begin(115200);
   WiFi.mode(WIFI_STA);
   // CONNECT TO BLYNK
   #ifdef LOCAL_SERVER
   Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, LOCAL_SERVER);
   #else
   Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
   #endif
   while (Blynk.connect() == false) {}
   Blynk.syncVirtual(vPIN_LIMITMENU, vPIN_WATER_COST, vPIN_WATER_TOTAL_MONTH, vPIN_RESSET);
   // OVER THE AIR UPDATES
   //ArduinoOTA.setHostname(OTA_HOSTNAME);
   //ArduinoOTA.begin();
    dht.begin();  


   PUMPs.off();
   PUMPa.off();
   LAMPa.off();
   digitalWrite(pumpPin, HIGH); // To be used with Relay module (inverted logic: normally HIGH)
   digitalWrite(lampPin, HIGH); // To be used with Relay module (inverted logic: normally HIGH)

 }

     void loop() {
       Blynk.run(); // Initiates Blynk
       timer.run(); // Initiates SimpleTimer
     }
 /****************************************************************
 *     Calculation of ultrasonic sensor and relay control        :
 ****************************************************************/


 /****************************************************************
 * Read remote commands 
 ****************************************************************/
 BLYNK_WRITE(3) // Pump remote control
 {
   int i=param.asInt();
   if (i==1) 
   {
     pumpStatus = !pumpStatus;
     aplyCmd();
   }
 }

 BLYNK_WRITE(4) // Lamp remote control

 {
   int i=param.asInt();
   if (i==1) 
   {
     lampStatus = !lampStatus;
     aplyCmd();
   }
 }

 /****************************************************************
 * Read local commands Flow swich (NO)and Lamp buttons are normally "HIGH":
 ****************************************************************/
 void readLocalCmd() 
 {  
   boolean digiValue = debounce(pumpButton);
   if (!digiValue) 
   {
     pumpStatus = !pumpStatus;
     aplyCmd();
   }
 }

 /***************************************************
 * Receive Commands and act on RELAYs - Logic LOW
 ****************************************************/
 void aplyCmd()
 {
   if (pumpStatus == 1) 
   {
     Blynk.notify("Pump System: Warning ==>> Pump ON"); 
     digitalWrite(pumpPin, LOW); // To be used with Relay module (inverted logic: activate with LOW)
     PUMPs.on();
     PUMPa.on();
   }
   else
       {
         digitalWrite(pumpPin, HIGH); // To be used with Relay module (inverted logic: normally HIGH)
         PUMPs.off();
         PUMPa.off();
       }
   
   if (lampStatus == 1) 
   {
     Blynk.notify("Factory : Warning ==>> Lamp ON");
     digitalWrite(lampPin, LOW); // To be used with Relay module (inverted logic: activate with LOW)
     LAMPs.on();
     LAMPa.on();
   }
   else
       {
         digitalWrite(lampPin, HIGH); // To be used with Relay module (inverted logic: normally HIGH)
         LAMPs.off();
         LAMPa.off();
       }
       // thiet lap muc nuoc su dung xuong 30 cm thi tu dong bat relay
      if (distance >= 30 ){
    digitalWrite (pumpPin, LOW);
    Serial.print("Relay On");
   status.clear();
   status.print(0,0,"Motor Online");
   }
   // thiet lap muc nuoc len den 10 cm thi tu dong tat relay
    if (distance < 10 ){
   digitalWrite (pumpPin, HIGH);
   Serial.print("Relay Off ");
   status.clear();
   status.print(0,0,"Motor Offline");
   } 
 }


 /***************************************************
 * Turn Pump On for a certain amount of time
 ****************************************************/
 void turnPumpOn()
 {
   pumpStatus = 1;
   aplyCmd();
   delay (TIME_PUMP_ON*1000);
   pumpStatus = 0;
   aplyCmd();
 }

 /***************************************************
 * Turn Lamp On for a certain amount of time 
 ****************************************************/
 void turnLampOn()
 {
   lampStatus = 1;
   aplyCmd();
   delay (TIME_LAMP_ON*1000);
   lampStatus = 0;
   aplyCmd();
 }

 /***************************************************
  * Send data to Blynk
  **************************************************/
 void sendUptime()
 {
   Blynk.virtualWrite(10, airTemp); //virtual pin V10
   Blynk.virtualWrite(11, airHum); // virtual pin V11
 }

`**Defines.h**`



    /* Automatic Control Parameters Definition */
    #define HOT_TEMP      22
    #define TIME_PUMP_ON  15
    #define TIME_LAMP_ON  15

    /* TIMER */
    #define READ_BUTTONS_TM   1L  // definitions in seconds
    #define READ_AIR_DATA_TM  2L
    #define SEND_UP_DATA_TM   10L
     

    /* DHT11*/
    #define DHTPIN 4 
    #define DHTTYPE DHT11 
    float airHum = 0;
    float airTemp = 0;

    /* Ultrasonic */
    #define echoPin 13 // Echo Pin
    #define trigPin 15 // Trigger Pin

    /* Relays */
    #define pumpPin 5               // Relay
    #define lampPin 16              // Lamp Pin
    boolean pumpStatus = 0;
    boolean lampStatus = 0;


    /* Buttons */
    #define pumpButton 2            // Flow Swich

    /* VPIN */
    #define vPIN_WATER_TOTAL_MONTH    V21
    #define vPIN_WATER_COST           V22
    #define vPIN_LIMITMENU            V23
    #define vPIN_RESSET               V24



Functions.ino


    /***************************************************
    * Wait Button to be pressed a defined time in seconds
    ****************************************************/
    void waitButtonPress (int waitTime)
    {
      long startTiming = millis();
      while (debounce(pumpButton)) 
      { 
        if ((millis()-startTiming) > (waitTime*1000)) break;
      }
    }

    /***************************************************
    * Starting Timers
    ****************************************************/
    void startTimers(void)
    {
      timer.setInterval(READ_BUTTONS_TM*1000, readLocalCmd);
      timer.setInterval(READ_AIR_DATA_TM*1000, getDhtData);
      timer.setInterval(SEND_UP_DATA_TM*1000, sendUptime);
    }

    /***************************************************
    * Debouncing a key
    ****************************************************/
    boolean debounce(int pin)
    {
      boolean state;
      boolean previousState;
      const int debounceDelay = 30;
      
      previousState = digitalRead(pin);
      for(int counter=0; counter< debounceDelay; counter++)
      {
        delay(1);
        state = digitalRead(pin);
        if(state != previousState)
        {
          counter = 0;
          previousState = state;
        } 
      }
      return state;
    }

Sensor.ino

    /***************************************************
     * Get DHT data
     **************************************************/
    void getDhtData(void)
    {
      float tempIni = airTemp;
      float humIni = airHum;
      airTemp = dht.readTemperature();
      airHum = dht.readHumidity();
      if (isnan(airHum) || isnan(airTemp))   // Check if any reads failed and exit early (to try again).
      {
        Serial.println("Failed to read from DHT sensor!");
        airTemp = tempIni;
        airHum = humIni;
        return;
      }
    }

    /***************************************************
     * Get Ultrasonic Sensor data
     **************************************************/
     void MeasureCm()
    {

     digitalWrite(trigPin, LOW); 
     delayMicroseconds(2); 

     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10); 

     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);

     //Calculate the distance (in cm) based on the speed of sound.
     distance = duration/58.2;
    }

#3

I had to edit your post to correctly format your code for proper forum viewing, as documented in the Welcome Topic.