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

Mã không hoạt động cho các nút bấm vật lý khi mất kết nối với internet

#1

Mã code của tôi với 2 bút bấm vật lý:

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

    char auth[] = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;

    const int relayPin1 = 6;
    const int relayPin2 = 7;

    const int btnPin1 = 8;
    const int btnPin2 = 9;

    SimpleTimer timer;
    void checkPhysicalButton1();
    void checkPhysicalButton2();

    int relayState1 = LOW;
    int relayState2 = LOW;

    int btnState1 = HIGH;
    int btnState2 = HIGH;

    BLYNK_CONNECTED() {
    Blynk.syncVirtual(V0);
    Blynk.syncVirtual(V1);
    }
    BLYNK_WRITE(V0) {
    relayState1 = param.asInt();
    digitalWrite(relayPin1, relayState1);
    }
    BLYNK_WRITE(V1) {
    relayState2 = param.asInt();
    digitalWrite(relayPin2, relayState2);
    }

    void checkPhysicalButton1()
    {
    if (digitalRead(btnPin1) == LOW) {
    if (btnState1 != LOW) {
    relayState1 = !relayState1;
    digitalWrite(relayPin1, relayState1);
    Blynk.virtualWrite(V0, relayState1);
    }
    btnState1 = LOW;
    } else {
    btnState1 = HIGH;
    }
    }
    void checkPhysicalButton2()
    {
    if (digitalRead(btnPin2) == LOW) {
    if (btnState2 != LOW) {
    relayState2 = !relayState2;
    digitalWrite(relayPin2, relayState2);
    Blynk.virtualWrite(V1, relayState2);
    }
    btnState2 = LOW;
    } else {
    btnState2 = HIGH;
    }
    }

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

    pinMode(relayPin1, OUTPUT);
    pinMode(btnPin1, INPUT_PULLUP);
    digitalWrite(relayPin1, relayState1);
    timer.setInterval(100L, checkPhysicalButton1);

    pinMode(relayPin2, OUTPUT);
    pinMode(btnPin2, INPUT_PULLUP);
    digitalWrite(relayPin2, relayState2);
    timer.setInterval(100L, checkPhysicalButton2);
    }

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

How to arduino mega can work without internet
#2

Blynk.begin() is a blocking command, no server connection = no further processing.

However, using the alternative Blynk.config() may not work due to your use of Ethernet instead of WiFi…

Also, you are trying to run both timers at the exact same time… stagger them a bit…

timer.setInterval(100L, checkPhysicalButton1);
timer.setInterval(135L, checkPhysicalButton2);

PS, See what properly formatting your code does… allows others to easily read your code and not ignore the post for a month until you edited it (actually it was @noi :+1: ) Although the proper way to format code is like this…

Blynk - FTFC

1 Like

#3

if blynk.config () not working with w5100, what is code replace for blink.config

0 Likes

#4

Blynk.begin() thus if the other option doesn’t work, then either stick with what you have or consider switching to WiFi based ESP

0 Likes

#5

If using the blynk.begin () function, the first time you boot without internet, the system is locked. The commands behind it still do not execute! Is there a way to fix it and skip blynk.begin () when there is no internet for the first time. Thank you very much! The blynk.config () function I use for Arduino + w5100 is very good. But with the Arduno Mega 2560 + w5100 again encountered this problem! They are offline when push button on wedget!

0 Likes

#7

http://docs.blynk.cc/#blynk-firmware-configuration-blynkconfig

The idea of Blynk.config() is that you first establish your own network connection, then this command will establish the Blynk server link over that network connection.

Google for Ethernet connection examples for your adapter and see if any of those commands work prior to running Blynk.config()

0 Likes

#8

My code

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

#define W5100_CS  10
#define SDCARD_CS 4

const int relayPin1 = 22;
const int relayPin2 = 24;
const int relayPin3 = 26;
const int relayPin4 = 28;
const int relayPin5 = 30;
const int relayPin6 = 32;
const int relayPin7 = 34;
const int relayPin8 = 36;

const int btnPin1 = 23;
const int btnPin2 = 25;
const int btnPin3 = 27;
const int btnPin4 = 29;
const int btnPin5 = 31;
const int btnPin6 = 33;
const int btnPin7 = 35;
const int btnPin8 = 37;

