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