I want to read a network scan from my oled screen instead from the serial monitor. Because some network names are too long for my oled screen I want to display only the first 10 characters of WiFi.SSID()
How can I do that?
Can I make a .substring? Or should I choose another path?
Just a regular example wifi ScanNetworks sketch. How should I change Serial.println(WiFi.SSID(i)); to get only the first 10 characters of the SSID if the name is longer than 10 characters.
I have this script:
#include "ESP8266WiFi.h"
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
}
void loop()
{
Serial.print("Scan start ... ");
int n = WiFi.scanNetworks();
Serial.print(n);
Serial.println(" network(s) found");
for (int i = 0; i < n; i++)
{
Serial.println(WiFi.SSID(i));
}
delay(5000);
}
You probably can. The below test code indicates that an SSID can be captured as a String. In the scan loop you probably could capture the SSID as a String, check the length, and if it is too long, use substring to get the part you want to display, and print that out.
/*
This sketch demonstrates how to scan WiFi networks.
The API is almost the same as with the WiFi Shield library,
the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"
String readString; //String test
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop() {
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
readString = (WiFi.SSID(i)); //String test
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
Serial.println("");
Serial.print("String SSID is "); //String test
Serial.println(readString); //String test
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
I added a substring operation in the test code below and it prints out the first part of the SSID, so it may fit your oled screen.
/*
This sketch demonstrates how to scan WiFi networks.
The API is almost the same as with the WiFi Shield library,
the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"
String readString; //String test
String newString; //String test
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop() {
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
//Serial.print(WiFi.SSID(i));
readString = (WiFi.SSID(i)); //String test
newString = readString.substring(0, 11);
Serial.print(newString);
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
Serial.println("");
Serial.print("String SSID is "); //String test
Serial.println(readString); //String test
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}