Quickstart Device: Code Overview

Now that you know how to Quickstart device was prepared, let's look at the code it used to connect and communicate with Blynk.

Code depends on the hardware and connection method you use. You can always find a correct code example for your hardware here.

Let's look at the parts of the code one by one.

Definitions

In the beginning, we define three main parameters.

#define BLYNK_TEMPLATE_ID      "MyTemplateID"
#define BLYNK_TEMPLATE_NAME    "MyTemplateName"
#define BLYNK_AUTH_TOKEN       "MyAuthToken"

You already know what a TemplateID is. Template Name can be anything that helps you manage your different templates.

AuthToken is a unique identifier generated by Blynk.Cloud. Every device should have it. For the Quickstart process, a new device was generated automatically and a new AuthToken was generated in the system for it. We just took this AuthToken and inserted it into your sketch.

You can always find AuthToken for your Device in the Device Info section of the selected device.

Authtoken

Getting an AuthToken manually is the simplest way to get your hardware connected. However, we also offer an alternative way for WiFi devices, where AuthToken can be saved to the device without specifying it beforehand.

You can learn more about such a process here.

Let's continue looking at the sketch.

#define BLYNK_PRINT Serial

Here we are enabling output to Serial monitor of what Blynk is doing for debugging purposes.

Hardware specifics

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

These lines above are specific to your hardware. For example, it you are working with NodeMCU, these lines will be different.

WiFi Credentials

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

You will only see these lines when you work with a WiFi-enabled device. Replace it with your home or office WiFi network name and password.

If your network doesn't have a password - leave it empty: char pass[] = ""

Many devices can only connect to 2.4Ghz networks. Double-check specs of your hardware to make sure you will be connecting to the correct network.

In some cases, you won't know the credentials upfront. For that, we offer an alternative way to provision devices with WiFi credentials using Blynk.Apps. You can read more about it here.

Timers

When working with IoT devices, you usually need to send data in certain intervals. It's a bit different from how you usually work with Arduino devices when MCU can process thousands of commands per second.

When working with the cloud you need to define how often to send the data to the cloud.

Blynk.Library offers its own timer. These are the lines in your sketch that work with timer. You will learn how to use timers later in this guide.

BlynkTimer timer; // Creating a timer object


void setup()
{
...
  timer.setInterval(1000L, myTimerEvent); //Staring a timer
...  
}


void myTimerEvent() // This loop defines what happens when timer is triggered
{

}

void loop()
{
  Blynk.run();
  timer.run(); // runs the timer in the loop
}

Sending/receiving data

Mobile apps and web dashboards have widgets that can control and monitor your device remotely.

In the Quickstart Template, we used four widgets placed on the dashboards. Each widget is assigned a certain Datastream.

  1. Button Widget writes 1 and 0 to V0 Datastream

  2. Two Label Widgets, that read the value from V1 Datastreamand V2 Datastream

  3. Web Button Image - it's a button where you can add custom images for ON and OFF state. On click it can open a web page in Blynk.App

Button

The code below listens for actions from Blynk to V0 Datastream, and then records the value to a variable. We use BLYNK_WRITE(V0) for that.

BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

After the value from Button was recorded, we also send it to the Label Widget using Blynk.virtualWrite(V1, value);

Label Widget

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

This piece of code is for the timer that updates Label Widget attached to V2 Datastream every second. All we do is just send the uptime (time in seconds counting from device boot up).

Updating Web Image Button Widget

With Blynk you can change different parameters of widgets directly from hardware.

BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/template-quick-setup");
}

In the code above we track when the device connects to the Blynk.Cloud with BLYNK_CONNECTED. When device successfully connects to the Blynk Cloud, we change the URL of an image to show in the widget and also assign a click URL


Void setup() and void loop()

void setup()
{
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  // Setup a timer function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

This is a standard Arduino code which runs once on device boot up. With Blynk.begin() we use AuthToken and WiFi credentials to get your device online and authenticate in Blynk.Cloud.

Then we launch the timer to run it every minute (it was used to send uptime)

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

void loop() is also a standard Arduino code that continuously runs after void setup().

Blynk.run() - this is where all the Blynk magic happens. This routine keeps the connection with the Blynk.Cloud, syncs the data, checks for new firmware OTA and much more

timer.run() runs the timer function

It's important to keep the void_loop() clean. Avoid using delays and use timers when you need to send data.

Now you are ready to build your own devices! Check out these guides to learn more about Blynk features:

Device Activation MethodsTemplate Quick SetupControl Devices (GPIOs and beyond)Events

Last updated

Revision created