Skip to content
IoT / CloudMIT (library) — Blynk.Cloud service itself is commercial/freemiumv1.3.2

Blynk

by Blynk Inc.

Blynk is a hosted IoT platform plus a matching Arduino library. It lets a microcontroller send sensor values to a mobile or web dashboard, and receive commands from switches and sliders, without you building your own server or app. `Blynk.virtualWrite(V0, value)` sends a value to a "virtual pin" (a datastream) on the dashboard, and a `BLYNK_WRITE(V1)` handler runs whenever the app sends a new value back. The biggest source of confusion is that there are two different Blynks. The original "Blynk Legacy" app and servers were discontinued, and the current service is Blynk IoT (Blynk.Cloud). Old tutorials that use only an auth token from the legacy app will not work. With current Blynk you first create a template and a device in the Blynk web console, and the code needs three defines placed before any Blynk include: `BLYNK_TEMPLATE_ID`, `BLYNK_TEMPLATE_NAME` and `BLYNK_AUTH_TOKEN`. If your device shows as offline or you see an "invalid auth token" message, check that all three values come from the same template and device. The free plan has limits on devices and datastreams, so check the current pricing page before you plan a large project. The second common mistake is writing to Blynk too often. The server accepts only a limited number of messages per second, and sending a value on every pass of `loop()` causes a flood error and a forced disconnect. Use `BlynkTimer` to send data every one or two seconds, and keep `loop()` free of long `delay()` calls, because `Blynk.run()` must run constantly to keep the connection alive. Add `#define BLYNK_PRINT Serial` before the include to see connection messages in the serial monitor while debugging. Two more behaviours matter in real projects. First, `Blynk.begin()` blocks until it connects, so if the Wi-Fi or the server is down your device does nothing, including local control. For a product that must keep working offline, use `Blynk.config()` and `Blynk.connect(timeout)` as in the example below. Second, when a device restarts, widgets in the app do not automatically tell the board their current state, so call `Blynk.syncVirtual()` inside `BLYNK_CONNECTED()` to restore it. Be aware that you depend on a commercial cloud service. If you want full control and no limits, MQTT with Home Assistant or Node-RED is the usual alternative, and the PubSubClient page covers it. Note for pin numbers: use real GPIO numbers on the ESP32, such as `analogRead(34)`. The Arduino-style name `A0` does not exist on that board.

Installation

Arduino IDE — Library Manager

Sketch → Include Library → Manage Libraries → search Blynk

PlatformIO

lib_deps = blynkkk/Blynk

Supported boards

Arduino UnoESP32STM32 Blue Pill

Examples

Send a sensor reading to the dashboard (ESP32)

Sends a value every 2 seconds with BlynkTimer, so it never floods the server. Replace the three defines with the values from your Blynk template and device.

#define BLYNK_TEMPLATE_ID   "TMPLxxxx"
#define BLYNK_TEMPLATE_NAME "MyDevice"
#define BLYNK_AUTH_TOKEN    "your_auth_token"
#define BLYNK_PRINT Serial          // shows connection status in the serial monitor

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

char ssid[] = "YOUR_WIFI";
char pass[] = "YOUR_PASSWORD";

BlynkTimer timer;

void sendReading() {
  int raw = analogRead(34);         // a real GPIO number, not A0
  Blynk.virtualWrite(V0, raw);
}

void setup() {
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(2000L, sendReading);
}

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

Receive a switch and a slider from the app

BLYNK_WRITE runs automatically whenever the app changes that virtual pin. Add this to the sketch above and create a Switch on V1 and a Slider on V2 in the dashboard.

#define LED_PIN 2

BLYNK_WRITE(V1) {                 // switch widget
  int state = param.asInt();      // 0 or 1
  digitalWrite(LED_PIN, state);
}

BLYNK_WRITE(V2) {                 // slider widget
  int value = param.asInt();
  Serial.print("Slider: ");
  Serial.println(value);
}

// in setup(): pinMode(LED_PIN, OUTPUT);

Restore widget state after a restart

Asks the server for the last saved values of V1 and V2 whenever the device connects, so a reboot does not leave the board out of step with the app.

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1, V2);    // triggers BLYNK_WRITE(V1) and BLYNK_WRITE(V2)
}

Keep running when Wi-Fi or Blynk is down

Blynk.begin() blocks until it connects. This version never blocks, so local control, sensors and displays keep working while the connection is lost.

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  Blynk.config(BLYNK_AUTH_TOKEN);          // set up only, no blocking connect
  timer.setInterval(2000L, sendReading);
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    if (!Blynk.connected()) {
      Blynk.connect(3000);                 // try for up to 3 seconds
    } else {
      Blynk.run();
    }
  }

  timer.run();
  // ...your local logic runs here even when offline
}

Send a notification when a limit is crossed

Uses a Blynk event. Create an event with the code high_temp in the Blynk console first. The flag stops it sending a message on every reading.

bool alertSent = false;

void checkTemperature(float temp) {
  if (temp > 35.0 && !alertSent) {
    Blynk.logEvent("high_temp", "Temperature is above 35 C");
    alertSent = true;
  }
  if (temp < 33.0) {
    alertSent = false;                     // re-arm with a small gap to avoid chatter
  }
}