ESP32 With Two Wifi

Thank you. Very good forum, helped a lot of people, including me. I tried to present my difficulty in programming. It's like a child learning to speak, he will need to follow adults and then grow up to understand what he is saying. Programming too, the difficulty I'm facing, I don't know how to solve. I want on the forum, can get everyone's help. But maybe things are going too far from what I want. But I like to even people, when in trouble, they want to help. No one has the conditions, wisdom, and strength, but has to raise his hand to beg. Thank

It's a subject that has to be learned incrementally, like math... Counting > Arithmetic > Algebra > Linear Algebra > Calculus.

You can't just leap from Counting to Calculus.

And, to the point, you can't teach Calculus to someone that doesn't know Algebra...

I completely understand, I know that everything needs to be cultivated and accumulated knowledge. The purpose I bring to the forum, is to get input and comment on the mistakes I'm making. But everyone is misunderstanding what I mean. I don't want a cake that's ready-to-eat without having to put in the effort. What I want is a cake mold for me to have a chance to follow. Does anyone here admit that going to school has never plagiarized? Never seen a copy of a sample document?

The code you posted in post #1 looks good so far.
For analysing more detail information is needed from you.
If you upload the code to your ESP32 is the upload successful?
What do you see in the compile/upload-log window if you have clicked on the uploading button?

What does the serial monitor show if you run the code like posted in post # 1?

The above code is that I wrote a demo way. It loaded successfully and the connection to wifi ssid 1 seems ok. Thus it is capturing the internet router to use as WIFI STA mode. But after I try to turn off wifi ssid "Con co be be" and turn it back on, esp cannot automatically reconnect to wifi ssid 1 but it keeps hanging on WIFI AP mode with ssid "ESP8266_AP" . I have to hang it for a long time, from 5 - 10p, it can switch to WIFI STA with ssid "Con co be be" . Sometimes it can't connect even if I leave it for more than 5-10 minutes. It was an error that I understood because I was writing code indiscriminately without following the rules of programming.

Now that is exactly that kind of information that was missing as you started your thread.
The ESP32 can be configured in different ways which WiFi to connect and which not.
You can configure the ESP32 to keep a certain SSID stored on non-volatuile memory etc.

more serial printing will help to understand what is going on
one example how to proceed further
I linked to this tutorial
which has a table about the conncection-status


So one example is that you try to code how to print the connection-status
and even if this code does not compile to post this code-version and ask how it has to be corrected to make it work.

And this basic principle can be used all the time

Another thing is to ask "can somebody point to a link where there is information how to make use of this WiFi.status()-function

best regards Stefan

Here is a function than scans for WiFis and prints the SSID and RSSI to the serial monitor

void ScanWiFis() {
  Serial.println("Startscanning for networks");
  int NoOfWiFis = WiFi.scanNetworks();

  Serial.println("scanning done List of SSIDs");
  Serial.print("found networks ");
  Serial.println(NoOfWiFis);

  for (int WiFiIdx = 0; WiFiIdx < NoOfWiFis; WiFiIdx++) {
    // Print SSID and RSSI for each network found
    Serial.print(WiFiIdx);
    Serial.print(" #");
    Serial.print(( WiFi.SSID(WiFiIdx) ).c_str() );
    Serial.print(" RSSI");
    Serial.print(WiFi.RSSI(WiFiIdx) );
  }
}

best regards Stefan

Thank you for your sharing. But I'll probably give up on programming. I need more practice time. I have an idea but I don't know where to start. Thank you

start here
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Thank you. The above model is my first problem to learn arduino, or esp. I have been dying with this first problem. I'm really stupid

I don't understand what you mean by writing "the above model" Which model what kind of model?
What is the "first problem"?

What do you mean with "I have been dying with the first problem?"

You are still alive otherwise you could not post.

The first problem I was assigned when learning to program was: flexible switching between wifi modes (sta and ap). Default will be sta and after losing connection, esp will run ap. After wifi is back, esp will continue to run in sta mode and stop ap. This is my first math assignment but I'm dead in my programming language. I couldn't do a first math problem like that. I won't be able to continue to the 2nd problem. I'm really dead in programming

programming is programming. Not much math.
It seems that english is not your native language.
You use the word "death" in a strange way.

I do not understand what you want to say.

I recommend that you write your postings in your native language
and then let google translate the translation.
This works very good.
I have found that many users do not want to use google translate. I do not understand why. using google translation will significantly improve communication

That's right, English is not my mother tongue. There are some sentences that I translated from the local language, so it will be difficult for outsiders to understand. I don't mean math in programming. It is solving a problem posed in programming. Here, my problem is to solve the problem of switching between wifi modes (STA and Ap). I couldn't do it myself. It's out of reach for someone with no programming knowledge like me. I know it's hard for me to post on the forum waiting for something to become available. You have to think for yourself, learn on your own, and modify it. I know that I am not good enough, do not understand the programming language enough, so "dead" here I mean that I am no longer capable. Like a person who dies without being able to do anything else.

I appreciate the interest from you and the community. You and the community are helping me a lot in learning and getting used to programming. Help me correct the mistake and find a way to fix it. Can I ask for your information such as fb, gmail, tele... or something so that it can be easily exchanged later? I would love to learn many things from you.

I highly dout this.
I have even done translating from german to english then from english to arabic and then back from arabic to german and the result was pretty good.

I will answer questions here in the forum. The reason is that the whole community not only you shall be able to learn from the communication and the codes posted online

If you fall back into generalised questions I will quit answering.

If you go on posting code with your own modifications and then asking specific questions I will answer your questions.

If you fall back into generalised questions I will mute this thread and you as user.

So as a last appeasement

start writing code and post your selfwritten code !

If, within some period, you cannot connect to a router then you can go on and try for the other router (just change 'credentials')?

Yes I understand, so how do I "for a while, you can't connect to the router then you can go ahead and try another router (just change the 'login info') ')?"

Thanks you.
I will return

Rudimentary (no End of the World contingencies addressed) --

void setup()
{
  WiFi.begin(ssid, password);
  delay(1000);
  
  Serial.begin(115200);
  Serial.println("Acquiring WiFi 1");//
  byte chances = 0;
  while ((WiFi.status() != WL_CONNECTED ) && (chances < 20))
  {
    delay (500);
    Serial.print ( "." );
    chances ++;
  }
  Serial.println("Failed 1st choice");

  // WiFi.end();    // Not Sure

  WiFi.begin(ssid2, password2);
  delay(1000);
  Serial.println("Acquiring WiFi 2");//
  chances = 0;
  while ((WiFi.status() != WL_CONNECTED ) && (chances < 20))
  {
    delay (500);
    Serial.print ( "." );
    chances ++;
  }
  
  //timeClient.begin();
}

Following Up --
It works (except I was re-declaring the 'chances' variable).
I used "mock credentials" for a non-existent router, which it fails, and then it goes on with life and picks up the live router (with ssid2, password2).

The previous 'works' because the first always fails (it's non-existent).
The following makes provision for the first choice connecting - and in that event it won't look for an alternative:

void setup()
{
  WiFi.begin(ssid, password);
  delay(1000);
  
  Serial.begin(115200);
  Serial.println("Acquiring WiFi 1");//
  byte chances = 0;
  while ((WiFi.status() != WL_CONNECTED ) && (chances < 20))
  {
    delay (500);
    Serial.print ( "." );
    chances ++;
  }
  
  if(WiFi.status() != WL_CONNECTED)
  {
    failedfirst = true;
    Serial.println("Failed 1st choice");
  }
  if(!failedfirst)
  {
    Serial.println("Conn'd first choice");
  }
  
  if (failedfirst)
  {
    chances = 0;
    while ((WiFi.status() != WL_CONNECTED ) && (chances < 20))
    {
      delay (500);
      Serial.print ( "." );
      chances ++;
    }
  }
  timeClient.begin();
}

You aren't using timeClient, and I declared my failedfirst bool before setup().
Illustrates the general idea, yes?

1 Like