Display wifi icon when connected to wifi

Hello, I want the wifi icon to be displayed on the oled when connected to wifi, and when disconnected display "x"

Have any idea. thank you

#include <Wire.h>
#include <Adafruit_GFX.h> 
#include <Adafruit_SSD1306.h> 
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//
#include <WiFiManager.h> 
WiFiManager wm;
const unsigned char wifiicon[] PROGMEM  ={ // wifi icon
  0x00, 0xff, 0x00, 0x7e, 0x00, 0x18,0x00, 0x00
};

void setup() {
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP    
    // put your setup code here, to run once:
    Serial.begin(115200); 
      if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
    //reset settings - wipe credentials for testing
    //wm.resetSettings();
    wm.setConfigPortalBlocking(false);
    wm.setConfigPortalTimeout(10);
    //automatically connect using saved credentials if they exist
    //If connection fails it starts an access point with the specified name
    if(wm.autoConnect("AutoConnec","aa")){
        Serial.println("connected...yeey :)");
  display.clearDisplay();
  display.drawBitmap(120,1,wifiicon,8,8,WHITE);
   display.display();
    }
    else {
        Serial.println("Configportal running");
        display.clearDisplay();
         display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
 display.println("x");
  display.display();
    }
}
int x;
void loop() {
    wm.process();
    x++;
    Serial.println(x);
    delay(1000);
     
    display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);  
 display.println(x);
  display.display();
 
}

WiFi.status() will return the status of the WiFi connection as follows

Value Constant Meaning
0 WL_IDLE_STATUS temporary status assigned when WiFi.begin() is called
1 WL_NO_SSID_AVAIL when no SSID are available
2 WL_SCAN_COMPLETED scan networks is completed
3 WL_CONNECTED when connected to a WiFi network
4 WL_CONNECT_FAILED when the connection fails for all the attempts
5 WL_CONNECTION_LOST when the connection is lost
6 WL_DISCONNECTED when disconnected from a network

You can use the value returned to control what is displayed

1 Like

Thank you for your answer.
But I couldn't figure out how to do. If you tell on above code, I will be grateful :pray:
Thank..

What did you try ?

Try putting this in loop()

if (WiFi.status() == WL_CONNECTED)
{
  //code here to draw the WiFi icon on the display
}
else
{
  //code write "x" to the display
}

There are much better ways to do it but as you have a 1 second delay() in loop() this will do

1 Like

Thank you for this
But What I want to do is:

For example, the program worked. not connected because wifi is not ready. and meanwhile the microcontroller continues to do its main job. Of course, it will show x on the screen because it is not connected to wifi. then it will try to connect to wifi from time to time and show the wifi icon when connected.
Using Wifimanager library

Is that not what my suggested code does ?

The code you suggested works on the first run of the circuit. While the circuit is running, it does not detect the change in wifi status.I will try again today.
i will write results
Thank you very much

What does WiFi.status() return whilst the circuit is running ? If the value remains WL_CONNECTED are you sure that it is not actually still connected ?

Where in the sketch and how frequently are you reading the status ?

1 Like

Yes it was that easy, but I was thinking about the hard one and making it harder.
thank you. :smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.