/* ESP32 WiFi Scanning example */
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
#include "WiFi.h"
#include <TaskScheduler.h>
#define ledPin1 4 // D4 LED connected to digital pin 22 or pin9
#define ledPin2 5 // D5 LED connected to digital pin 5
bool pinState = 1;
bool lastConnState = 0;
int ledPin2Val = 0;
unsigned long uptime = 0;
unsigned long nextMilliFlash = 200;
Scheduler runner;
// Callback methods prototypes
void t1Callback();
void t2Callback();
Task t1(50, TASK_FOREVER, &t1Callback);
//Task t2(5800, TASK_FOREVER, &t2Callback); //5800 appears to be the max delay for the esp32-c3
Task t2(8000, TASK_FOREVER, &t2Callback); //if this is 5900 or above this freezes the esp32-c3 after 1 run. 5850 works for 2 runs then freezes. 5849 works for 4 runs then freezes. 5800 seems pretty stable. The reg esp32 works for all these durations.
void initScheduler()
{
runner.init();
Serial.println("Initialized scheduler");
runner.addTask(t1);
Serial.println("added t1");
runner.addTask(t2);
Serial.println("added t2");
t1.enable();
Serial.println("Enabled t1");
t2.enable();
Serial.println("Enabled t2");
//t2.restartDelayed(); // LED blinking is initiated
}
void setup() {
Serial.begin(115200);
delay(1000); //NEEDED FOR ESP32-C3 without dedicated usb-serial chip
testLED(ledPin1);
testLED(ledPin2);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
initScheduler();
Serial.println("Setup done!");
}
void loop() {
runner.execute();
}
void t1Callback() {
idleLoop();
}
void t2Callback() {
//Serial.print("t2: ");
//Serial.println(millis());
//t2.disable();
scanForNetworks();
//t2.restartDelayed(); // LED blinking is initiated
}
void idleLoop(){
if ((millis() > nextMilliFlash) && (pinState == 1)){
nextMilliFlash = millis() + 100;
//toggle led
if (ledPin2Val > 9){
ledPin2Val = 0;
analogWrite(ledPin2, ledPin2Val);
}
else{
ledPin2Val = 100;
analogWrite(ledPin2, ledPin2Val);
}
}
}
void scanForNetworks(){
uptime = (millis() / 1000);
Serial.printf("Uptime=%lu Scan start.", uptime);
//Serial.print("Scan start. ");
// WiFi.scanNetworks will return the number of networks found.
int n = WiFi.scanNetworks();
Serial.print("Done. ");
bool found = 0;
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
Serial.println("# | SSID | RSSI | CH | Encryption");
for (int i = 0; i < n; ++i) {
if (found == 1){ break;}
if (i > 19){ break;}
// Print SSID and RSSI for each network found
Serial.printf("%2d | ",i + 1);
//Serial.print(" | ");
if (WiFi.SSID(i) == "aaa"){
Serial.printf("**FOUND aaa!! %-32.32s", WiFi.SSID(i).c_str());
found = 1;
}
else{
//analogWrite(ledPin1, 0);
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
}
//Serial.print(" | ");
Serial.printf(" | %4d", WiFi.RSSI(i));
//Serial.print(" | ");
Serial.printf(" | %2d | ", WiFi.channel(i));
//Serial.print(" | ");
switch (WiFi.encryptionType(i))
{
case WIFI_AUTH_OPEN:
Serial.print("open");
break;
case WIFI_AUTH_WEP:
Serial.print("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.print("WPA");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.print("WPA2");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.print("WPA+WPA2");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.print("WPA2-EAP");
break;
case WIFI_AUTH_WPA3_PSK:
Serial.print("WPA3");
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
Serial.print("WPA2+WPA3");
break;
case WIFI_AUTH_WAPI_PSK:
Serial.print("WAPI");
break;
default:
Serial.print("unknown");
}
Serial.println();
//delay(10);
Serial.flush();
}
}
Serial.println("");
if (found == 0)
{
analogWrite(ledPin1, 0);
lastConnState = 0;
}
else{
analogWrite(ledPin1, 5);
lastConnState = 1;
}
// Delete the scan result to free memory for code below.
WiFi.scanDelete();
//Serial.flush();
}
void testLED(int LedToTest){
for (int fadeValue = 0; fadeValue <= 1024; fadeValue += 10) {
// sets the value (range from 0 to 255):
analogWrite(LedToTest, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(10);
}
analogWrite(LedToTest, 0);
delay(30);
analogWrite(LedToTest, 1023);
}