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

My physical button not working if not connected wifi

#1
#define BLYNK_PRINT Serial

#include "Adafruit_NeoPixel.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "cosmote";
char pass[] = "25802580";
char server[] ="192.168.1.10";

// Set your LED and physical button pins here
#define btnPin  D3
#define ledPin  D1
#define btnPin1  D4
#define ledPin1  D2
#define DHTPIN D6 
#define DHTTYPE DHT22 
//#define TRIGGERPIN D5
//#define ECHOPIN    D6
#define PIN            D7 // GPIO pin
 #define NUMPIXELS      8 // Number of pixels in strip
 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = LOW;
int ledState1 = LOW;
int btnState1 = LOW;

DHT dht(DHTPIN, DHTTYPE);


// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V12);
  Blynk.syncVirtual(V13);
  Blynk.syncVirtual(V20);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
  Serial.println(!ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V13) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
  Serial.println(!ledState1);
}
BLYNK_WRITE(V20) // Widget WRITEs to Virtual Pin V10
{   
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
  Serial.println(R);
  Serial.println(G);
  Serial.println(B);
  


for(int i=0;i<NUMPIXELS;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R,G,B)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
}

}
void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    Serial.println(!ledState);
   

      // Update Button Widget
      Blynk.virtualWrite(V12, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
  if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);
    Serial.println(!ledState1);
   

      // Update Button Widget
      Blynk.virtualWrite(V13, ledState1);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}
void sendSensor(){
  float h = (int)dht.readHumidity();
  float t = (int)dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
 
    
   
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V15, h);
  Serial.println(h);
  Blynk.virtualWrite(V16, t);
  Serial.println(t);
}

void setup()
{
  
  Serial.begin(115200);
 
  // 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);
  
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT);
  digitalWrite(ledPin, ledState);
  Serial.println(!ledState);
  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT);
  digitalWrite(ledPin1, ledState1);
  Serial.println(!ledState1);
 // pinMode(TRIGGERPIN, OUTPUT);
 // pinMode(ECHOPIN, INPUT);

  Blynk.begin(auth, ssid, pass, server,8080);
  dht.begin();
  pixels.begin();
 pixels.show();

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

}

void loop()
{

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

#2

Please edit your post to add triple backticks at the beginning and end of your code, so that it displays correctly.
Triple backticks look like this:
```

Once you’ve done that we’ll explain why your physical button isn’t working, and what to do to resolve the problem.

Pete.

2 Likes

#3

Okay, thanks for editing your post.

You’re using Blynk.begin, which is a blocking function. If your device can’t connect to Blynk for whatever reason then the code will never progress beyond that line until a connection to Blynk is established.

The solution is to re-write your code do that it uses Blynk.connect instead. This isn’t a straightforward swap of one command for another, it requires you to set-up the Wi-Fi connection first and use the Blynk.config command to specify the Blynk parameters.

The various firmware connection methods are described here:

If you search the forum for keywords like “Blynk.connect” and “Offline operation” then you’ll find code examples which will give you a good starting point.

Pete.

3 Likes

#4

Thank you very much for your Information !

0 Likes