Arduino UNO, ESP8266 and BLYNK internet connection not responsive + button not working

I am trying to power on a Motor Driver which will enable a water pump to turn on using a simple application that I created using Blynk. However, the internet connection just stays up for something like 5 seconds, and then it disconnects. It does this continuously, hence the whole system is just not functional. I have tried testing the button during the short period of time that it connects to the internet and it is also not working. The button is the one that I want to use to switch on or off the motor driver.

Here is my code:

    #define BLYNK_PRINT Serial
    #include <SoftwareSerial.h>
    #include <ESP8266_Lib.h>
    #include <BlynkSimpleShieldEsp8266.h>

    


    int sensor_raw_data;
    int mapped_data;
    int pump;

    SoftwareSerial esdata(3,4);
    #define auth "XXXXXXX"
    #define ssid "XXXXXXX"
    #define pass "XXXXXXX"

    

    ESP8266 wifi(&esp8266);

     // Send AT commands
     String sendAT(String cmd, const int tmeout) {

       String rsp = "";

       esdata.print(cmd);

       long int time = millis();

      while((time + tmeout) > millis()) {

        while(esdata.available()) {

        char c = esdata.read();

        rsp += c;

        }
      }
      Serial.print(rsp);
      return rsp;
    }

void setup() {

      Serial.begin(9600);

      esdata.begin(9600);

      Blynk.begin(auth, wifi, ssid, pass);

     
      sendAT("AT+RST\r\n", 2000);

      sendAT("AT\r\n", 1000);

      sendAT("AT+CWMODE=1\r\n", 1000);

      sendAT("AT+CWJAP=\""said"\",\""pass"\"\r\n", 2000);

      while(!esdata.find("OK")) {
    
      }

      sendAT("AT+CIFSR\r\n", 1000);

      sendAT("AT+CIPMUX=0\r\n", 1000);

      pinMode(A0, INPUT);

      pinMode(8, OUTPUT);
  
 
     }

void loop() {

      Blynk.run();

      sensor_raw_data = analogRead(A0);

      mapped_data=map(sensor_raw_data, 0, 1023, 100, 0);

      String sensor_value = String(mapped_data);

      Serial.print("Sensor Data: ");

      Serial.println(mapped_data);

      // Display the soil moisture on a gauge. // This is working   properly
     Blynk.virtualWrite(V5, prdata); 

      // Send notification if soil moisture is below 80%
      if(mapped_data < 80) {

      Blynk.notify("Soil Moisture Is Below 80%. Turn On Water Pump.");

      } else {

        Blynk.notify("Soil Moisture Is Above 80%. Turn Off Water Pump.");

       }

      String pmp = String(pump);
      updateTS(sensor_value, pmp);
      delay(2000);

     }

This is the function for the button. In my app, the button is Virtual pin 8. The Motor Driver is connected to Digital pin 8 on the Arduino. Is there something wrong with the function? Or maybe there is actually another way to let the button switch on or off the pump.

BLYNK_WRITE(V8) {

         int pinValue = param.asInt();

          if(pinValue == 1) {

            digitalWrite(8, HIGH);
            
            // This is the value that will be displayed for the pump status in thingspeak API graph.
            pump = 100;

          } else {

            digitalWrite(8, LOW);

            pump = 0;
          }
        }

First thing you need to do is keep your void() loop clean - the delays and other functions will kill your Blynk connection.

cul
billd

1 Like