virtualWrite causes cmd error

Hi all,

Just starting out with Blynk and loving it so far, except for this issue I’ve encountered.
I’m using an ESP8266 (Sonoff TH16 to be exact).

I tried to use virtualWrite() in my sketch to sync hardware button and app button, but am receiving a “cmd error” (and then a reconnect).

While debugging, I tried to run some of Blynk’s examples, and found out that ButtonInterrupt has the exact same issue for me. So I’ll be using that instead of my sketch for simplicity. The only thing I needed to change was button pin to 0.
(Also, please note that other examples, such as ButtonPoll, worked perfectly for me!)

Code and log below.

Any thoughts or suggestions? What am I missing?

My only guess as to what’s happening is that virtualWrite() doesn’t work if called inside an interrupt (pin interrupt in the example and timer interrupt in my original sketch). Is it a known issue? Did anyone else have success with ButtonInterrupt on the ESP8266?

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial

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

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

WidgetLED led1(V1);

void checkPin()
{
  // Invert state, since button is "Active LOW"
  if (digitalRead(0)) {
    led1.off();
  } else {
    led1.on();
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  // Make pin 0 HIGH by default
  pinMode(0, INPUT_PULLUP);
  // Attach INT to our handler
  attachInterrupt(digitalPinToInterrupt(0), checkPin, CHANGE);
}

void loop()
{
  Blynk.run();
}
[31617] Connected to WiFi
[31618] IP: 192.168.1.140
[31618] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.6 on ESP8266

[31691] Free RAM: 45648
[31717] Connecting to blynk-cloud.com:8442
[32102] <[02|00|01|00] <AUTH TOKEN HERE>
[32422] >[00|00|01|00|C8]
[32422] Ready (ping: 1ms).
[32422] <[11|00|01|00]Hver[00]0.4.6[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP8266[00]build[00]Mar 23 2017 14:28:42[00]
[32794] >[00|00|01|00|C8]
[42794] <[06|00|02|00|00]
[43124] >[00|00|02|00|C8]
[53125] <[06|00|03|00|00]
[53468] >[00|00|03|00|C8]
[56497] <[14|00|04|00|08]vw[00]1[00]255
[61498] Cmd error
[61498] Cmd skipped:20
[61499] Connecting to blynk-cloud.com:8442
[61832] <[02|00|01|00] <AUTH TOKEN HERE>
[62151] >[00|00|01|00|C8]
[62151] Ready (ping: 1ms).
[62151] <[11|00|06|00]Hver[00]0.4.6[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP8266[00]build[00]Mar 23 2017 14:28:42[00]
[62579] >[14|00|01|00|02]

Hello. Hm. Look like this is something specific to interrupts and ESP. I remember @vshymanskyy recently found some issue with it. For now recommendation would be to use polling.

Yes, exactly. ESP8266 doesn’t allow sending WiFi data from inside the interrupt handler.
You should set the flag that the data has changed and then call blynk functions inside main loop context.

Thanks guys for the quick response!

I implemented your suggestions and it’s working fine now.

I feel that this limitation is not communicated enough though - I couldn’t find a mention of it anywhere in the docs nor on this forum. Perhaps add a mention of it to the “Limitations and Recommendations” section? Also it’s very confusing when the examples.blynk.cc generator creates an example that doesn’t actually work :slight_smile:

Keep up the great work!

Agree. We will fix that soon.