Cov txheej txheem:

ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw: 8 Kauj Ruam
ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw: 8 Kauj Ruam

Video: ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw: 8 Kauj Ruam

Video: ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw: 8 Kauj Ruam
Video: Track E 03 How to ARM yourself Derek Banks 2024, Hlis ntuj nqeg
Anonim
ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw
ESP32 Captive Portal los teeb tsa Static thiab DHCP IP Chaw

ESP 32 yog lub cuab yeej nrog WiFi sib xyaw thiab BLE. Nws yog qhov zoo rau IoT cov haujlwm. Tsuas yog muab koj tus SSID, tus lej zais thiab tus IP teeb tsa thiab muab cov khoom tso rau hauv huab. Tab sis, kev tswj hwm IP chaw teeb tsa thiab Cov neeg siv daim ntawv pov thawj tuaj yeem mob taub hau rau tus neeg siv.

Yuav ua li cas yog tias Tus Neeg Siv Khoom xav hloov daim ntawv pov thawj WiFi?

Yuav ua li cas yog tias tus neeg siv xav hloov DHCP/Static IP nqis?

Kev ntsais muag ESP32 txhua lub sijhawm tsis txaus ntseeg thiab tsis yog qhov kev daws rau cov teeb meem no. Ntawm no hauv cov lus qhia no peb yuav nthuav qhia.

  • Yuav ua li cas thiaj tsim tau lub portal raug kaw.
  • Hosting lub vev xaib los ntawm ESP32.
  • Nyeem thiab Sau Ntawv los ntawm SPIFFS ESP32.
  • Tsim kom muaj Kev Nkag Nkag Nkag Nkag Nkag Nkag thiab txuas mus rau chaw nres tsheb

Kauj Ruam 1: Kev Kho Vajtse thiab Software Tshwj Xeeb

Kho vajtse Specification

  • ESP32 WiFi/BLE
  • Wireless Kub thiab Vaum Sensor

Specification ntawm Software

Arduino IDE Cov

Kauj Ruam 2: Tsim Lub Nkoj Portal

Tsim kom muaj Kev Nkag Nkag Nkag Nkag
Tsim kom muaj Kev Nkag Nkag Nkag Nkag
Tsim kom muaj Kev Nkag Nkag Nkag Nkag
Tsim kom muaj Kev Nkag Nkag Nkag Nkag
Tsim Kev Nkag Nkag Nkag Nkag
Tsim Kev Nkag Nkag Nkag Nkag

Lub portal raug kaw yog lub vev xaib uas tau nthuav tawm rau cov neeg siv txuas tshiab t ua ntej lawv tau txais kev nkag mus tau dav rau cov peev txheej network. Ntawm no peb tab tom ua haujlwm peb nplooj ntawv los xaiv ntawm DHCP thiab Static IP Settings. peb tuaj yeem txhais tus IP chaw nyob rau ESP hauv ob txoj hauv kev.

  • DHCP IP chaw nyob- nws yog ib txoj hauv kev los muab tus IP chaw nyob rau lub cuab yeej. ESP qhov chaw nyob IP tsis yog 192.168.4.1
  • IP chaw nyob zoo li qub- muab tus IP chaw nyob ruaj khov rau peb cov cuab yeej siv network. los muab tus IP zoo li qub rau lub cuab yeej peb yuav tsum tau txheeb xyuas tus IP chaw nyob, rooj vag chaw nyob, thiab subnet daim npog qhov ncauj.

Hauv thawj lub vev xaib, Tus Neeg Siv tau muab lub xov tooj cua khawm los xaiv ntawm DHCP thiab Static IP teeb tsa. Hauv lub vev xaib tom ntej no, peb yuav tsum muab IP cov ntaub ntawv ntsig txog txhawm rau txuas ntxiv mus.

HTML Code

Tus lej HTML rau nplooj ntawv web tuaj yeem pom hauv Github qhov chaw cia khoom no.

