Blynk, simulated

Run a Blynk sketch unchanged on an ESP8266, ESP32 or Pico W, and watch and set its virtual pins in the Cloud panel.

The Blynk server is simulated. Nothing reaches blynk.cloud, and there is no Blynk account, app or template behind it. What is faithful is the Arduino code you write: Blynk.begin(), Blynk.virtualWrite(), BLYNK_WRITE and BlynkTimer behave the way the Blynk library does on a real board, so the sketch you test here is the sketch you flash later.

The headers

  • ESP8266 (NodeMCU, D1 mini): #include <BlynkSimpleEsp8266.h>
  • ESP32 boards (ESP32, ESP32-C3, ESP32-C6, ESP32-S3 and the boards built on them): #include <BlynkSimpleEsp32.h>
  • Pico W and UNO R4 WiFi: #include <BlynkSimpleWifi.h>

Each one checks the board as Blynk's own does: the ESP8266 header on an ESP32 stops with This code is intended to run on the ESP8266 platform!

A light monitor

An LDR from 3V3 to A0, a 10k resistor from A0 to GND, and an LED with a 220 ohm resistor on D1:

#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"
#define BLYNK_TEMPLATE_NAME "Light Intensity Monitor"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

BLYNK_WRITE(V1) {
  digitalWrite(D1, param.asInt());
}

void sendLight() {
  Blynk.virtualWrite(V0, analogRead(A0));
}

void setup() {
  Serial.begin(115200);
  pinMode(D1, OUTPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, "Mokxi-Lab", "breadboard");
  timer.setInterval(1000L, sendLight);
}

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

The network name and password are the board's wifiSsid and wifiPassword properties, as in WiFi, simulated. The library examples in the editor have this one ready to run.

The Blynk section of the Cloud panel

Open Cloud beside Serial while the sketch runs. Once the board logs in, a Blynk section shows:

  • whether the device is online, its template name and ID, and the start of its token (shown, never checked);
  • one row per virtual pin the sketch wrote or has a BLYNK_WRITE for, with its value, who set it and when. V0 above shows the light level, and moving the LDR's slider changes it on the next send;
  • 0, 1 and a box with Send on each row, which is the Blynk app's switch or slider: send 1 to V1 and the sketch's BLYNK_WRITE(V1) turns the LED on. Any pin sends to a pin that has no row yet;
  • the last events from Blynk.logEvent().

What is real and what is not

Real, the way the Blynk library does it:

  • Blynk.begin() waits until the board is on the network and logged in;
  • BLYNK_WRITE runs inside Blynk.run(), so a loop() that stops calling it stops hearing the panel, as a real board stops hearing the app;
  • BLYNK_CONNECTED runs on each login, and Blynk.syncVirtual() or syncAll() there brings the server's last values back into BLYNK_WRITE;
  • a write while the board is offline is dropped, and a board that leaves the network logs in again five seconds after it is back;
  • a float is sent with three decimals and a double with seven;
  • param.asInt(), asFloat(), asStr() and param[1] for a pin with several values; WidgetLED, WidgetTerminal, WidgetLCD and Blynk.setProperty() (old sketches that include WidgetLCD.h, WidgetLED.h, WidgetTerminal.h or BlynkWidgets.h by name build too);
  • Blynk 0.6's Blynk.notify(), Blynk.email() and Blynk.tweet(), from old tutorials, are listed as events: nothing is pushed, mailed or posted.

Not simulated:

  • the Blynk app, web dashboard and account: the Cloud panel stands in for them;
  • the LCD widget is not drawn: its pin shows the last message the library sent, clr or p, x, y, text (so lcd.print(4, 1, "World") reads p, 4, 1, World);
  • BLYNK_APP_CONNECTED, which compiles and never runs, since nothing opens an app (BLYNK_READ is gone from Blynk 1.4.0: a sketch that uses it gets a build error saying to push the value with Blynk.virtualWrite() from a BlynkTimer);
  • Blynk.Edgent (BlynkEdgent.h), firmware updates over the air and the _SSL headers.