Blynk on ESP32 and ESP8266: virtual pins, timers and BLYNK_WRITE
Blynk connects a board to a phone app and a web dashboard through its cloud. The board never talks to the phone directly: it logs in to the Blynk server with an auth token, and widgets in the app read and write virtual pins, V0 to V255, which are just numbered channels for any value you like.
Built in on Mokxi: #include <BlynkSimpleEsp8266.h> and it builds, with nothing to install. Free, and no account needed.
What Blynk does
Every Blynk IoT sketch starts with three defines copied from the Blynk console, BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME and BLYNK_AUTH_TOKEN, placed above the includes. The board header you include picks the WiFi stack: BlynkSimpleEsp32.h, BlynkSimpleEsp8266.h, or BlynkSimpleWifi.h on a Pico W or Uno R4 WiFi.
The rule that matters most: loop() should contain Blynk.run() and timer.run() and nothing else. Values go out from functions that BlynkTimer calls on an interval, and values come in through BLYNK_WRITE(Vn) handlers, which run inside Blynk.run(). A delay() in loop(), or writing to the server on every pass, gets the board disconnected or rate limited.
| Library Manager name | Blynk |
|---|---|
| Author | Volodymyr Shymanskyy |
| Include | #include <BlynkSimpleEsp8266.h>#include <BlynkSimpleEsp32.h>#include <BlynkSimpleWifi.h>#include <WidgetLCD.h> |
| Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi |
| In the simulator | Runs, with a gap the note below names. |
Install it
It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.
In Mokxi it is built in: #include <BlynkSimpleEsp8266.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Blynk" by Volodymyr Shymanskyy from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add Blynk to lib_deps in PlatformIO. Mokxi follows the calls of release 1.3.2, so a sketch written here builds there too.
What to know in the simulator: There is no TCP or blynk.cloud here: the server is simulated, and the Cloud panel鈥檚 Blynk section is its console, showing the virtual pins and sending values to BLYNK_WRITE. The token is not checked; BLYNK_READ, Edgent and OTA are not simulated. BlynkSimpleEsp8266.h is for the ESP8266, BlynkSimpleEsp32.h for the ESP32 boards and BlynkSimpleWifi.h for the Pico W and the UNO R4 WiFi. WidgetLCD鈥檚 clr and print(x, y, text) are shown as its pin鈥檚 value, not drawn as an LCD.
ESP32-C3 路 ESP32-C6 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32 DevKit 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 ESP32-S3-DevKitC-1 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 ESP8266 NodeMCU 路 Raspberry Pi Pico W 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Blynk.begin(BLYNK_AUTH_TOKEN, "ssid", "password")
Joins WiFi, connects to the server and logs in, and blocks until that is done. Blynk.config() with your own WiFi.begin() is the non-blocking way.
Blynk.run()
Keeps the connection, reads what the server sent and runs your BLYNK_WRITE handlers. Call it on every pass of loop().
Blynk.virtualWrite(V0, analogRead(A0))
Sends a value to a virtual pin, which a gauge, label or chart in the app shows. Send from a timer, about once a second, not from loop().
BLYNK_WRITE(V1)
Declares the handler for a value the app sends to V1, for example from a switch. param holds the value: param.asInt(), param.asFloat() or param.asStr().
BLYNK_WRITE(V1) {
digitalWrite(LED_PIN, param.asInt());
}BlynkTimer timer
Blynk鈥檚 own timer, up to sixteen of them, run from timer.run() in loop(). It is how a Blynk sketch does something every few seconds without delay().
timer.setInterval(1000L, sendSensor)
Calls sendSensor() every 1000 ms. The L matters on AVR boards, where an int stops at 32767.
BLYNK_CONNECTED()
Runs each time the board logs in. Put Blynk.syncVirtual(V1) in it to pull the app鈥檚 last values back after a reset.
Blynk.logEvent("high_temp", "Over 30 C")
Raises an event set up in the template, which Blynk can turn into a notification or an email.
Example: A light monitor on Blynk: V0 is the light, V1 the LED
On the ESP8266 NodeMCU V1.0. It runs in the browser exactly as printed.
#define BLYNK_TEMPLATE_ID "TMPL3xYzAbCd"
#define BLYNK_TEMPLATE_NAME "Light Intensity Monitor"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
#define BLYNK_PRINT Serial
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define LED_PIN D1
#elif defined(ESP32)
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define LED_PIN 2
#else
#include <BlynkSimpleWifi.h>
#define LED_PIN 2
#endif
// Open the Cloud panel: V0 shows the light level, and sending 1 to V1
// switches the LED from its Blynk section, as the app's switch would.
BlynkTimer timer;
BLYNK_WRITE(V1) {
digitalWrite(LED_PIN, param.asInt());
}
void sendLight() {
Blynk.virtualWrite(V0, analogRead(A0));
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, "Mokxi-Lab", "breadboard");
timer.setInterval(1000L, sendLight);
}
void loop() {
Blynk.run();
timer.run();
}Wiring
- ESP8266 NodeMCU V1.0 3V3 to Light sensor 1
- Light sensor 2 to ESP8266 NodeMCU V1.0 A0
- ESP8266 NodeMCU V1.0 A0 to Resistor 1
- Resistor 2 to ESP8266 NodeMCU V1.0 GND
- ESP8266 NodeMCU V1.0 D1 to Resistor 1
- Resistor 2 to LED A
- LED C to ESP8266 NodeMCU V1.0 GND
Common errors and how to fix them
#error "Please specify your BLYNK_TEMPLATE_ID and BLYNK_TEMPLATE_NAME"
The three defines must come before #include <BlynkSimpleEsp32.h>, at the very top of the sketch, exactly as the Blynk console shows them. If they are below the include, the library never sees them.
The device shows offline, or keeps reconnecting
Something blocks Blynk.run(): a delay() in loop(), a long while loop, or a sensor read that waits. Move the work into a BlynkTimer function and keep loop() to Blynk.run() and timer.run().
Flood error or "Trouble detected" in the console
virtualWrite() is being called on every pass of loop(), hundreds of times a second. Send values from a timer at most a few times a second, and only when they change if you can.
Questions and answers
Can I try Blynk in Mokxi without an account?
Yes. Mokxi simulates the Blynk server, and the Cloud panel鈥檚 Blynk section shows each virtual pin and lets you send values to BLYNK_WRITE. The token is shown but not checked; BLYNK_READ, Edgent and OTA do not run.
Try Blynk without installing anything
Open the example, change the sketch, and keep your version in a free account.