Wemos d1 & blynk

Hello everyone ! someone can tell me why the board continues to disconnect? I think that the code is very basic … I don t understand what is incorrect …

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "******"; //CHIAVE DEL PROGETTO
int buttonPin = 14 ;                              //PULSANTE VIRTUALE
int buttonState = 0;                              //VARIABILE PULSANTE
int buttonVelux = 5 ;
int buttonStateVelux = 0; 



WidgetLED led1(V10);
WidgetLED led2(V11);
WidgetLED led3(V6);
WidgetLED led4(V7);


void setup()
{

  
  pinMode(buttonPin, INPUT);
  pinMode(buttonVelux, INPUT);
  
  Serial.begin(9600);
  Blynk.begin(auth, "*****", "******");


}


void loop()
{
  Blynk.run();

  buttonState = digitalRead(buttonPin);
  buttonStateVelux = digitalRead(buttonVelux);
  
  
  
  if (buttonState == HIGH) {
    
                  led3.on();
                  led4.off();
                  Blynk.notify("A/C ACCESA!");
                  Serial.print(buttonState);
        }else{
                  led3.off();
                  led4.on();
        }
    
  if(buttonStateVelux == HIGH) {
              
                led1.on();
                led2.off();
       }else{
                led1.off();
                led2.on();

       }
       
//buttonState = 0;
//buttonStateVelux = 0;
}

thank you!!!

Reading buttonState a thousand times a second will not help.

Go for a maximum of 2 lines of code in the loop and use SimpleTimer to call your functions. That is why SimpleTimer is one of the 4 libraries you must install to use Blynk.

thanks for your help!
I tried to change the code to use it as a “stand alone” module, but at the end of compilation comes back to me these errors:

#define BLYNK_PRINT Serial
#include <SPI.h>
//#include <Ethernet.h>
//#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "***************";

SimpleTimer timer;
ESP8266 wifi(EspSerial);
void setup()
{

    Serial.begin(9600);
    Blynk.begin(auth, "**********", "**********");
    //Blynk.begin(auth);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Notify immediately on startup
  Blynk.notify("Device started");

  // Setup a function to be called every minute
// timer.setInterval(60000L, notifyUptime);

  // Setup notification button on pin 2
  pinMode(5, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);
}

void notifyUptime()
{
  long uptime = millis() / 60000L;

  // Actually send the message.
  // Note:
  //   We allow 1 notification per minute for now.
  Blynk.notify(String("Running for ") + uptime + " minutes.");
}

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(5);
  if (isButtonPressed) {
    Serial.println("Button is pressed.");

    Blynk.notify("Yaaay... button is pressed!");
  }
}

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

error: ‘notifyOnButtonPress’ was not declared in this scope

attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, CHANGE);

exit status 1
‘notifyOnButtonPress’ was not declared in this scope

any helps pls?

The following compiles with CHANGE or RISING (normally RISING is better).
Commented out ESP8266 wifi(EspSerial) and placed #include <SimpleTimer.h> after #include <BlynkSimpleEsp8266.h>. Don’t think you need #include <SPI.h> either but I have left it in.

#define BLYNK_PRINT Serial
#include <SPI.h>
//#include <Ethernet.h>
//#include <BlynkSimpleEthernet.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "***************";
SimpleTimer timer;
//ESP8266 wifi(EspSerial);
void setup()
{
    Serial.begin(9600);
Blynk.begin(auth, "**********", "**********");
//Blynk.begin(auth);
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  // Notify immediately on startup
  Blynk.notify("Device started");
  // Setup a function to be called every minute
// timer.setInterval(60000L, notifyUptime);
  // Setup notification button on pin 5
  pinMode(5, INPUT_PULLUP);
  // Attach pin 5 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, RISING);
}
void notifyUptime()
{
  long uptime = millis() / 60000L;
  // Actually send the message.
  // Note:
  //   We allow 1 notification per minute for now.
  Blynk.notify(String("Running for ") + uptime + " minutes.");
}
void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(5);
  if (isButtonPressed) {
    Serial.println("Button is pressed.");
    Blynk.notify("Yaaay... button is pressed!");
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}
1 Like

Ty Costas for your patience …
I modified the scheme as you described it keeps giving me these errors in the compilation:

Arduino: 1.6.6 (Windows 10), detail: “Wemos D1 R2 & mini, 80 MHz, 921600, 4M (3M spiffs)”

C: \ Program Files (x86) \ Arduino \ arduino-builder -dump-prefs -logger machine -hardware = “C: \ Program Files (x86) \ Arduino \ hardware” -hardware "C: \ Users \ Daniel \ AppData \ Local \ Arduino15 \ packages “-tools” C: \ Program Files (x86) \ Arduino \ tools-builder “-tools” C: \ Program Files (x86) \ Arduino \ hardware \ tools \ avr “-tools” C: \ Users \ Daniel \ AppData \ Local \ Arduino15 \ packages “-built-in-libraries” C: \ Program Files (x86) \ Arduino \ libraries “-libraries” C: \ Users \ Daniel \ Documents \ Arduino \ libraries "

