Not found in the documentation, maybe it's time to add it?

Have you tried the sketch without any change ?

Is this your void loop ?

it code from youtobe

Nope. Watch from 11:25ā€¦ it wonā€™t reconnect. I had commented on that video a couple of days back itself, sayings its not the way a device should be workingā€¦

Iā€™m using esp32 with a similar sketch and everything works fine, never tried the nodemcu sketch.

That sketch will be different may beā€¦ the video you are referring to is not good. It is not practical to use it on a daily basis.

1 Like

Guys, itā€™s very simple - everything that should be done regardless of the connection to the cloud can be done on a timer!

#define BLYNK_TEMPLATE_ID             "********"
#define BLYNK_DEVICE_NAME             "RELABOX"
#define BLYNK_FIRMWARE_VERSION        "0.1.283"
#define BLYNK_PRINT Serial
#define LED_CONN 0

#include "BlynkEdgent.h"
#include "Adafruit_MCP23017.h"
#include <Wire.h>

Adafruit_MCP23017 mcpOut;
Adafruit_MCP23017 mcpInp;

int relayPins[8][2] = {{0,1},{2,3},{4,5},{6,7},{8,9},{10,11},{12,13},{14,15}};
bool inpLastState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
bool relayToggled[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
bool btn; 


BLYNK_CONNECTED() {
  Blynk.syncAll();
  Blynk.syncVirtual(V0,V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13);
}

BLYNK_WRITE(V0)
{
  relayChangeState(0, (bool)param.asInt());
}

BLYNK_WRITE(V1)
{
  relayChangeState(1, (bool)param.asInt());
}

BLYNK_WRITE(V2)
{
  relayChangeState(2, (bool)param.asInt());
}

BLYNK_WRITE(V3)
{
  relayChangeState(3, (bool)param.asInt());
}

BLYNK_WRITE(V4)
{
  relayChangeState(4, (bool)param.asInt());
}

BLYNK_WRITE(V5)
{
  relayChangeState(5, (bool)param.asInt());
}

BLYNK_WRITE(V6)
{
  relayChangeState(6, (bool)param.asInt());
}

BLYNK_WRITE(V7)
{
  relayChangeState(7, (bool)param.asInt());
}



void cloudState()
{
  if(Blynk.connected())
    digitalWrite(LED_CONN,1);  
  else
    digitalWrite(LED_CONN,!digitalRead(LED_CONN));  
}



void checkButton(){
  for(int i = 0; i<16;i++){
    btn = !mcpInp.digitalRead(i);
    if(btn && !relayToggled[i]){
      relayToggled[i] = true;
      toggleState(i);
    }else if(!btn)
    relayToggled[i] = false;
  }
}


void setup()
{
  
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  Wire.begin(14, 12);
  
  mcpOut.begin();      // use default address 0
  mcpInp.begin(1);      // use default address 0
  
  for(int i =0; i <16; i++){
    mcpOut.pinMode(i, OUTPUT);
    mcpOut.digitalWrite(i,0);
    mcpInp.pinMode(i, INPUT);
    mcpInp.pullUp(i, HIGH);     // turn on a 100K pullup internally
  }
  
  pinMode(LED_CONN,OUTPUT);
  digitalWrite(LED_CONN,0);
  timer.setInterval(50L, checkButton);
  timer.setInterval(300L, cloudState);
}





void relayChangeState(int i, bool _state)
{
  mcpOut.digitalWrite(relayPins[i][_state],1); 
  delay(40);
  mcpOut.digitalWrite(relayPins[i][_state],0); 

  if(_state)
    inpLastState[i] = 1;
  else
    inpLastState[i] = 0;
}



void toggleState(int i)
{
  bool _state = !inpLastState[i];
    if(Blynk.connected()){
      Blynk.virtualWrite(i, _state); 
      Blynk.syncVirtual(i);
    }else
      relayChangeState(i,_state);
    inpLastState[i] = _state;
}

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

Thank you all for participating!

Dear developers, please make it clear in the application that the device is not connected to the cloud not only on the tiles page, but also inside. Thanks.

Are you sure the problem is solved ? I dont think so !! If the device dont get a wifi connection during reboot !! I think your buttons wont work. And the device will keep rebooting if you have not modified your code not to.

Keep us updated on this.
Thanks

I checked, the buttons workā€¦ however, reconnection is not performed. Is there something you need to do to make the device try to connect again?

The device works very unstable after the wifi signal disappears. It does not always reconnect, after reconnecting it does not respond to changes in widgets in the application, although the LED shows that it is connected and the TimeLine in the application indicates that the device is online. Has anyone encountered this?

Sometimes part of the code is suddenly executed and the relays are triggered with a large delay.

What does the serial monitor show when this happens?

Pete.

Total: in about 3ā€¦The device has been fully functional for 4 minutesā€¦ I donā€™t think it should be like that.

Pete Knight, unfortunately my device doesnā€™t have a serial port, but I can run this sketch on nodemcu for example and see whatā€™s going on there, do it?

PeteKnight, I ran a copy of this device on NodeMCU and received data from the port, but as it turned out there were no more delays in code execution, I sin on the router, perhaps it delayed traffic after the first shutdown of the 2.4GHz network segment.

Without serial output youā€™ll always be guessing about whatā€™s happening, so yes, you should do it.

Pete.

Pete Knight, many tasks were solved, but some remained so that they would not be lost in the correspondence, I give them here as a list

  1. In case of an error in the ID or password, you need to reset the device to retry connecting to WiFi.

  2. In case of Wi-Fi loss, the device does not connect again. I tested this case at home by disconnecting the 2.4GHz network segment in the router - in this case, the device reconnected, but when I turned off and turned on the power of the router again, the device did not connect.

I hope for your assistance and will wait for corrections while I make a timer to restart ESP

You seem to be saying that if the device canā€™t connect to the WiFi because either the SSID or password are wrong then the device should clear the save credentials and go back in to provisioning mode. Is that correct?
If it is then I think you have to look at this from the other point of view. If the Wifi is down, lets say because you power-down your router for some reason, then the device wonā€™t be able to connect to it. If it then cleared the credentials and required you to re-provision it then every Blynk user would be complaining about this behaviour - you included.
Iā€™m not aware of any way to differentiate between a failed connection caused by incorrect credentials and one caused by the specified network being unavailable.
So, as far as I can see, there is no way around the behaviour that you are complaining about.

Iā€™ve already said that Blynk.begin is a blocking function, so any sketch that uses it will stop at that point in the code execution.
As Iā€™ve said before, the solution is to manage your own WiFi connection then use Blynk.config and Blynk.connect as this is a non-blocking function (although there are delays each time Blynk.run or Blynk.connect are called, so you have to configure your sketch in a way that will work for your particular requirements).

Pete.

If I understand correctly, BlynkEdgent takes care of creating and managing connections itself, so your suggestion is tantamount to rejecting such convenient functionality. Is this statement true?

Blynk Edgent allows device provisioning with WiFi credentials, and can be used in conjunction with the web portal and/or the reset button on the device to clear the saved credentials, but it doesnā€™t take care of this all by itself - some user interaction is required.
Your complaint seems to be that you need to press the reset button on the device to put it back into provisioning mode if incorrect credentials are entered. Iā€™m saying that this isnā€™t something that can be avoided in my opinion.

Pete.