Blynk for Beginners and help with your project

To get the most out of Blynk you really need some C coding experience, even if it’s only at a very basic level. The easiest way to use Blynk is with the Arduino IDE and the variant of C therein. There are lots of online tutorials but you can learn a lot just by studying other people projects if you know the basics.

Study example sketches
Blynk provides basic examples for all their widgets. Make sure you go through them and understand how they work. Though, many of the sketches are written for Arduino’s with Ethernet shields and this is very outdated when you consider the power and nominal cost of ESP’s.

Limit your void loop()
Blynk sketches have some fundamental differences compared with regular Arduino sketches and this is because you are connected via a network to a server. Suffice to say, as beginners, we recommend you limit your loop() to just 2 lines as shown below:

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

Don’t use delay(). Use timers instead.
You should look to structure your sketch so everything follows a timed structure which is covered, in part, by timer.run() in the loop(). The basic example provided by Blynk to demonstrate SimpleTimer is PushData.ino at https://github.com/blynkkk/blynk-library/blob/master/examples/GettingStarted/PushData/PushData.ino

Blynk’s example sketch is Ethernet based and limited to showing the up time of your hardware so let’s make it a little more useful and run it on an ESP. I was recently asked if Blynk could turn a device on and then off 30 minutes later. The TimeInput widget would be fine for this but it’s not the easiest widget to follow for beginners so let’s use SimpleTimer and a slider.

// thirtysecondtimer.ino by Costas 12 Dec 2016
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>      // library for SimpleTimer       

SimpleTimer timer;            // define a timer for use by SimpleTimer library
int Countdown;                // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;       // variable to switch device ON and OFF
char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BLYNK_WRITE(V0){   // add a slider to your project on V0 range 0 to 30 (minutes)
  Countdown = param.asInt();  // set variable as Slider value
}

void runEveryMinute(){ // runs every 60s, will do noting when Slider is at zero
  
  if((Countdown > 0)&& (ONstatus == true)){
    Countdown--;    //  reduce Countdown by 1 minute every 60s
    Serial.print(F("Device will switch OFF in "));
    Serial.print(Countdown);
    Serial.println(F(" minute(s)"));    
  }  
  
  if((Countdown > 0) && (ONstatus == false)){
    Serial.println(F("Device was switched ON"));
    ONstatus = true;   // to ensure device is only turned ON once
    // code here to turn your device ON 
  }

  if((Countdown == 0) && (ONstatus == true)){
    Serial.println(F("Device is now OFF"));
    ONstatus = false;    // to ensure device is only turned OFF once
    // code here to turn your device OFF
  }
}

void setup()
{
  Serial.begin(115200); 
  Blynk.begin(auth, ssid, pass);  // this is now a blocking function - more on this later
  timer.setInterval(60000L, runEveryMinute);  // start the 60s timer function  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running 
}

We have tried to keep the sketch as basic as possible and as such it has some limitations. For example when you move the Slider it will take up to 60 seconds for “Device was switched ON” to appear in Serial Monitor.
There are many ways to remove this limitation and guidance can be found by studying SimpleTimer in detail at http://playground.arduino.cc/Code/SimpleTimer

23 Likes

@vshymanskyy, can we pin this topic somehow?

It’s posted elsewhere but just adding it here as there are still lots of Blynkers posting grotesque code.

All code snippets should be formatted

Watch the animated gif at [README] Welcome to Blynk Community!

3 Likes

Are you looking for help with your Blynk project?

Struggling to get your hardware connected to a Blynk server?

As you might expect Blynk developers are busy developing a great product and I’m sure they accept they need to be involved in bug fixing, but they probably have better things to do than answer the same question over and over again.

Be sure to do a thorough search of the site before asking a question that has been answered over and over again.

Looking for a quick solution to your problem?

If you don’t want to suffer needless delays I would suggest you start your post with the following details:

 1. The Blynk library version you are using.
 2. Android or iOS and the version of the OS.
 3. Server type, Local or Cloud based.
 4. If local server, which version you are using.
 5. How you powering the MCU.
 6. Has the MCU ever connected to a Blynk server (Cloud or local).
 7. The IDE you areusing, including the version number.
 8. The settings you have in the IDE for the MCU.
 9.The Arduino core version you are using.
10. The make and model of your phone.
11.Your shoe size and anything else you think is important.

You might know what phone you are using but nobody else does unless you provide the details. The assumption is always that you are using Blynk’s cloud server as most people are but ensure you actually state it in the thread.

Many threads go on and on before the OP decides to confess they are using a local server.

Once you have stated the basic details indicated above, describe your problem in detail.

1. What have you tried?
2. What did you expect to happen?
3. What actually happened?
4. Provide screenshots if applicable.
5. Provide formatted code extracts.

If you post a thread simply with “Help I have a problem” don’t expect too much help from Blynkers.

Don’t forget the site has a very impressive search facility and Google is your friend.

Let’s all try and help the developers develop.

7 Likes

as always @Costas, :pray:. You are the best

A simple sketch to control relays with an ESP. Be sure to read ALL the notes. SimpleTimer is included as it is imperative for almost all Blynk sketches. Only a very experienced coder could even attempt a Blynk project using millis(). This sketch was posted in response to the “LED widget” thread at LED and LCD widgets

If anything is not clear please ask for clarification.

