ESP32 Stuck at Connecting to ****

Hello I have seen other people having similar issue as me, but I couldn’t implement their solution to my sketch and make it work.

Sometimes when I power up my ESP32 it gets stuck at “Connecting to ****” and I have to press the reset pin and at second try it will connect.

I want to implement a timeout function so if it gets stuck at connecting there will be a timeout after 30 seconds which set digital pin 9 as output, because it causes the ESP32 to reboot and try again.

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "****"
#define BLYNK_TEMPLATE_NAME         "****"
#define BLYNK_AUTH_TOKEN            "****"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#define MaskinNumber V85

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****;
char pass[] = "****";

BlynkTimer timer;

#define trButton 19      // training/reset button (being eliminated/modified)
#define processInput 19  // needs the sensor from the process
#define faultLED 3
#define readyLED 4
#define runningLED 5

bool needCalibration;  // set this to force a calibration run

enum bar { IDLE = 0,
           MEASURE,
           CHECK,
           IFAULT,
           NFAULT,
};

// Declaring a global variabl for sensor data
int sensorVal = 0;

//What to do when successfully connected to Blynk
BLYNK_CONNECTED() {

  //Makes sure that V1 is in sync in case of lost connection.
  Blynk.syncVirtual(V2);

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Connected to Blynk 🙌");

  //A LED lights up when connected to server.
  digitalWrite(21, HIGH);
}

//What to do when not connected to Blynk
BLYNK_DISCONNECTED() {

  //Message that shows up in Serial Monitor.
  BLYNK_LOG("Blynk disconnected");
}


//Virtual pin 1 in datastream
BLYNK_WRITE(V2) {

  // This monitors if the virtual pin value changes, then followed by the action.
  // In this case "if V1 value is ==1 then digitalWrite 26, HIGH else LOW"
  if (param.asInt() == 1) {

    pinMode(9, OUTPUT);

    // execute this code if the switch widget is now ON (Remember to set the on value to 1)
    digitalWrite(26, HIGH);  // Set digital pin 2 HIGH
  } else {
    // execute this code if the switch widget is now OFF(Remember to set the off value to 0)
    digitalWrite(26, LOW);  // Set digital pin 2 LOW
  }
}

//This is for preventing dataoverflow so it only sends the updated value with certain intervals.
//The update interval is defined by timer.setInterval in the void setup section.
void myTimerEvent() {
  // You can send any value at any time.
  // Please don't send more that 10 values per second.

  //send among of seconds that has past to virtual pin V0
 // Blynk.virtualWrite(V0, millis() / 1000);

  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V2
  Blynk.virtualWrite(MaskinNumber, sensorVal);
  Serial.println(sensorVal);
}
void setup()
{

    // Define LED pins
  pinMode(readyLED, OUTPUT);
  pinMode(runningLED, OUTPUT);
  pinMode(faultLED, OUTPUT);
  pinMode(trButton, INPUT_PULLUP);
  pinMode(processInput, INPUT_PULLUP);
  pinMode(26, OUTPUT);
  pinMode(21, OUTPUT);

  needCalibration = true;

  // Debug console
  Serial.begin(115200);
  Serial.println();
  // Give Serial Monitor some time to connect
  delay(3000);

  pinMode(12, OUTPUT);

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


  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

unsigned int counter;
unsigned char state = IDLE;  // does. is.
unsigned long now;
unsigned long timeLimit;
unsigned long timer1;

unsigned long previousMillis = 0; // Gemmer det tidspunkt, hvor LED'en sidst blev opdateret
const long interval = 1000; // Blink interval (i millisekunder)


void loop()
{

  // runs BlynkTimer
  timer.run();

  // Runs all Blynk stuff
  Blynk.run();

 unsigned long currentMillis = millis(); // Hent antallet af millisekunder siden Arduino startede


    now = millis();

  bool controlX = digitalRead(processInput) == HIGH;
  bool buttonState = digitalRead(trButton) == HIGH;

  static bool lastButtonState;
  static bool buttonPress;

  if (buttonState != lastButtonState) {
    if (buttonState) {
      buttonPress = !buttonPress;
    }
    lastButtonState = buttonState;
  }

  switch (state) {
    case IDLE:
      sensorVal = 5;
      digitalWrite(readyLED, HIGH);
      digitalWrite(runningLED, LOW);
      if (controlX) {
        timer1 = now;
        //      if (buttonPress || needCalibration) {
        if (needCalibration) {
          state = MEASURE;
          Serial.print("training for time\n");
          buttonPress = false;
          needCalibration = false;
        } else {
          state = CHECK;
          Serial.print("X up. monitoring time\n");
        }
      }
      break;

    case CHECK:
      sensorVal = 10;
      digitalWrite(readyLED, LOW);
      digitalWrite(runningLED, HIGH);
      if (now - timer1 > timeLimit) {
        state = IFAULT;
        break;
      }
      if (!controlX) {
        Serial.print("X down     made it by ");
        Serial.println(timeLimit - (now - timer1));
        state = IDLE;
      }
      break;

    case MEASURE:
      sensorVal = 10;
      digitalWrite(readyLED, LOW);
      digitalWrite(runningLED, HIGH);
      if (!controlX) {
        timeLimit = now - timer1;
        timeLimit += timeLimit / 10;  // comfort margarine 10 percent here
        Serial.print("X down    new period = ");
        Serial.println(timeLimit);

        state = IDLE;
      }
      break;

    case IFAULT:
      Serial.print(" initialize fault mechanism");
      state = NFAULT;
      Serial.print(" / perpetuate fault mechanism\n");
      Blynk.logEvent("maskinstop");
      sensorVal = 1;
      digitalWrite(faultLED, HIGH);
      buttonPress = false;  // eat any stray button presses
      break;

    case NFAULT:
      digitalWrite(readyLED, LOW);
      digitalWrite(runningLED, LOW);
      digitalWrite(faultLED, millis() & 512 ? HIGH : LOW);

if (currentMillis - previousMillis >= interval) { // Tjek om det er tid til at tænde/slukke LED'en
    previousMillis = currentMillis; // Gem det aktuelle tidspunkt
 //   Serial.println(previousMillis);

      if (buttonPress) {
        Serial.print("sytem to IDLE\n");
        digitalWrite(faultLED, LOW);
        sensorVal = 1;
        buttonPress = false;
        needCalibration = true;
        state = IDLE;
      }
}
      break;
  }
}

Stop using Blynk.begin because it’s a blocking command.

Instead, manage your own WiFi connection then use Blynk.config and Blynk.connect (with an optional timeout).

Search the forum for “Blynk.config()” and you’ll see some examples.

Pete.

Yeah I see it mentioned a couple of times, but is the blynk.begin() the only line of code I should remove from my sketch or is there more to avoid conflicts?

I found this example:


void setup(){
//  Blynk.begin(auth, ssid, pass); don't use this
  WiFi.begin(ssid, pass);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  Blynk.config(auth);
}

How do I make the timeout function?

I know it’s something like:


If (?????) {
digitalPin(9, OUTPUT);
}

Timeout is an option parameter in Blynk.connect().
Do a bit of searching and you’ll find some examples.

Pete.