SimpleTimer timer;
SimpleTimer timer_cnn;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();
void checkPhysicalButton4();
void checkPhysicalButton5();
void checkPhysicalButton6();
void checkPhysicalButton7();
void checkPhysicalButton8();

int relayState1 = LOW;
int relayState2 = LOW;
int relayState3 = LOW;
int relayState4 = LOW;
int relayState5 = LOW;
int relayState6 = LOW;
int relayState7 = LOW;
int relayState8 = LOW;


int btnState1 = HIGH;
int btnState2 = HIGH;
int btnState3 = HIGH;
int btnState4 = HIGH;
int btnState5 = HIGH;
int btnState6 = HIGH;
int btnState7 = HIGH;
int btnState8 = HIGH;

BLYNK_CONNECTED() {
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_WRITE(V0) {
  relayState1 = param.asInt();
  digitalWrite(relayPin1, relayState1);
}
BLYNK_WRITE(V1) {
  relayState2 = param.asInt();
  digitalWrite(relayPin2, relayState2);
}
BLYNK_WRITE(V2) {
  relayState3 = param.asInt();
  digitalWrite(relayPin3, relayState3);
}

BLYNK_WRITE(V3) {
  relayState4 = param.asInt();
  digitalWrite(relayPin4, relayState4);
}
BLYNK_WRITE(V4) {
  relayState5 = param.asInt();
  digitalWrite(relayPin5, relayState5);
}
BLYNK_WRITE(V5) {
  relayState6 = param.asInt();
  digitalWrite(relayPin6, relayState6);
}
BLYNK_WRITE(V6) {
  relayState7 = param.asInt();
  digitalWrite(relayPin7, relayState7);
}

BLYNK_WRITE(V7) {
  relayState8 = param.asInt();
  digitalWrite(relayPin8, relayState8);
}

void checkPhysicalButton1()
{ 
  if (digitalRead(btnPin1) == LOW) {
    if (btnState1 != LOW) {
     relayState1 = !relayState1;
      digitalWrite(relayPin1, relayState1);
      Blynk.virtualWrite(V0, relayState1);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}
void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW) {
    if (btnState2 != LOW) {
      relayState2 = !relayState2;
      digitalWrite(relayPin2, relayState2);
      Blynk.virtualWrite(V1, relayState2);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
}
void checkPhysicalButton3()
{
  if (digitalRead(btnPin3) == LOW) {
    if (btnState3 != LOW) {
      relayState3 = !relayState3;
      digitalWrite(relayPin3, relayState3);
      Blynk.virtualWrite(V2, relayState3);
    }
    btnState3 = LOW;
  } else {
    btnState3 = HIGH;
  }
}
  void checkPhysicalButton4()
  {
    if (digitalRead(btnPin4) == LOW) {
      if (btnState4 != LOW) {
       relayState4 = !relayState4;
        digitalWrite(relayPin4, relayState4);
        Blynk.virtualWrite(V3, relayState4);
      }
      btnState4 = LOW;
    } else {
      btnState4 = HIGH;
    }
  }

 void checkPhysicalButton5()
{
  if (digitalRead(btnPin5) == LOW) {
    if (btnState5 != LOW) {
     relayState5 = !relayState5;
      digitalWrite(relayPin5, relayState5);
      Blynk.virtualWrite(V4, relayState5);
    }
    btnState5 = LOW;
  } else {
    btnState5 = HIGH;
  }
}

 void checkPhysicalButton6()
{
  if (digitalRead(btnPin6) == LOW) {
    if (btnState6 != LOW) {
     relayState6 = !relayState6;
      digitalWrite(relayPin6, relayState6);
      Blynk.virtualWrite(V5, relayState6);
    }
    btnState6 = LOW;
  } else {
    btnState6 = HIGH;
  }
}

 void checkPhysicalButton7()
{
  if (digitalRead(btnPin7) == LOW) {
    if (btnState7 != LOW) {
     relayState7 = !relayState7;
      digitalWrite(relayPin7, relayState7);
      Blynk.virtualWrite(V6, relayState7);
    }
    btnState7 = LOW;
  } else {
    btnState7 = HIGH;
  }
}

 void checkPhysicalButton8()
{
  if (digitalRead(btnPin8) == LOW) {
    if (btnState8 != LOW) {
     relayState8 = !relayState8;
      digitalWrite(relayPin8, relayState8);
      Blynk.virtualWrite(V7, relayState8);
    }
    btnState8 = LOW;
  } else {
    btnState8 = HIGH;
  }
}

void reconnect(){
  if(!Blynk.connected()){
    Ethernet.begin(mac);
    Blynk.connect(1000);
  }
}
void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);
  
  Serial.begin(9600);
  //Blynk.begin(auth);

     Blynk.config(auth);
     Blynk.connect();

  timer_cnn.setInterval(30000L, reconnect); 
  
  int mytimeout = millis() / 1000;
    while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 8)
      {  // try for less than 9 seconds
        break;
      }
    }

  pinMode(relayPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(relayPin1, relayState1);
  timer.setInterval(100L, checkPhysicalButton1);

  pinMode(relayPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(relayPin2, relayState2);
  timer.setInterval(100L, checkPhysicalButton2);

  pinMode(relayPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(relayPin3, relayState3);
  timer.setInterval(100L, checkPhysicalButton3);

  pinMode(relayPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(relayPin4, relayState4);
  timer.setInterval(100L, checkPhysicalButton4);

  pinMode(relayPin5, OUTPUT);
  pinMode(btnPin5, INPUT_PULLUP);
  digitalWrite(relayPin5, relayState5);
  timer.setInterval(100L, checkPhysicalButton5);

  pinMode(relayPin6, OUTPUT);
  pinMode(btnPin6, INPUT_PULLUP);
  digitalWrite(relayPin6, relayState6);
  timer.setInterval(100L, checkPhysicalButton6);

  pinMode(relayPin7, OUTPUT);
  pinMode(btnPin7, INPUT_PULLUP);
  digitalWrite(relayPin7, relayState7);
  timer.setInterval(100L, checkPhysicalButton7);

  pinMode(relayPin8, OUTPUT);
  pinMode(btnPin8, INPUT_PULLUP);
  digitalWrite(relayPin8, relayState8);
  timer.setInterval(100L, checkPhysicalButton8);
}

void loop()
{
  timer.run(); 
  if(Blynk.connected()) { Blynk.run(); }
}
0 Likes

#9

You are trying to run eight different functions, all at the same time, every 100ms… much better to stagger the timers.

0 Likes

#10

I tried it but it did not work. Arduino seems offline after hitting the button three times

0 Likes

#11
void reconnect(){
  if(!Blynk.connected()){
    Ethernet.begin(mac);
    Blynk.connect(1000);
  }
}
void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);
  
  Serial.begin(9600);
  //Blynk.begin(auth);

     Blynk.config(auth);
     Blynk.connect();

  timer_cnn.setInterval(30000L, reconnect); 
  
  int mytimeout = millis() / 1000;
    while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 8)
      {  // try for less than 9 seconds
        break;
      }
    }

OK?

0 Likes

#12

I didn’t say the simultaneous timers were your issue… but they ARE an issue that can contribute to disconnectio problems.

OK what? Sorry, but I am not really following your issue or code changes, been busy working on my own projects, I just saw something that needed pointing out (the timers)…

0 Likes

#13

I used this code, however, my Arduino Mega + W5100 is offline when I hit a few times. If you replace `blynk.config ()` with `blynk.begin (), that's fine. However, if you use blynk.begin () `` the first time you boot without the internet, the circuit will be locked, the code behind can not work! Please help me!

0 Likes

#14

@minhhoxp You already have your own topic about your issue… no need to jump into other topics… particularly older ones.

I am moving your post back into your active topic. The OP of that old topic will see this link… assuming they ever return.

0 Likes

#15

Sorry!

0 Likes

#16

@minhhoxp: làm được chưa bạn? Theo tôi sao bạn không dùng chung 1 timer để kiểm tra trạng thái và xử lý cho tất cả các relay có chức năng tương tự.

So have done the mission yet? I wonder why you have not combined all activities into 1 timer where all those relays have the same function?

0 Likes