Transfering of physical switch to blynk

I AM USING BLYNK IN MY PROJECT. THE PROJECT IS PLANT WATERING SYSTEM. I AM USING STEPPER MOTOR AND PUMP MOTOR IN PROJECT. I HAD A PROBLEM WITH STOPING OF STEPPER MOTOR. I USED PHYSICAL SWITCH TO STOP THE MOTOR SO WHEN I PRESS IT THE CIRCUIT STOP , BUT I WANT TO CONTROL THIS FROM MY PHONE I ALREADY CAN START THE CIRCUIT GIVING COMMAND FROM THE BLYNK APP SO I WANT TO STOP IT AS WELL HOW CAN I TRANSFER PHYSICAL SWITCH TO BLYNK SO WHEN I PRESS IT ON THE SCREEN OF MY SMARTPHONE IT WILL STOP THE CIRCIUT
HERE THE CODE I AM USING

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

WidgetLED standbyLed(V4);
WidgetLED workingLed(V5);

//Stepper motor pins
#define step1 14 //D5
#define step2 12 //D6
#define step3 13 //D7
#define step4 15 //D8
//Pump pin
#define pump 5 //D9
//Limit switch
#define limitSwitch 4 //D10

int steps = 0 ;
int direction = 0 ;
int totalStaps = 0 ;
int stepsCount = 0 ;

//Data from Blynk App
int onOffTimer = 0 ;
int test = 0 ;
int timerState1 = 0 ;
int timerState2 = 0 ;
int pumpPower = 0 ;
int distance = 0 ;
int instantWatering = 0 ;
int limitSwitchState = 0 ;


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  //Stepper motor
  pinMode(step1, OUTPUT);
  pinMode(step2, OUTPUT);
  pinMode(step3, OUTPUT);
  pinMode(step4, OUTPUT);
  //Pump
  pinMode(pump, OUTPUT);
  //Limit switch
  pinMode(limitSwitch, INPUT);
  stopMotor();
}

void loop()
{
  Blynk.run();
  stepCalculate(distance) ;
  if (onOffTimer == 1){ //Turn on timer
    if (timerState1 == 1 || timerState2 == 1){ //Time to watering
      standbyLed.off();
      workingLed.on();
      analogWrite(pump, pumpPower);
      goWork(totalStaps);
      analogWrite(pump, 0);
      backHome();
      standbyLed.on();
      workingLed.off();

    }
  }
  if (instantWatering == 1){ //Press instantwatering button
    standbyLed.off();
    workingLed.on();
    analogWrite(pump, pumpPower);
    goWork(totalStaps);
    analogWrite(pump, 0);
    backHome();
    standbyLed.on();
    workingLed.off();
  }
  if (test == 1){ // Press test button
    standbyLed.off();
    workingLed.on();
    goWork(totalStaps);
    backHome();
    standbyLed.on();
    workingLed.off();
  }
}

BLYNK_WRITE(V0) {
  onOffTimer = param.asInt();
}
BLYNK_WRITE(V1) {
  test = param.asInt();
}
BLYNK_WRITE(V2) {
  timerState1 = param.asInt();
}
BLYNK_WRITE(V3) {
  timerState2 = param.asInt();
}
BLYNK_WRITE(V6) {
  pumpPower = param.asInt();
}
BLYNK_WRITE(V7) {
  distance = param.asInt();
}
BLYNK_WRITE(V8) {
  instantWatering = param.asInt();
}

void stepCalculate(int distance){
  totalStaps = (distance*(4076/4)) ;
}

void backHome(){
  limitSwitchState = digitalRead(limitSwitch);
  while(limitSwitchState == 0){
    stepper(0) ;
    delay(1);
    limitSwitchState = digitalRead(limitSwitch);
  }
  stopMotor() ;
  limitSwitchState = digitalRead(limitSwitch);
}

void goWork(int totalStaps){
  while(stepsCount < totalStaps){
    stepper(1);
    delay(1);
    stepsCount ++ ;
  }
  stopMotor() ;
  stepsCount = 0 ;
}

void stepper(int direction){
  for (int x=0; x<1; x++){
    Blynk.run();
    switch(steps){
      case 0:
      digitalWrite(step1, LOW);
      digitalWrite(step2, LOW);
      digitalWrite(step3, LOW);
      digitalWrite(step4, HIGH);
      break;
      case 1:
      digitalWrite(step1, LOW);
      digitalWrite(step2, LOW);
      digitalWrite(step3, HIGH);
      digitalWrite(step4, HIGH);
      break;
      case 2:
      digitalWrite(step1, LOW);
      digitalWrite(step2, LOW);
      digitalWrite(step3, HIGH);
      digitalWrite(step4, LOW);
      break;
      case 3:
      digitalWrite(step1, LOW);
      digitalWrite(step2, HIGH);
      digitalWrite(step3, HIGH);
      digitalWrite(step4, LOW);
      break;
      case 4:
      digitalWrite(step1, LOW);
      digitalWrite(step2, HIGH);
      digitalWrite(step3, LOW);
      digitalWrite(step4, LOW);
      break;
      case 5:
      digitalWrite(step1, HIGH);
      digitalWrite(step2, HIGH);
      digitalWrite(step3, LOW);
      digitalWrite(step4, LOW);
      break;
      case 6:
      digitalWrite(step1, HIGH);
      digitalWrite(step2, LOW);
      digitalWrite(step3, LOW);
      digitalWrite(step4, LOW);
      break;
      case 7:
      digitalWrite(step1, HIGH);
      digitalWrite(step2, LOW);
      digitalWrite(step3, LOW);
      digitalWrite(step4, HIGH);
      break;
      default:
      digitalWrite(step1, LOW);
      digitalWrite(step2, LOW);
      digitalWrite(step3, LOW);
      digitalWrite(step4, LOW);
      break;
    }
    if(direction == 0){
      steps++ ;
    }
    if(direction == 1){
      steps-- ;
    }
    if(steps > 7){
      steps = 0 ;
    }
    if(steps < 0){
      steps = 7 ;
    }
  }
}

void stopMotor(){
  digitalWrite(step1, LOW);
  digitalWrite(step2, LOW);
  digitalWrite(step3, LOW);
  digitalWrite(step4, LOW);
}

HOW DOES CAPS LOCK HELP OTHERS TO READ BETTER?
:smirk_cat:

hello and welcome to blynk forum!

you can try to stop the motor with adding a button widget, and a virtual pin.

in hardware, try something like this:

BLYNK_WRITE(V8) { // replace v8 with your virtual pin, of course
if (!param.asInt()) stopMotor();
}

i’m not sure how instant will this “stop” be, because of the for cycle for stepper motor… but i see you have a blynk.run() in the for cycle, so it could work. try and share the results here. :wink:

2 Likes

Thanks alot for cooperation can u explain a little bit detailed because i a bigginer its my first blynk project

There are a plenty of topics on this community about using virtual pins. There is also Docs section on Blynk site, there you will find detailed explanation for various cases. Just use search option without any fear :slight_smile:

what, exactely?

1 Like


We have added this to the code but no result we couldn stop usıng the virtual button in the app
We prees the v9 button but nothıng happen unles we stop the system bu the physücal switch that is what we dont want to do

ok. please do not post screenshots, post formatted code instead.

also, try to set the button widget to switch, and put a flag in the for cycle, to exit from the for cycle if the button is pressed.

you have to experiemet a little bit, i can’t write and test the code for you.

1 Like