/*************************************************************
 LED_Relay.ino for ESP's with Blynk  http://community.blynk.cc/t/led-widget/11582/5
 Mod of PushData.ino from Ethernet to ESP by Costas 10 Feb 2017
 Control a relay and show status with virtual LED, tested on WeMos D1 Mini
 Pinout details for WeMos D1 Mini and Mini Pro https://www.wemos.cc/product/d1-mini-pro.html
 
 For active LOW relays like the onboard LED on a WeMos, 
 Swap lines at rows 41 and 47 for active HIGH relays
 
 V0 - Virtual Button in SWITCH mode
 V1 - Virtual LED
 V2 - Value Display widget demonstrating SimpleTimer, set as PUSH READING FREQUENCY

 ESP8266 slected as Device in app, not WeMos
 *************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define vRelayBtn V0
#define vLED      V1
#define vDisplay  V2 

#define vLEDHigh 255
#define vLEDLow    0
#define relayPin   2   // (D4 on WeMos, builtin LED, active LOW)

char auth[]   = "91xxxxxxxxxxxxxxxx2e22a";
char ssid[]   = "xxxxxxxxxx";
char pass[]   = "xxxxxxxxxx";
char server[] = "blynk-cloud.com";
unsigned int vRelayStatus;

SimpleTimer testingTimer;

BLYNK_WRITE(vRelayBtn) {     // Blynk app WRITES button status to server
  vRelayStatus = param.asInt();
  if(vRelayStatus == 1){
    digitalWrite(relayPin, LOW);        // see note at line 8
    Blynk.virtualWrite(vLED, vLEDHigh);
    Serial.println(F("Relay is now ON"));    
  }
  else{
    digitalWrite(relayPin, HIGH);       // see note at line 8
    Blynk.virtualWrite(vLED, vLEDLow);
    Serial.println(F("Relay is now OFF"));   
  }
}


void myTimerEvent()  // included because SimpleTimer is essential for "ALL" Blynk sketches
{
  Blynk.virtualWrite(vDisplay, millis() / 1000);        // max 10 values per second
  // any other function can also go here, perhaps read a physical button HIGH / LOW status
}

void setup()
{
  pinMode(relayPin, OUTPUT);
  Serial.begin(115200);                           // Debug console
  Blynk.begin(auth, ssid, pass, server);
  testingTimer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
}

void loop()
{
  Blynk.run();
  testingTimer.run(); // Initiates SimpleTimer
}
4 Likes

simple question - what is the purpose and function of F in the println function ?

@mars compile the sketch with and without the F’s (and associated opening and closing brackets) and check program space used.

With the F’s

Sketch uses 236,641 bytes (22%) of program storage space. Maximum is 1,044,464 bytes.
Global variables use 34,560 bytes (42%) of dynamic memory, leaving 47,360 bytes for local variables.

Without the F’s

Sketch uses 236,529 bytes (22%) of program storage space. Maximum is 1,044,464 bytes.
Global variables use 34,592 bytes (42%) of dynamic memory, leaving 47,328 bytes for local variables.

Not important with a simple sketch like this one but if your code has a few thousand lines with lot’s of Serial Print’s it can make a difference.

thanks Costas but I’m still not clear as to what the F does :wink:
it seems to use more memory based in your notes above.

A throwback to working with Nano’s 32KB of Flash memory and just 2KB of RAM.

Dynamic memory (RAM) is much more limited than Flash (Programmable memory).

Take a look at this Arduino thread and see if that makes it clearer https://forum.arduino.cc/index.php?topic=428015.0

1 Like

got it thanks - the F in println forces the use of flash vs dynamic ram.

1 Like

I agree… and would amend that with “experienced or bullheaded coderwanabees”… which of course explains why I skipped over millis() and went right for delayMicroseconds() :stuck_out_tongue:

Great goto topic @Costas… I think I will bookmark it as well :wink:

With regards to the F in print() statements… am I correct in thinking that this is only a benefit when using ‘Pre-written’ strings (actual text in-between the brackets) as opposed to variables that refer to a bunch of combined strings or other incoming data?

println(F("This Is Data! "));

vs

u = "That ";
v = "This ";
w = "Was ";
x = "Is ";
y = "Data! ";
z = "Sparta! ";

println(F(v + x + y));  // Is the F of any dynamic memory benefit here?

Aside from the fact that the variables themselve take up memory.

1 Like

Yes, the F macro will not accept the following based on u, v, w defined as String’s.
Serial.println(F(u + v + w));

@Costas I saw that you managed to install blynk on an omega 2, I’m trying to install it following this tutorial

But I have an error in step 2 (./scripts/feeds update -a)

/bin/ash: ./scripts/feeds: not found

Could you tell me how you installed it?

@Omar_Mariscal do you have a 2 or a 2+? You need a 2+ or some way of transferring the file system to a USB stick if you are using a 2 as it’s doesn’t have enough memory for all the required packages.

The link you gave is giving me a 404 error, did you mean this page https://github.com/vshymanskyy/OpenWRT-Espruino-packages

This is the tutorial I followed https://wiki.onion.io/Tutorials/blynk-library and I think there was just a minor path mod that I needed to do. The tutorial is for the Omega 1 but it worked pretty much the same for the 2+

Please don’t spam multiple help requests across different threads… you have already received an answer to your question in another old thread you re-opened.

2 Likes