Unable to connect my board ESP32 to Hotspot

Hi, I’m working in my project that it’s giving a big headache because for a time i had success to connect my board to hotspot and i was testing the DHT11 sensor, but for a moment the board didn’t connect anymore. When I connect it via my phone’s Hotspot, Blynk shows that it is offline and will not connect and sometimes shoes that is connected and the board is resetting immediately. What is causing this?


#define BLYNK_TEMPLATE_ID "TM********i"
#define BLYNK_TEMPLATE_NAME "Avicultor2023"
#define BLYNK_AUTH_TOKEN "Blk******************"


#define BLYNK_PRINT Serial

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

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,4);

bool fetch_blynk_state = true;  //true or false
char auth[] = BLYNK_AUTH_TOKEN;//Paste auth token you copied

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

#define DHTPIN 32      
#define wifiLed 2


#define DHTTYPE DHT11     // DHT 11


#define VPIN_t      V0
#define VPIN_h      V1

int wifiFlag = 0;

DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = 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(V1, h); // select your virtual pins accordingly
  Blynk.virtualWrite(V0, t); // select your virtual pins accordingly
}

void checkBlynkStatus() { // called every 2 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
   Serial.println("Blynk Not Connected");
   lcd.setCursor(0,0);
   lcd.print("Ilumin:");
   lcd.setCursor(0,1);
   lcd.print("Comprt:");
    lcd.setCursor(0,2);
   lcd.print("Sens:");
    lcd.setCursor(5,3);
     lcd.print("Mod Manual");
    digitalWrite(wifiLed, LOW);
    delay(800);
    digitalWrite(wifiLed, HIGH);
  }
  if (isconnected == true) {
    wifiFlag = 0;
    if (!fetch_blynk_state){
      /*
    Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
    Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
    Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
    Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
    
    Blynk.virtualWrite(VPIN_Wsensor_1, toggleStW1_1);
    Blynk.virtualWrite(VPIN_Wsensor_2, toggleStW1_2);
    Blynk.virtualWrite(VPIN_Wsensor_3, toggleStW1_3);
    */
    }
    digitalWrite(wifiLed, HIGH);
    Serial.println("Blynk Connected");
//    sendData();
   // lcd.clear();
     lcd.setCursor(5,3);
     lcd.print("Mod Web On");
  }
}

BLYNK_CONNECTED() {
  // Request the latest state from the server
  if (fetch_blynk_state){
   // Blynk.syncVirtual(VPIN_BUTTON_0);
   // Blynk.syncVirtual(VPIN_BUTTON_1);
    //Blynk.syncVirtual(VPIN_BUTTON_2);
    //Blynk.syncVirtual(VPIN_BUTTON_3);
  }
    //Blynk.syncVirtual(VPIN_IRSENSOR,irVal);
    //Blynk.syncVirtual(VPIN_MQ2, mq2Val );
   // Blynk.syncVirtual(VPIN_LDR, ldrVal);  
}
void setup()
{
  // Debug console
  Serial.begin(115200);
 // lcd.begin();
  //lcd.backlight();
  
    pinMode(wifiLed, OUTPUT);
  digitalWrite(wifiLed, LOW);


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


timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  Blynk.config(auth);
  delay(1000);
   dht.begin();
  if (!fetch_blynk_state){
   // Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
   // Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
    //Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
    //Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
  }
  lcd.setCursor(4, 0);
  lcd.print("Aviario");
  lcd.setCursor(1, 1);
  lcd.print("Automatico ESP32");
  delay(2000);
  lcd.clear();
}


void loop()
{
  Blynk.run();
  timer.run();
} ```

Like this Pete?

@Anderson1 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

Dear Pete, i did the thing that you suggested.

Okay, your code is a bit of a mess, and I assume that it’s not something you’ve written yourself.

I’d suggest that you stop using all of the flags to indicate if WiFi or Blynk is connected, because these are part of the mess within the code.
I’d also remove all of the commented-out code too.

When you connect an ESP32 to WiFi it takes more than this to consistently get a successful connection…

If you do a…

test and it’s false (not connected) then that could be because WiFi isn’t connected. Without WiFi you will never connect to the Blynk server.
So, you need to do two tests - is WiFi connected and is Blynk connected.

The Blynk.config() command can also accept an optional timeout period in millis. Without this, the default timeout is around 18 seconds. You shouldn’t go for a timeout of less than about 2500 millis though, as it takes a couple of seconds to negotiate the Blynk connection.

Also, every time your code encounters a Blynk.run() command it will try to re-connect to Blynk, and if this isn’t possible you’ll encounter the timeout period (18 seconds in your case). To avoid this you need to put Blynk.run() inside a logical Blynk.connected() test (an if statement).

You also need to ensure that your test of WiFi and Blynk connectivity runs far less frequently than your Blynk.config timeout period.

Do some forum searches for Blynk.config() and timeout and you’ll see examples of how to do all of this.

Pete.

Thanks Pete

Hi Anderson1. I had bizzarre Wifi issues off my phone hotspot when working on esp32. I noticed a fleeting message from a text box serial monitor that amongst garbage, "Too many connections to hotspot (5). So, I connected a wireless access point to the back of my imac by lan cable, , shared the Macs Internet’ from its connection to my iphone ten. on Mac, Preferences, Sharing. fixed my issue. lost weeks of work tracking it down,

1 Like