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

Blynk for Beginners and help with your project


#1

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


[SOLVED] Using BLYNK_WRITE() with loop()
Soil Moisture Sensor with gauge
Hi is anyone to know how set Slider widget to set a minutes
Dht22 and ds18b20 together?
Yet Another Automated Plant box project. Please help me refine my code
I need a FAIL SAFE
Help with value float!
Esp8266 software serial problm
Need help in setting up my nodemcu esp8266 to control two dc motors
Blynk slow response / no response at all
[Solved] Heartbeat timeout with rRF24 and ENC28J60
Newbie : how to read digital inputs (sensors) with Wemos D1 mini?
Button trigger
LED and LCD widgets
ESP8266 with LCD, ESP8266 with DHT
Device was disconnected Wemos mini
How to tidy and write code properly
Unable to get Eventor to work with my project
Low Cost 4 channel Wifi switch
How do I use virtual pins as buttons?
#2

@vhymanskyy, can we pin this topic somehow?


#3

Stopped before I got started (SparkFun IOT Blynk box)
#4

#5

#6

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!


Blynk local server on Raspverry Pi and timer widget
#7

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.


[SOLVED] Xmas Lights
Automatic scheduler. ESP-01 with 4 Time Input Widgets
#8

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


#9

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
}

LED and LCD widgets
Raspberry wiringPi: virtual Pin control output
#10

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


#11

@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.


#12

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.


#13

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


#14

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


#15

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:


#16

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.


#17

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


#18

@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?


#19

@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+


#21

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.