prova_notifica_wemos_D1: 29: error: ‘notifyOnButtonPress’ was not declared in this scope

attachInterrupt (digitalPinToInterrupt (5), notifyOnButtonPress, RISING);

‘NotifyOnButtonPress’ was not declared in this scope

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "*************";

SimpleTimer timer;
//ESP8266 wifi(EspSerial);   <-------- commented

void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth, "******", "*******");
     while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Notify immediately on startup
  Blynk.notify("Device started");

  // Setup a function to be called every minute
  // timer.setInterval(60000L, notifyUptime);    <--------commented??

  // Setup notification button on pin 5
  pinMode(5, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(5), notifyOnButtonPress, RISING);
}

void notifyUptime()
{
  long uptime = millis() / 60000L;

  Blynk.notify(String("Running for ") + uptime + " minutes.");
}

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(5);
  if (isButtonPressed) {
    Serial.println("Button is pressed.");

    Blynk.notify("Yaaay... button is pressed!");
  }
}

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

Upgrade your Arduino IDE to 1.6.9 (not 1.6.10 or 1.6.11) and try again. 1.6.6 is quite old.

Or try this above the setup() routine:

void notifyOnButtonPress();

It’ll declare the function you made so the program knows it exists. Sometimes it’s needed (and truth be told, it’s nicer in terms of programming neat-ness : ) )

Yes there was a “feature” introduced around version 1.6.5 whereby the functions have to be in a specific order. They quickly realised this was a backward step and it was fixed in later versions.

It seems endless story … :frowning:
I updated Java and now also Arduino IDE 1.6.9 …
They are not errors in the compilation … but when I press the button Wemos will disconnect …
My button is connected to GND and pin D4 (2 Arduino IDE)
I tried with two Wemos boards with the same result

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "*************************";

SimpleTimer timer;
//ESP8266 wifi(EspSerial);   <-------- commented

void setup()
{
    Serial.begin(9600);
    Blynk.begin(auth, "TP-LINK", "*************");
     while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Notify immediately on startup
  Blynk.notify("Device started");

  // Setup a function to be called every minute
  // timer.setInterval(60000L, notifyUptime);    <--------commented??

  // Setup notification button on pin 2
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, RISING);
}

void notifyUptime()
{
  long uptime = millis() / 60000L;

  Blynk.notify(String("Running for ") + uptime + " minutes.");
}

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(2);
  if (isButtonPressed) {
    Serial.println("Button is pressed.");

    Blynk.notify("Yaaay... button is pressed!");
  }
}

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

Try another pin.some pins doesn’t support interrupt.

@n3v3r all the digital pins on the WeMos D1 and D1 Mini have full functionality (interrupt/pwm/I2C/one-wire support) with the exception of D0 (GPIO 16).

That said GPIO 0, 2, 16 (D3, D4 and D0) should only be used if you have no other pins available as it can mess up the flashing of your device (see below).

Are you using the D1 R2, Mini or the original D1?

Below is a “working” sketch running on my Mini with a button between ground and D4 (GPIO 2). You don’t need to do the digitalRead(2) because the attachInterrupt does this for you. Once you have it working you need to make the following changes:

  1. Select another pin as GPIO2 will create problems for you.
  2. Debounce the button.
  3. Disable interrupt whilst you are running the notifyOnButtonPress() function and enable when it is finished. Failure to do this will certainly result in your device rebooting or worse, locking up.

Google has all the stuff you need.

// n3v3rButton.ino 24/8/16 Costas

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxx";
SimpleTimer timer;
void setup()
{
    Serial.begin(115200);
    Blynk.begin(auth, "xxxx", "xxxxx");
     while (Blynk.connect() == false) {
    // Wait until connected
  }
  // Notify immediately on startup
  Blynk.notify("Device started");
  // Setup a function to be called every minute
  // timer.setInterval(60000L, notifyUptime);    <--------commented??
  // Setup notification button on pin 2
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, RISING);
}
void notifyUptime()
{
  long uptime = millis() / 60000L;
  Blynk.notify(String("Running for ") + uptime + " minutes.");
}
void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  //int isButtonPressed = !digitalRead(2);
  //if (isButtonPressed) {
    Serial.println("Button is pressed.");
    Blynk.notify("Button is pressed!");
  //}
}
void loop()
{
  Blynk.run();
  timer.run();
}

To top this off, here is a graphical view of what @Costas mentions :slight_smile:

Ow and yes, here is an article about debouncing using a little cap:

It works really well and saves you a lot of hassle in software. It only adds a little capacitor :slight_smile:

1 Like

@Costas I tried to load the code that you suggested, trying to even change the pin from D4 to D1 to D5 etc. … if I open the Monitor Serial I see that the button is pressed, but at the same time Wemos D1 mini resets with this code :

[10014] Connected to WiFi
[10014] IP: 192.168.1.105
[10014] Blynk v0.3.8 on NodeMCU
[10014] Connecting to blynk-cloud.com:8442
[10205] Ready (Table: 1ms).
Button is pressed.

