To jest stara wersja strony!
WiFi na ESP 32
Obsługa WiFi na ESP32.
Deklaracje
#include "Arduino.h" #include <WiFi.h> const String WiFi_SSID = "nazwa_wifi"; //WiFi SSID const String WiFi_password = "haslo_wifi"; //WiFi hasło String WiFi_IP = ""; //WiFi IP uzupełniane po nawiązaniu połączenia WiFiClient wifiClient; void connectWiFi();
Połączenie podczas uruchomienia
void setup() { connectWiFi(); }
Ponowne łączenie w przypadku rozłączenia
void loop() { if (WiFi.status() != WL_CONNECTED) { connectWiFi(); } vTaskDelay(pdMS_TO_TICKS(10)); }
Funkcja
void connectWiFi() { // ustanawianie połączenia WiFi digitalWrite(BUILT_LED, LOW); WiFi.hostname(myHostname); WiFi.mode(WIFI_STA); WiFi.begin(WiFi_SSID, WiFi_password); int attempt = 0; Serial.print("Connecting to WiFi "); while (WiFi.status() != WL_CONNECTED) { if (attempt == 30) { Serial.println("Reboot po 30 probach polaczenia z WiFi"); ESP.restart(); } ++attempt; vTaskDelay(pdMS_TO_TICKS(200)); wl_status_t status = WiFi.status(); if(status != WL_CONNECTED) { digitalWrite(BUILT_LED, HIGH); Serial.print(" Connecting to WiFi error: "); Serial.println(status); vTaskDelay(pdMS_TO_TICKS(100)); digitalWrite(BUILT_LED, LOW); } } WiFi_IP = WiFi.localIP().toString(); Serial.println("Connected to WiFi:"); Serial.println(" IP: " + String(WiFi_IP)); Serial.println(" Hostname: " + myHostname); long rssi = WiFi.RSSI(); Serial.print(" Signal: "); Serial.print(rssi); Serial.println(" dBm"); }
Kody błędów
| WL_NO_SHIELD | 255 | assigned when no WiFi shield is present |
|---|---|---|
| WL_IDLE_STATUS | 0 | it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED) |
| WL_NO_SSID_AVAIL | 1 | assigned when no SSID are available |
| WL_SCAN_COMPLETED | 2 | assigned when the scan networks is completed |
| WL_CONNECTED | 3 | assigned when connected to a WiFi network |
| WL_CONNECT_FAILED | 4 | assigned when the connection fails for all the attempts |
| WL_CONNECTION_LOST | 5 | assigned when the connection is lost |
| WL_DISCONNECTED | 6 | assigned when disconnected from a network |