shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

How can i run code after a WiFi router with no internet access #122

Closed copinthearea closed 3 weeks ago

copinthearea commented 4 weeks ago

Good Timing

I am trying to run a code to see if the wifi is connected to the internet, but when i run this line of code:

myBot.wifiConnect(ssid, pass); <-- I put this on void Setup(){}

It seems that this line of code is in a loop, if my router is without internet access!!

But if it is with internet it works fine.

So the point is, How to run a code once in the setup, to see if my wifi is working with internet, and how to restart the ESP8266 if it is not.

I tried everything ChatGPT, but no success!

After this command: myBot.wifiConnect(ssid, pass); it does not run the code line bellow it!

Also, i tried this too:

` bool wifiConnected = myBot.wifiConnect(ssid, pass);

// Print the result if (wifiConnected) { Serial.println("WiFi connected successfully!"); } else { Serial.println("WiFi connection failed!"); }`

Screenshot (30)

copinthearea commented 4 weeks ago

I want to run a code even if it is not connected to the internet.

I want to know if my wifi is working, if it is not, restart the ESP8266 Board!

shurillu commented 4 weeks ago

Hello copinthearea,

all the WiFi methods are "helper function". In other words, you can use your WiFi connection methods to establish a connection to the router. Anyway the method wifiConnect() by default wait until a connection is established as you have seen before. If you want to break the loop after a while, you can set the number of tries by using the method setMaxConnectionRetries(uint8_t retries). Here an example (snippet) of code:

<your code here>
...
   myBot.setMaxConnectionRetries(10);
   if (!myBot.wifiConnect(mySSID, myPassword)) {
      Serial.println("WiFi connection NOT established.");
   } else {
      Serial.prinln("Connection established!");
   }
...

Here you can find some info about all the helper functions.

Hope this help, regards.

Stefano

copinthearea commented 4 weeks ago

@shurillu Thank you very much! God bless you!

It worked!

shurillu commented 3 weeks ago

Good! I'll close the issue.