close

ESP32 wifi WebServer設定
參考原文網址:https://kknews.cc/code/jleplne.html

https://kknews.cc/zh-tw/code/e6v88q4.html

網路資源

ESPAsyncWebServer下載位置

https://github.com/me-no-dev/ESPAsyncWebServer

AsyncTCP下載位置

https://github.com/me-no-dev/AsyncTCP

建立ESP32 WebServer 的網路教學文

https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/

https://randomnerdtutorials.com/esp32-client-server-wi-fi/

(本文為記錄)

//匯入Wifi標頭檔

#include <WiFi.h>


//設置WiFi工作模式,參數可選WIFI_OFF WIFI_STA WIFI_AP WIFI_AP_STA;bool

WiFi.mode(wifi_mode_t)

mode(WIFI_OFF)            //關閉

mode(WIFI_AP)              //AP模式

mode(WIFI_STA)            //Station模式

mode(WIFI_AP_STA)     //AP 和 Station模式並存
 


ESP32 wifi 3種模式

station 模式

soft-AP 模式

station + soft-AP 模式

//將ESP32設定為AP模式,SSID=ESP32_AP,可連4個設備,建立為AP時,默認IP 192.168.4.1

WiFi.softAP("ESP32_AP");

//AP模式使用方法,成功啟動後返回布林值

softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4)


//const char* SSID(自訂ESP32網路名稱),指標類型

//const char* passphrase=NULL (連線密碼,此設定為不用密碼)

//int ssid_hidden=0(是否隱藏SSID名稱,0設定為不隱藏,1設定為隱藏)

//int max_connection=4( 設定最大連接數量,設定為4,ESP32連接裝置有限5以下)

 

bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)

設置本地地址、網關地址和子網掩碼,默認分別為192.168.4.1 192.168.4.1 255.255.255.0;

bool softAPdiscon



原文網址:https://kknews.cc/code/jleplne.html


 

//設定websever port 為80 通訊埠

WifiSever  sever(80)

//可自訂義ESP32為靜態IP 後面IP位置可自己設定

IPAddress apIP(192, 168, 1, 100);

//你想要ESP32連線的網絡名稱

const char *ssid = "你的網絡名稱";

//你想要ESP32連線的網絡密碼

const char *password = "網絡密碼";

//開始連接你指定的網路

Wifi.begin(SSID你的網絡名稱, password網絡密碼)

//WIFI連接中,返回值 TRUE

WL_CONNECTED

//設置斷網自動重連接;返回值(true,false)

setAutoReconnect(bool autoReconnect)

//返回是否自動重連接;返回值(true,false)

getAutoReconnect()

//wifi連線狀態

WiFi.status()

//網路連線後,自動產生的動態IP,返回值 為IP位址(IP address)

WiFi.localIP()

//返回MAC地址;(字串String)

uint8_t * macAddress(uint8_t* mac)

macAddress()

//主機名字;返回值const

char * getHostname()

//設置主機名字(自訂主機名字Hostname)

setHostname(const char * hostname)

//聯網狀態,返回值所代表的狀態如下:

wl_status_t status()

255:WL_NO_SHIELD不用在意(兼容WiFi Shield而設計)

0:WL_IDLE_STATUS正在WiFi工作模式間切換;

1:WL_NO_SSID_AVAIL無法訪問設置的SSID網絡;

2:WL_SCAN_COMPLETED掃描完成;

3:WL_CONNECTED連接成功;

4:WL_CONNECT_FAILED連接失敗;

5:WL_CONNECTION_LOST丟失連接;

6:WL_DISCONNECTED斷開連接;

 

事件列表

 

SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready

SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP

SYSTEM_EVENT_STA_START < ESP32 station start

SYSTEM_EVENT_STA_STOP < ESP32 station stop

SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP

SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP

SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed

SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP

SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0

SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode

SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode

SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode

SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode

SYSTEM_EVENT_AP_START < ESP32 soft-AP start

SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop

SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP

SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP

SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface

SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred

SYSTEM_EVENT_ETH_START < ESP32 ethernet start

SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop

SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up

SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down

SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP

SYSTEM_EVENT_MAX

 

//listen  for client incoming,在LOOP裡

//隨時等待用戶端發出請求訊息,有就運作

//sever client.avaliable()回返布林值

WiFiClient client=sever client.avaliabe();

//如果client為true,(sever  client.avaliable()=true)

//執行大括號裡的程式

if (client){   }

//字串變數,接收紀錄來自client端的請求

//先用空白字串

String currentLine="";

 

 

 

 

#include <WiFi.h>

const char* ssid = "ssid";

const char* password = "pwd";

WiFiServer server(80);

HardwareSerial myHardwareSerial(1); //ESP32可宣告需要一個硬體序列,軟體序列會出錯

 

void setup()

{

Serial.begin(115200);

//啟動WIFI

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) //等候連線成功

{

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");//連線成功

//啟動WEB Server

server.begin();

Serial.println("Server started");

//顯示IP

Serial.print("Use this URL : ");

Serial.print("http://");

Serial.print(WiFi.localIP());

Serial.println("/");

Serial.println("use 'http://ip/?mp3=x;' to play x'th mp3 file");

//啟動mp3連線

myHardwareSerial.begin(9600, SERIAL_8N1, 12, 13); // Serial的TX,RX

//實際上只用到TX傳送指令,因此RX可不接(接收player狀態)

Serial.println("Initializing DFPlayer ... (May take 1-2 seconds)");

delay(500);

}

void loop()

{

//WIFI指令-------------------

WiFiClient client = server.available();

if (!client) //有人連入才執行

{

return;

}

Serial.println("new client");

delay(100);

//讀取url中的指令

String request = client.readStringUntil('\r');

Serial.println(request);

 

//-------------------以下為網站內容:HTML語法

client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html");

client.println(""); // do not forget this one

client.println("<!DOCTYPE HTML>");

client.println("<html>");

client.print("<h1>Mp3 now is playing: ");

client.print(i_mp3no);

client.print("</h1>");

client.println("</html>");

delay(1);

//-------------------網站內容結束

client.flush();//清除client

Serial.println("Client disconnected");

Serial.println("");

}

 

WIFI 在AP模式的設定

WiFi.mode(WIFI_AP); //設置工作在AP模式 WiFi.softAPConfig(local_IP, gateway, subnet); //設置AP地址 while(!WiFi.softAP(ssid, password)){}; //啟動AP Serial.println("AP啟動成功"); Serial.print("IP address: "); Serial.println(WiFi.softAPIP()); // 列印IP位址 WiFi.softAPsetHostname("myHostName"); //設置主機名 Serial.print("HostName: "); Serial.println(WiFi.softAPgetHostname()); //列印主機名 Serial.print("mac Address: "); Serial.println(WiFi.softAPmacAddress()); //列印mac地址

原文網址:https://kknews.cc/code/jleplne.html

 
arrow
arrow
    文章標籤
    esp32教學
    全站熱搜
    創作者介紹
    創作者 a15001500 的頭像
    a15001500

    麵包雜記

    a15001500 發表在 痞客邦 留言(0) 人氣()