mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-26 07:02:15 +03:00
Many people have problems with using ESP8266 as WiFi-client due to none of the examples mentioning that you should use WIFI_STA if you only want the ESP8266 to act as a WiFi-client. Many WiFi-devices will randomly try to connect to the ESP8266 if used as STA+AP and complain about not being able to access the Internet or other devices on the network.
53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
/*
|
|
* This sketch shows the WiFi event usage
|
|
*
|
|
*/
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
const char* ssid = "your-ssid";
|
|
const char* password = "your-password";
|
|
|
|
|
|
void WiFiEvent(WiFiEvent_t event) {
|
|
Serial.printf("[WiFi-event] event: %d\n", event);
|
|
|
|
switch(event) {
|
|
case WIFI_EVENT_STAMODE_GOT_IP:
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
break;
|
|
case WIFI_EVENT_STAMODE_DISCONNECTED:
|
|
Serial.println("WiFi lost connection");
|
|
break;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// delete old config
|
|
WiFi.disconnect(true);
|
|
|
|
delay(1000);
|
|
|
|
WiFi.onEvent(WiFiEvent);
|
|
|
|
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
|
|
would try to act as both a client and an access-point and could cause
|
|
network-issues with your other WiFi-devices on your WiFi-network. */
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.println("Wait for WiFi... ");
|
|
}
|
|
|
|
|
|
void loop() {
|
|
delay(1000);
|
|
}
|
|
|