[SOLVED] How to convert to Blynk: Blink example

Hi, I’m very new with Blynk, and realized that converting the regular code to Blynk isn’t as straightforward as I thought because of my limited knowledge of how the virtual pins work.

I’m referring to the example here: https://www.arduino.cc/en/tutorial/blink

How would someone go about achieving the same effect using a Blynk button to launch the program. For example, press button in Blynk and built-in LED starts blinking.

Hardware: Uno + CC3000

What is the best way to learn Blynk? I’ve been pulling my hair for the last two days to convert existing sketches to Blynk. I’ve tried reverse engineering the example browser codes but failed miserably.

By working through the basic examples and using the easiest hardware (ESP standalone with USB port).

The PUSH DATA example is a good starter. It demonstrates:

  1. For beginners, loop() has just 2 lines.
  2. Use a timer to call functions.
1 Like

The Help Center will help bring you up to speed.
http://help.blynk.cc/blynk-basics/what-is-virtual-pins

Just keep in mind that virtual pins is just a function you can use to send and receive data from the hardware → app.

1 Like

I’m trying to port this code to Blynk where the goal is to run the stepper motor when Button(V1) is pressed:

    #include <AccelStepper.h>
    #define HALFSTEP 8

    // Motor pin definitions
    #define motorPin1  D5     // IN1 on the ULN2003 driver 1
    #define motorPin2  D6     // IN2 on the ULN2003 driver 1
    #define motorPin3  D7     // IN3 on the ULN2003 driver 1
    #define motorPin4  D8     // IN4 on the ULN2003 driver 1

    // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
    AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

    void setup() {
      stepper1.setMaxSpeed(1000.0);
      stepper1.setAcceleration(100.0);
      stepper1.setSpeed(200);
      stepper1.moveTo(20000);

    }//--(end setup )---

    void loop() {

      //Change direction when the stepper reaches the target position
      if (stepper1.distanceToGo() == 0) {
        stepper1.moveTo(-stepper1.currentPosition());
      }
      stepper1.run();
    }

Here’s my Blynk code:

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <AccelStepper.h>
    #define HALFSTEP 8

    // Motor pin definitions
    #define motorPin1  D5     // IN1 on the ULN2003 driver 1
    #define motorPin2  D6     // IN2 on the ULN2003 driver 1
    #define motorPin3  D7     // IN3 on the ULN2003 driver 1
    #define motorPin4  D8     // IN4 on the ULN2003 driver 1

    // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
    AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "********";

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "********";
    char pass[] = "********";

    void setup() {
      // Debug console
      Serial.begin(9600);

      Blynk.begin(auth, ssid, pass);
      // You can also specify server:
      //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
      //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

      stepper1.setMaxSpeed(1000.0);
      stepper1.setAcceleration(100.0);
      stepper1.setSpeed(200);
      stepper1.moveTo(-999999);
    }

    void loop() {
      Blynk.run();
    }

    BLYNK_WRITE(1)
    {
      stepper1.run();
    }

So far, when the button is pressed on or off, the motor is moving one step at a time. So, for 200,000 steps, I’d be pressing to 200,000 times. What’s the right way to do this instead of having to manually press the button 200,000 times?

EDIT:

After some more investigation, BLYNK_WRITE(1) is invoked every time the blynk button is pressed ON or OFF. This calls the “stepper1.runSpeed()” which returns a boolean vale “true”. This causes the motor to go 1 step. Still not sure how I can keep it moving continuously when the blynk button is pressed.

Porting complex code into Blynk format and structure without having a good grasp of said format and structure is akin to getting your learner’s license and applying for the Grand-Prix :smiley:

A stepper motor can require as much coded precision to control as it offers in motion… but to do so with Blynk (or any other MCU environment where specific timing management is required) requires non-blocking counter and timer routines, etc. There is no ONE way to do it.

Try searching this site for the word “stepper motor” as I believe there were a few other topics about it that might have some pointers, but I believe the type of stepper controller and library you use will make all the difference as some take all the nitpicky out of the motion commands.

But honestly, start with the Sketch Builder and get familiar with some of the basics

1 Like

Thinking a bit about your stepper question… A dirt simple way to just run the stepper command once in a loop that would run only when the button is pressed would consist of three things:

1) A BLYNK_WRITE() function that looks to see if the button is pressed or not and sets a flag variable 0 or 1 accordingly:
http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynk_writevpin

BLYNK_WRITE(Vx) {
  int StepSwitch = param.asInt();
}

2) A BlynkTimer that runs every so often, say 250ms (1/4 second), that runs a function - in this case MoveStepper() :
http://docs.blynk.cc/#blynk-firmware-blynktimer

BlynkTimer Steptimer;
Steptimer.setInterval(250L, MoveStepper);  // run the ```void MoveStepper()``` every 1/4 second
void loop() {
  Blynk.run();
  Steptimer.run();
}

3) A function that runs your stepper command to step once, if the button flag is 1. With this timing, if the button is pressed, the stepper will move 4 steps a second:

void MoveStepper()  {
  if (StepSwitch == 1)  {
    stepper1.run();
    }
}

None of this has been compiled at my end, so check the syntax :wink:

1 Like

I’m getting this error when compiling:

