BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

The physical button without wifi does not work

• NodeMcu ESP8266 V3 Lua CH340 Wifi Dev. Board
• android p
• Blynk server
• Blynk Library version 0.6.1


#define BLYNK_PRINT Serial
#include "FastLED.h"
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define LED_PIN 3
#define NUM_LEDS 3 // Number of LEDs connected.
#define BRIGHTNESS 64 // Default LED brightness.
#define LED_TYPE WS2812B
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];



char auth[] = "";
char ssid[] = "";
char pass[] = "";



BlynkTimer timer;
void checkPhysicalButton();

//---variables for our RGB values (0-1023) ---
int r = 0;
int g = 0;
int b = 0;

//---variables for On/Off, and Auto/Manual toggle

int autoMode = 0;

//--- int/byte for automatic hue incrementation---
uint8_t gHue = 0; // rotating "base color" used by many of the patterns

bool rele;

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
  Blynk.syncVirtual(V7);
  Blynk.syncVirtual(V8);
  }

// Button 1
BLYNK_WRITE(V0) {
  rele = param.asInt();
  digitalWrite(D1, rele);
}


// Button 2
BLYNK_WRITE(V1) {
  rele = param.asInt();
  digitalWrite(D4, rele);
}

// Button 3
BLYNK_WRITE(V2){
  rele = param.asInt();
  digitalWrite(D5, rele);
}

// Button 4
BLYNK_WRITE(V3)
{
  rele = param.asInt();
  digitalWrite(D8, rele);
}



//--- Toggle Auto/Manual Mode ---
BLYNK_WRITE(V4) {
  autoMode = param.asInt();
}



//--- Red slider value---
BLYNK_WRITE(V6)
{
  r = param.asInt();
}


BLYNK_WRITE(V7) {
  g = param.asInt();
}

//--- Blue slider value---
BLYNK_WRITE(V8) {
  b = param.asInt(); 
}

void checkPhysicalButton()
{
 if (digitalRead(D7))
  {
    rele = !rele;
    digitalWrite(D8, rele);
    Blynk.virtualWrite(V3, rele);
    delay (450);
  }


 if (digitalRead(D0))
  {
    rele = !rele;
    digitalWrite(D4, rele);
    Blynk.virtualWrite(V1, rele);
    delay (450);
  }


  if (digitalRead(D6))
  {
    rele = !rele;
    digitalWrite(D5, rele);
    Blynk.virtualWrite(V2, rele);
    delay (450);
  }

  if (digitalRead(D2))
  {
    rele = !rele;
    digitalWrite(D1, rele);
    Blynk.virtualWrite(V0, rele);
    delay (450);
  }



}



void setup()
{
  // Debug console
  Serial.begin(9600);

  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  FastLED.setBrightness(BRIGHTNESS);
  #define FRAMES_PER_SECOND 120



  

  pinMode(D1, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D8, OUTPUT);
  pinMode(D2, INPUT);
  pinMode(D0, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);

  // Setup a function to be called every 100 ms
 
  timer.setInterval(100L, checkPhysicalButton);

Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

}

void loop()
{
 
if(autoMode == 0) {
    for (int i = 0; i < NUM_LEDS; i++){
      leds[i] = CRGB(r, g, b);
      FastLED.show();
      delay(30);
    }
  }

if(autoMode == 1 ) {
    fill_rainbow( leds, NUM_LEDS, gHue, 7);
    // send the 'leds' array out to the actual LED strip
    FastLED.show(); 
    // insert a delay to keep the framerate modest
    FastLED.delay(1000/FRAMES_PER_SECOND); 
    EVERY_N_MILLISECONDS(20) {
      gHue++; // slowly cycle the "base color" through the rainbow
    } 
  }

  Blynk.run();
  timer.run();
}

Take a look at this:

2 Likes

sir i don’t know how to add this code in my code, please help me,

@Toro_Blanco help me please

void setup()
{

WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  Blynk.config(auth, server, port);
  Blynk.connect();
//your code

}


void loop() {
  timer.run();
  
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

As @harymk is using Blynk.begin in his original void setup then that will need to be changed as well :slightly_frowning_face:

Pete.

1 Like

I just add it in setup :stuck_out_tongue_winking_eye:

1 Like

thanks dear now its working fine.

sir can we change wifi ssid and password with blynk auth

You are welcome.
Why don’t you use interrupt to control physical buttons ?

Yes you can but with HTML
You can find a way to change it on this forum

give me detail

The search function is very handy, as are saying Please and Thank You.
Try searching for “WiFiManager”.

Pete.

1 Like

sir now all function going slowly

What do you mean “now all function going slowly” ?
Pls provide us your new code and serial printed
Don’t forget to format it.