Search code examples
arduinoarduino-esp8266

WiFi Status 1 on ESP8266


I'm trying to connect my Arduino Uno R3 + ESP8266 to a WiFi connection, and it returned a status of 1 when I printed out WiFi.status(), does anyone now what does it really mean and what's the solution? Here's my ESP8266 code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// WiFi CREDENTIALS
const char *ssid = "xxxx";
const char *password = "xxxx";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.println("Connect to: ");
  Serial.println(ssid);
}
    
void loop(){ 
  delay(5000);
  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.println("Connect to: ");
  Serial.println(ssid);
  Serial.println(WiFi.status());
  Serial.println(WL_CONNECTED);
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    delay(15000);
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("From ESP Connected!");
  }
  else {
    Serial.println("From ESP Not Connected!");
  }
}

=== UPDATE === I've tried using my smartphone's hotspot and it worked on the first try.


Solution

  • I'm not sure myself, but when I start over using the cleaner code:

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    
    // WiFi CREDENTIALS
    const char *ssid = "xxxx";
    const char *password = "xxxx";
    
    void setup() {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      Serial.println("");
      Serial.print("Connecting");
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    
    void loop() {
      if (WiFi.status() == WL_CONNECTED) {
        Serial.println("From ESP Connected!");
      }
      delay(5000);
    }
    

    My gut is telling me that maybe WiFi.mode(WIFI_STA) is causing the error?

    By the way, it worked already, thanks Juraj and cbalakus for the help!