'StepTimer' does not name a type

Full code:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AccelStepper.h>
#define HALFSTEP 8
#define INT_MAX 999999

// Motor pin definitions
#define motorPin1  D5     // IN1 on the ULN2003 driver 1
#define motorPin2  D6     // IN2 on the ULN2003 driver 1
#define motorPin3  D7     // IN3 on the ULN2003 driver 1
#define motorPin4  D8     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

int StepSwitch = 0; //Setting StepSwitch as global to be used in BLYNK_WRITE(V1) and MoveStepper()

BlynkTimer StepTimer;
StepTimer.setInterval(250L, MoveStepper);  // run the ```void MoveStepper()``` every 1/4 second

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "00000000";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "00000000";
char pass[] = "00000000";

void setup() {
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(-INT_MAX);
}

void loop() {
  Blynk.run();
  StepTimer.run();
}

BLYNK_WRITE(V1)
{
  StepSwitch = param.asInt();
}

void MoveStepper()  {
  if (StepSwitch == 1)  {
    stepper1.run();
  }
}
BlynkTimer timer;
int StepTimer = timer.setInterval(250L, MoveStepper);  // run the ```void MoveStepper()``` every 1/4 second


void loop(){
Blynk.run()
timer.run();
}

The naming is a bit weird if you encounter it. The StepTimer in your case is a particular instance of the timer object, which in turn is an instance of the BlynkTImer.

1 Like

Ya, I didn’t write this in an IDE and compile for syntax… just hammered it out here and I couldn’t think on the fly of how to otherwise name the timer (not that it is always necessary), but your method is neater and will better accommodate multiple timers, thanks :+1:

2 Likes

We all learn here, which is a good thing :slight_smile:

1 Like

Will calling MoveStepper every millisecond overtax Blynk? What things do the 100ms limit apply to? I’m always afraid of overtaxing the blynk server.

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <AccelStepper.h>
    #define HALFSTEP 8
    #define INT_MAX 999999

    // Motor pin definitions
    #define motorPin1  D5     // IN1 on the ULN2003 driver 1
    #define motorPin2  D6     // IN2 on the ULN2003 driver 1
    #define motorPin3  D7     // IN3 on the ULN2003 driver 1
    #define motorPin4  D8     // IN4 on the ULN2003 driver 1

    // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
    AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

    int StepSwitch = 0; //Setting StepSwitch as global to be used in BLYNK_WRITE(V1) and MoveStepper()

    void MoveStepper();
    BlynkTimer timer;
    int StepTimer = timer.setInterval(1L, MoveStepper);  // run the void MoveStepper()

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "xxxxxxxx";

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "xxxxxxxx";
    char pass[] = "xxxxxxxx";

    void setup() {
      // Debug console
      Serial.begin(9600);

      Blynk.begin(auth, ssid, pass);
      // You can also specify server:
      //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
      //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

      stepper1.setMaxSpeed(1000.0);
      stepper1.setAcceleration(100.0);
      stepper1.setSpeed(200);
      stepper1.moveTo(-INT_MAX);
    }

    void loop() {
      Blynk.run();
      timer.run();
    }

    BLYNK_WRITE(V1)
    {
      StepSwitch = param.asInt();
    }

    void MoveStepper()  {
      if (StepSwitch == 1)  {
        stepper1.run();
      }
    }

One thousand times a second?.. yes, it probably will :wink:

Also unlikely that you even need as low as 100ms, let alone 1ms… but try different settings and see the results for yourself.

I also had to tweak your post to display the code properly with this method:

1 Like

Because of the way the accelstepper library is, it would need 1000 “true” per second to make the motor go at moderate speed. It sucks for the Blynk server and I won’t be using it for that reason. I’ll try with 100L. Maybe that speed will be acceptable for some projects like opening the window drapes. Otherwise, I’ll just shelf this project for now. Thank you all for your help. I learned a lot from this thread.

You can also try options like switching to a better motor controller (that can run set and forget control), or even a dedicated MCU for the Stepper, fed control data from a Blynk MCU via I2C or serial.

1 Like

Huh, seems someone did something similar already using accelstepper and Blynk slider. I only had to make minor changes to the code based on what I learned from this thread.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AccelStepper.h>
#define FULLSTEP 4
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  D5     // IN1 on the ULN2003 driver 1
#define motorPin2  D6     // IN2 on the ULN2003 driver 1
#define motorPin3  D7     // IN3 on the ULN2003 driver 1
#define motorPin4  D8     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxx";

int maximo;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
  delay(10);
  stepper.setMaxSpeed(1000.0);
  delay(10);
  stepper.setSpeed(200);
  delay(10);
  while (Blynk.connect() == false) {
  }
}

BLYNK_WRITE(V0) // Max slider value is 400 for FULLSTEP and 800 for HALFSTEP. If value is too high, the motor will seize.
{
  int vel = param.asInt();
  maximo = vel;
  stepper.setSpeed(maximo);
}

void loop()
{
  Blynk.run();
  stepper.runSpeed();
}

This is great since 28BYJ-48 Stepper Motor with ULN2003 seems to be one of the most common combos of the Arduino world. I’ll do a short write-up later, but this post should show up in search if someone searches for this combo and Blynk.