Koj tuaj yeem siv ib qho IDE lossis cov ntawv sau zoo li Sublime lossis notepad ++ los ua HTML nplooj ntawv web.

  • Ua ntej Tsim HTML webpage uas muaj ob lub xov tooj cua khawm los xaiv ntawm DHCP thiab Static IP Settings.
  • Tam sim no tsim lub pob kom xa koj cov lus teb
  • Muab qee lub npe rau khawm xov tooj cua. ESP Web server chav kawm yuav siv cov npe no los sib cav thiab tau txais cov lus teb ntawm cov xov tooj cua khawm siv cov lus sib cav no
  • Tam sim no ntxig rau 'SUBMIT' khawm kom xa cov lus teb rau lub cuab yeej.
  • Hauv lwm lub vev xaib, peb muaj cov ntawv ntawv. Muab lub npe tus nqi thiab Hom nkag mus rau lub thawv ntawv thiab ntxiv khawm xa mus rau 'SUBMIT' xa cov lus teb.
  • Tsim lub 'RESET' khawm kom rov pib dua cov ntsiab lus ntawm kab ntawv.

// Xov tooj cua Khawm DHCP Teeb

Teeb IP Static

// Cov thawv ntawv nkag

// Xa Khawm

cov tswv yim [hom = "xa"] {keeb kwm yav dhau xim: #3498DB; / * Ntsuab */ ciam teb: tsis muaj; xim: dawb; padding: 15px 48px; text-align: nruab nrab; text-decoration: tsis muaj; zaub: inline-block; font-loj: 16px; }

// Pib dua khawm

cov tswv yim [hom = "xa"] {keeb kwm yav dhau xim: #3498DB; / * Ntsuab */ ciam teb: tsis muaj; xim: dawb; padding: 15px 48px; text-align: nruab nrab; text-decoration: tsis muaj; zaub: inline-block; font-loj: 16px; }

Kauj Ruam 3: Tau Txais Lub Vev Xaib Los Ntawm Lub Vev Xaib mus rau ESP32

Tau Txais Cov Lus Teb Hauv Lub Vev Xaib los ntawm Nplooj Ntawv rau ESP32
Tau Txais Cov Lus Teb Hauv Lub Vev Xaib los ntawm Nplooj Ntawv rau ESP32

Ua haujlwm cov vev xaib los ntawm ESP 32 lub cuab yeej yog qhov lom zem heev. Nws tuaj yeem yog txhua yam los ntawm kev qhia cov ntaub ntawv ntsuas kub hauv lub vev xaib, xa cov coj los ntawm cov vev xaib uas raug cai lossis khaws cov neeg siv WiFi daim ntawv pov thawj los ntawm lub vev xaib. Rau lub hom phiaj no, ESP 32 siv WebServer Class rau server nplooj ntawv web.

  • Ua ntej, Tsim ib qho piv txwv ntawm WebServer chav kawm ntawm chaw nres nkoj 80 (HTTP chaw nres nkoj).
  • Tam sim no teeb tsa ESP ntaus ntawv ua softAP. Muab SSID thiab passkey thiab muab tus IP zoo li qub rau lub cuab yeej.
  • Pib server.

// ********* SSID thiab Pass rau AP **************/

const char *ssidAP = "muab SSID"; const char *passAP = "hla tus yuam sij";

// ********* Static IP Config **************/IPAddress ap_local_IP (192, 168, 1, 77); IPAddress ap_gateway (192, 168, 1, 254); IPAddress ap_subnet (255, 255, 255, 0);

// ********* SoftAP Config **************/

WiFi.mode (WIFI_AP);

Serial.println (WiFi.softAP (ssidAP, passAP)? "Soft-AP teeb": "Ua tsis tau txuas");

ncua (100); Serial.println (WiFi.softAPConfig (ap_local_IP, ap_gateway, ap_subnet)? "Configuring Soft AP": "Error in Configuration"); Serial.println (WiFi.softAPIP ());

// pib server

server.begin ();

  • Tsim thiab ua haujlwm rau URL siv kev hu rov qab sib txawv.
  • thiab tuav cov neeg siv khoom asynchronously siv handleClient.

server.on ("/", handleRoot);

server.on ("/dhcp", handleDHCP); server.on ("/zoo li qub", handleStatic); // lis cov lus teb server.handleClient ();

  • Txhawm rau nkag mus rau cov nplooj ntawv web. Txuas rau AP uas koj nyuam qhuav tsim, teev nyob hauv koj li WiFi tes hauj lwm. Tam sim no, mus rau qhov browser, Nkag mus rau IP teeb tsa los ntawm koj hauv qib kawg thiab nkag mus rau lub vev xaib.
  • Web server chav kawm siv lub npe muab rau kev nkag ('ntawv', 'khawm', 'radiobutton'etc.) Raws li kev sib cav. Nws txuag cov lus teb ntawm cov tswv yim no raws li kev sib cav thiab peb tuaj yeem tau txais qhov muaj txiaj ntsig lossis tshawb xyuas lawv siv args, arg, hasArg txoj kev.

yog (server.args ()> 0) {rau (int kuv = 0; kuv <= server.args (); kuv ++) {

Serial.println (Txoj hlua (server.argName (i)) + '\ t' + String (server.arg (i)));

}

yog (server.hasArg ("ipv4static") && server.hasArg ("gateway") && server.hasArg ("subnet")) {staticSet (); } lwm yog (server.arg ("ipv4")! = "") {dhcpSetManual (); } lwm {dhcpSetDefault (); }

Kauj ruam 4: Static IP Configuration

Static IP Configuration
Static IP Configuration

Txog tam sim no peb tau nkag siab yuav ua li cas txuas rau AP thiab yuav ua li cas kom tau txais qhov txiaj ntsig los ntawm cov lus teb ntawm lub vev xaib

Hauv qib no, peb yuav teeb tsa IP zoo li qub

  • Xaiv Static IP Setting thiab nyem rau ntawm Submit khawm. Koj yuav raug xa rov mus rau nplooj ntawv tom ntej.
  • Hauv nplooj ntawv tom ntej nkag mus rau tus IP zoo li qub, chaw nyob qhov rooj thiab subnet Mask Daim nplooj ntawv no yuav tau txais kev pabcuam ntawm "/zoo li qub" uas yog ua los ntawm kev tuav txoj haujlwm rov qab zoo li qub.
  • Tau txais tus nqi ntawm cov ntawv sau siv server.arg () txoj kev.

Txoj hlua ipv4static = Txoj hlua (server.arg ("ipv4static"));

Txoj hlua txoj hlua = Txoj hlua (server.arg ("rooj vag")); Txoj hlua subnet = Txoj hlua (server.arg ("subnet"));

  • Tam sim no, cov txiaj ntsig no tau muab sau ua JSON Hom.
  • Tom qab ntawd peb yuav sau JSON rau SPIFFS.

hauv paus ["statickey"] = "staticSet";

hauv paus ["staticIP"] = ipv4static;

hauv paus ["rooj vag"] = rooj vag;

hauv paus ["subnet"] = subnet;

Cov ntaub ntawv fileToWrite = SPIFFS.open ("/ip_set.txt", FILE_WRITE);

yog (root.printTo (fileToWrite)) {

Serial.println ("-Cov Ntawv Sau"); }

  • Qhov kev teeb tsa no tau txais kev cawmdim hauv SPIFFS. Tom qab ntawv, cov txiaj ntsig no tau nyeem los ntawm SPIFFS.
  • Tus nqi IP zoo li qub yog cais los ntawm JSON.

Cov ntaub ntawv cov ntaub ntawv = SPIFFS.open ("/ip_set.txt", "r");

thaum (file.available ()) {

debugLogData += char (file.read ()); }

yog (debugLogData.length ()> 5) {

JsonObject & readRoot = jsonBuffer.parseObject (debugLogData);

yog (readRoot.containsKey ("statickey")) {

Txoj hlua ipStaticValue = readRoot ["staticIP"];

Txoj hlua gatewayValue = readRoot ["rooj vag"];

Txoj hlua subnetValue = readRoot ["subnet"];

Kauj ruam 5: DHCP Chaw

DHCP Chaw
DHCP Chaw

Hauv theem no, peb yuav teeb tsa DHCP Chaw

Xaiv DHCP Chaw los ntawm nplooj ntawv ntsuas thiab nyem rau "Xa"

  • Koj yuav raug xa rov mus rau nplooj ntawv tom ntej. Hauv nplooj ntawv tom ntej nkag mus rau tus IP chaw lossis xaiv xaiv lub neej ntawd thiab nyem rau ntawm "Xa" khawm kom xa cov lus teb. Nplooj ntawv no yuav tau txais kev pabcuam ntawm "/dhcp" uas yog tuav los ntawm handleDHCP txoj kev hu rov qab. Tau txais tus nqi ntawm cov ntawv sau siv server.arg () txoj hauv kev. Thaum nyem rau hauv xaiv lub npov default. 192.168.4.1 IP yuav muab rau lub cuab yeej.
  • Tam sim no, cov txiaj ntsig no tau muab sau ua JSON Hom.
  • Tom qab ntawd peb yuav sau JSON rau SPIFFS.

JsonObject & hauv paus = jsonBuffer.createObject ();

hauv paus ["dhcpManual"] = "dhcpManual";

hauv paus ["dhcpIP"] = "192.168.4.1";

Cov ntaub ntawv fileToWrite = SPIFFS.open ("/ip_set.txt", FILE_WRITE);

yog (root.printTo (fileToWrite)) {

Serial.println ("-Cov Ntawv Sau"); }

  • Qhov kev teeb tsa no tau txais kev cawmdim hauv SPIFFS. Tom qab ntawv, cov txiaj ntsig no tau nyeem los ntawm SPIFFS.
  • Dhcp IP qhov tseem ceeb yog tom qab ntawd cais los ntawm JSON.

Cov ntaub ntawv cov ntaub ntawv = SPIFFS.open ("/ip_set.txt", "r"); thaum (file.available ()) {debugLogData += char (file.read ())); } yog (debugLogData.length ()> 5) {JsonObject & readRoot = jsonBuffer.parseObject (debugLogData);

yog (readRoot.containsKey ("dhcpDefault")) {

Txoj hlua ipdhcpValue = readRoot ["dhcpIP"];

Serial.println (ipdhcpValue);

dhcpAPConfig ();}

Kauj Ruam 6: Txuag Daim Ntawv Pov Thawj WiFi

Txuag WiFi Cov Ntawv Pov Thawj
Txuag WiFi Cov Ntawv Pov Thawj

Txog tam sim no, peb tau xaiv tus IP teeb tsa. Tam sim no peb yuav tsum khaws cov ntaub ntawv pov thawj wifi ntawm Tus Neeg Siv. Los daws qhov teeb meem no. Peb tau ua raws cov txheej txheem no.

  • Yog li tam sim no peb muaj peb lub cuab yeej AP teeb tsa hauv DHCP lossis Static IP teeb tsa uas peb tau xaiv los ntawm qhov chaw raug kaw hais hauv cov kauj ruam kawg.
  • Cia peb hais tias peb tau xaiv Static IP teeb tsa.
  • Peb yuav teeb tsa softAP ntawm tus IP no.
  • Tom qab nyeem qhov tseem ceeb ntawm SPIFFS thiab txheeb xyuas cov txiaj ntsig no los ntawm JSON. Peb yuav teeb tsa softAP ntawm tus IP no.
  • Hloov IP Txoj hlua rau hauv Bytes.

byte tus ip [4];

parseBytes (ipv4Arr, '.', ip, 4, 10);

ip0 = (uint8_t) ip [0];

ip1 = (uint8_t) ip [1];

ip2 = (uint8_t) ip [2];

ip3 = (uint8_t) ip [3];

IPAddress ap_local (ip0, ip1, ip2, ip3);

// *************** Parse bytes los ntawm txoj hlua ****************** //

void parseBytes (const char* str, char sep, byte* bytes, int maxBytes, int puag) {

rau (int kuv = 0; kuv <maxBytes; kuv ++) {

bytes = strtoul (str, NULL, puag);

str = strchr (str, sep);

yog (str == NULL || *str == '\ 0') {

tawg;

}

str ++;

}}

Tam sim no peb yuav teeb tsa softAP ntawm tus IP no

Serial.println (WiFi.softAPConfig (ap_localWeb_IP, ap_gate, ap_net)? "Teeb tsa softAP": "tsis txuas nrog"); Serial.println (WiFi.softAPIP ());

  • Tam sim no pib lub web server thiab ua haujlwm lub vev xaib ntawm tus IP no. Txhawm rau nkag rau WiFi Cov Ntawv Pov Thawj ntawm Tus Neeg Siv.
  • Lub vev xaib muaj ob kab ntawv sau nkag mus rau SSID thiab Password.
  • handleStaticForm yog txoj kev hu rov qab uas ua haujlwm rau lub vev xaib.
  • server.handleClient () saib xyuas qhov kev thov thiab cov lus teb rau thiab los ntawm lub vev xaib.

server.begin ();

server.on ("/", handleStaticForm);

server.onNotFound (handleNotFound);

STimer = millis ();

thaum (millis ()-STimer <= SInterval) {

server.handleClient (); }

Daim ntawv HTML raug khaws tseg hauv SPIFFS. peb tshawb xyuas qhov kev sib cav uas tsim nyog siv server.arg (). kom tau txais tus nqi SSID thiab Password

Cov ntaub ntawv cov ntaub ntawv = SPIFFS.open ("/WiFi.html", "r");

server.streamFile (ntaub ntawv, "ntawv nyeem/html");

file.close (); cov.

Kauj Ruam 7: Nyeem thiab Sau Los Ntawm SPIFFS

SPIFFS

Serial Peripheral Interface Flash File System, lossis SPIFFS kom luv. Nws yog lub teeb-hnyav cov ntaub ntawv kaw lus rau microcontrollers nrog SPI flash nti. Onboard flash nti ntawm ESP32 muaj chaw txaus rau koj nplooj ntawv web. Peb kuj tseem khaws peb lub vev xaib hauv Flash System. Muaj ob peb kauj ruam peb yuav tsum ua raws txhawm rau xa cov ntaub ntawv mus rau spiff

Rub tawm ESP 32 SPIFFS cov ntaub ntawv xa cov cuab yeej:

  • Hauv koj phau ntawv teev npe Arduino sketchbook, tsim cov npe khoom yog tias nws tseem tsis tau muaj
  • Unpack cov cuab yeej rau hauv cov cuab yeej phau ntawv (txoj kev yuav zoo li /Arduino/tools/ESP32FS/tool/esp32fs.jar)
  • Rov pib dua Arduino IDE
  • Qhib cov duab kos (lossis tsim ib qho tshiab thiab txuag nws)
  • Mus rau daim duab qhia chaw (xaiv Sketch> Qhia Sketch Folder)
  • Tsim cov npe npe cov ntaub ntawv thiab txhua cov ntaub ntawv koj xav tau hauv cov kab ke system nyob ntawd. Peb tau tshaj tawm peb nplooj ntawv HTML nrog lub npe webform.html
  • Nco ntsoov tias koj tau xaiv pawg thawj coj, chaw nres nkoj, thiab kaw Serial Monitor
  • Xaiv Cov Cuab Yeej> ESP8266 Sketch Data Upload. Qhov no yuav tsum pib rub cov ntawv mus rau hauv ESP8266 flash file system. Thaum ua tiav, IDE xwm txheej bar yuav tso saib SPIFFS Duab Tshaj tawm cov lus.

void handleDHCP () {File file = SPIFFS.open ("/page_dhcp.html", "r"); server.streamFile (ntaub ntawv, "ntawv nyeem/html"); ntawv.close ();}

tsis muaj dab tsi handleStatic () {

Cov ntaub ntawv cov ntaub ntawv = SPIFFS.open ("/page_static.html", "r"); server.streamFile (ntaub ntawv, "ntawv nyeem/html"); ntawv.close ();}

Sau ntawv mus rau SPIFFS

Ntawm no peb tab tom sau qhov chaw khaws tseg rau SPIFFS yog li cov neeg siv yuav tsum tsis txhob mus dhau cov theem no thaum twg ntaus ntawv rov pib dua.

  • Hloov cov lus sib cav tau txais los ntawm lub vev xaib mus rau JSON cov khoom
  • Sau JSON no mus rau.txt cov ntaub ntawv khaws tseg hauv SPIFFS.

Txoj hlua ipv4static = Txoj hlua (server.arg ("ipv4static"));

Txoj hlua txoj hlua = Txoj hlua (server.arg ("rooj vag")); Txoj hlua subnet = Txoj hlua (server.arg ("subnet")); hauv paus ["statickey"] = "staticSet"; hauv paus ["staticIP"] = ipv4static; hauv paus ["rooj vag"] = rooj vag; hauv paus ["subnet"] = subnet; Txoj hlua JSONStatic; char JSON [120]; root.printTo (Serial); root.prettyPrintTo (JSONStatic); JSONStatic.toCharArray (JSON, sizeof (JSONStatic) +2); Cov ntaub ntawv fileToWrite = SPIFFS.open ("/ip_set.txt", FILE_WRITE); yog (! fileToWrite) {Serial.println ("Yuam kev qhib SPIFFS"); } yog (fileToWrite.print (JSON)) {Serial.println ("-Cov Ntawv Sau"); } lwm {Serial.println ("-Yuam Kev Sau Cov Ntaub Ntawv"); } fileToWrite.close ();

Kauj Ruam 8: Txoj Cai Tag Nrho

Over Code rau HTML thiab ESP32 tuaj yeem pom nyob hauv Github Repository no

Pom zoo: