MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
test_wifi.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "WiFi.h"
16 #include "secrets.h"
17 
18 #define CHECK_NWS_TEST 0
19 
20 void initWiFi()
21 {
22  WiFi.mode(WIFI_STA);
23  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
24  Serial.print("Connecting to WiFi ..");
25  while (WiFi.status() != WL_CONNECTED) {
26  Serial.print('.');
27  delay(1000);
28  }
29  Serial.println(WiFi.localIP());
30 }
31 
32 void setup()
33 {
34  Serial.begin(115200);
35 
36  if (CHECK_NWS_TEST) {
37  // Set WiFi to station mode and disconnect from an AP if it was previously connected
38  WiFi.mode(WIFI_STA);
39  WiFi.disconnect();
40  delay(100);
41  } else {
42  initWiFi();
43  }
44 
45  Serial.println("Setup done");
46 }
47 
48 void loop()
49 {
50  if (CHECK_NWS_TEST) {
51  Serial.println("scan start");
52 
53  // WiFi.scanNetworks will return the number of networks found
54  // WiFi.scanNetworks will return the number of networks found
55  int n = WiFi.scanNetworks();
56  Serial.println("scan done");
57  if (n == 0) {
58  Serial.println("no networks found");
59  } else {
60  Serial.print(n);
61  Serial.println(" networks found");
62  for (int i = 0; i < n; ++i) {
63  // Print SSID and RSSI for each network found
64  Serial.print(i + 1);
65  Serial.print(": ");
66  Serial.print(WiFi.SSID(i));
67  Serial.print(" (");
68  Serial.print(WiFi.RSSI(i));
69  Serial.print(")");
70  Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
71  delay(10);
72  }
73  }
74  Serial.println("");
75  } else {
76  Serial.println("Wifi connected");
77  }
78 
79  // Wait a bit before scanning again
80  delay(5000);
81 }
#define CHECK_NWS_TEST
Definition: test_wifi.cpp:18
void setup()
Definition: test_wifi.cpp:32
void initWiFi()
Definition: test_wifi.cpp:20
void loop()
Definition: test_wifi.cpp:48