ets Jan 8 2013, rst causes: 4, boot mode: (1.7)

wdt reset

I do not think a debounce problem…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "*********************";
SimpleTimer timer;
void setup()
{
    Serial.begin(115200);
    Blynk.begin(auth, "TP-LINK", "***********");
     while (Blynk.connect() == false) {
    // Wait until connected
  }
  // Notify immediately on startup
  Blynk.notify("Device started");
  // Setup a function to be called every minute
  // timer.setInterval(60000L, notifyUptime);    <--------commented??
  // Setup notification button on pin 2
  pinMode(14, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(14), notifyOnButtonPress, RISING);
}
void notifyUptime()
{
  long uptime = millis() / 60000L;
  Blynk.notify(String("Running for ") + uptime + " minutes.");
}
void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(14);
  delay (100);
  //if (isButtonPressed) {
    Serial.println("Button is pressed.");
    Blynk.notify("Button is pressed!");
  //}
}
void loop()
{
  Blynk.run();
  timer.run();
}

any idea??

It will reset because you haven’t disabled the interrupt whilst you do the processing. It is just to confirm that you can see the button is pressed.

If you comment out Blynk.notify(“Button is pressed!”) it should run for longer without crashing. Please confirm when you have tested this. It can handle a quick Serial.print but not a big long Blynk.notify process.

but in this case, how does the notification to work?

on my Phone app?

sorry for my limited experience…
ty

Just comment out the notify line and retest. Confirm it doesn’t crash.

I confirm, remain connected

Ok now you understand that interrupts are very fussy. The procedure with interrupts is that when it is triggered is, disable the interrupt (to stop it triggering again), set a flag, enable the interrupt and a timer or loop() looks for the flag.

If you do it any other way your ESP will crash.

So add these global variables:

long prevZeroMillis = 0; // used to debounce button, set at 100 millis, 0.1s, debounce time
bool buttonChanged = false; // used to ensure button press is handled correctly
bool buttonStatus = false;  // toggle on and off

Add a new function:

void BlynkButton(){ //lines below will crash the ESP in notifyOnButtonPress() function 
  if(buttonChanged){  // only do something if button state has changed
    Serial.println("Button is pressed.");
    Blynk.notify("Button is pressed!");
    buttonChanged = false; 
  }      
}

Change the notifyOnButtonPress function to:

void notifyOnButtonPress()  // debounced, disabled interrupt and enabled interrupt
{
    noInterrupts();  // disable interrupts
    if(millis() - prevZeroMillis > 100) { // different buttons need different debounce period, test it
      buttonStatus = !buttonStatus;
      buttonChanged = true;      // just set a flag for loop() or SimpleTimer to pick up
      prevZeroMillis = millis();
    }
    interrupts(); // enable interrupts   
}

In loop() add (can also be done with SimpleTimer):

  if(buttonChanged){ 
    BlynkButton();    
  }

Test it, confirm it works ok and then publish the code for others to use.
If we just gave you the full code at the start you wouldn’t learn anything.

in this case the board is disconnected after about 20 seconds …

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

long prevZeroMillis = 0;    // used to debounce button, set at 100 millis, 0.1s, debounce time
bool buttonChanged = false; // used to ensure button press is handled correctly
bool buttonStatus = false;  // toggle on and off

char auth[] = "************";
SimpleTimer timer;

void setup()
{
    Serial.begin(115200);
    Blynk.begin(auth, "TP-LINK", "********");
     while (Blynk.connect() == false) {
    // Wait until connected
  }
    Blynk.notify("Device started");
    pinMode(14, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(14), notifyOnButtonPress, RISING);
}

void BlynkButton(){                    //lines below will crash the ESP in notifyOnButtonPress() function 
  if(buttonChanged){                   // only do something if button state has changed
    Serial.println("Button is pressed.");
    Blynk.notify("Button is pressed!");
    buttonChanged = false; 
  }      
}

void notifyUptime()
{
  long uptime = millis() / 60000L;
  Blynk.notify(String("Running for ") + uptime + " minutes.");
}

void notifyOnButtonPress()                // debounced, disabled interrupt and enabled interrupt
{
    noInterrupts();                       // disable interrupts
    if(millis() - prevZeroMillis > 100) { // different buttons need different debounce period, test it
      buttonStatus = !buttonStatus;
      buttonChanged = true;               // just set a flag for loop() or SimpleTimer to pick up
      prevZeroMillis = millis();
    }
    interrupts();                         // enable interrupts   
}

void loop()
{
  Blynk.run();
  timer.run ();
  if(buttonChanged){ 
      BlynkButton();    
  }
}

changing the time debounce … nothing changes
Now I do not see even the button state in the serial monitor

:sob:

[6265] Connected to WiFi
[6265] IP: 192.168.10.248
[6265] Blynk v0.3.8 on Arduino
[6266] Connecting to blynk-cloud.com:8442
[6665] Ready (ping: 5ms).
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.
Button is pressed.

Working fine here, you have bad wiring.
I am also getting the PUSH messages but remember it is a maximum of 1 per minute so don’t press the button too frequently.