nodeMCU๋ ESP8266 ๋ชจ๋์ด๋ผ WiFi์ ์ ์๋ ๊ฐ๋ฅํ๊ณ (Station mode), ์ค์ค๋ก๊ฐ WiFi๋ฅผ ํฐ๋จ๋ฆด ์๋(Soft AP mode) ์๋ค.
๋ชฉํ ๊ธฐ๋ฅ
(1) ์์ดํ์ด ์ค์บ์ด ๊ฐ๋ฅํ๋ค.
(2) ์ค์บํ ์์ดํ์ด ๋ชฉ๋ก ์ค์์ ์์ดํ์ด๋ฅผ ์ ํํ ํ, ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค.
(3) ์๋์ด๋ ธ IDE, ์ฆ sktech์์ ์ ํํ ์์ดํ์ด์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํ ์ ์๋ค.
Step1. ์์ดํ์ด ์ค์บํ๊ธฐ.
(1) ๊ด๋ จ ํค๋ํ์ผ ์ถ๊ฐ
#include "ESP8266WiFi.h"
#include <ESP8266WebServer.h>
(2) ์ค์บ ๊ธฐ๋ฅ ์ถ๊ฐ
void scanWiFiList()
{
int numberOfNetworks = WiFi.scanNetworks();
for(int i =0; i<numberOfNetworks; i++){
Serial.println(WiFi.SSID(i));
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
scanWiFiList();
}
void loop() {
// put your main code here, to run repeatedly:
}
<์ถ๋ ฅ๊ฒฐ๊ณผ>
๊ทผ์ฒ์์ ์กํ๋ ๋ชจ๋ ์์ดํ์ด๊ฐ ๊ฒ์๋์ด์ ์ถ๋ ฅ์ด ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ชฉํ๊ธฐ๋ฅ (1) ํด๋ฆฌ์ด.
Step2. ์นํ์ด์ง ๋์ฐ๊ธฐ.
ํ์ฌ ์๋์ด๋ ธ์์๋ง ์ถ๋ ฅ ๋์์ ๋ฟ, ์ฌ์ฉ์๊ฐ ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๋ผ์ ์นํ์ด์ง๋ฅผ ๋์ AP๋ฅผ ์ ํํ๊ฒ ํ๋ค.
(1) softAP ๋ชจ๋๋ก ์คํํ๊ธฐ ์ํด ssid์ ํจ์ค์๋ ์ ํ๊ธฐ
#define WIFI_MAX_SIZE 30 //์ต๋ 30๊ฐ์ AP ๋ชฉ๋ก๋ง ๋ฐ์์จ๋ค.
const char* ssid = "KWE_Solution";
const char* password = "12345678";
(2) ์๋ฒ ๋ฐ AP ๋ชฉ๋ก์ ๋ด์ ๋ฐฐ์ด ์ ์ธ
ESP8266WebServer server(80);
String wifiList[WIFI_MAX_SIZE];
(3) Soft AP ๋ชจ๋ ์คํ
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
scanWiFiList();
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
(4) scanWifiList ํจ์ ๋ณ๊ฒฝ
void scanWiFiList()
{
int numberOfNetworks = WiFi.scanNetworks();
for(int i =0; i<numberOfNetworks; i++){
wifiList[i] = WiFi.SSID(i);
}
}
(5) ์นํ์ด์ง ๋์ฐ๊ธฐ
void handleRoot()
{
server.send(200, "text/html", prepareSelectWifiListPage());
}
String prepareSelectWifiListPage(){
String strList ="<ul>";
for(int i =0; i< WIFI_MAX_SIZE; i++){
if(wifiList[i] != "")
{
strList += "<li><a name='" + wifiList[i] + "' onclick='select(this.name)'>" + wifiList[i] + "</a> </li>";
}
}
strList += "</ul>";
String apName = "";
String apPw = "";
String htmlPage =
String("<!DOCTYPE HTML>") +
"<html> <head>" +
"<meta http-equiv=\"Content-Type\" Content=\"text/html; charset=utf-8\">\r\n" +
"<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>\r\n" +
"<title>WIFI ๋ชฉ๋ก</title>\r\n"+
"<style>\r\n"+
".a:hover{cursor:pointer;}\r\n"+
"</style></head>\r\n"+
"<body>\r\n"+
"<h1>WiFi List</h1>\r\n"+
"์ธ์ฆ๊ฐ๋ฅํ WiFi๋ฅผ ์ ํํ์ธ์. <br>\r\n"+
"<div id='wifiList'>"+ strList + "</div>\r\n"
"<div id='selected'></div>\r\n"
"<div id='_pw'>\r\n"+
"<form method='get' action='/action_page'>\r\n"+
"<input type='text' id='ap' name='apName'> ์ ์ ํํ์
จ์ต๋๋ค. ์๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์. <br>\r\n"+
"<input type='password' id='pw' name='apPw'> <input type='submit' value='connect'>\r\n"+
"</form>\r\n"+
"</div>\r\n"+
"</body>\r\n"+
"<script>\r\n"+
"document.getElementById('_pw').style.visibility = 'hidden';\r\n"+
"function select(apName){\r\n"+
"document.getElementById('ap').value = apName;\r\n"+
"document.getElementById('_pw').style.visibility = 'visible';\r\n"+
"document.getElementById('pw').value = ''}\r\n"+
"function connection(){ \r\n"+
"var pw = document.getElementById('pw').value;\r\n"+
"} \r\n"+
"</script> \r\n"+
"</html> \r\n";
return htmlPage;
}
<์ถ๋ ฅ๊ฒฐ๊ณผ>
์์ดํ์ด์ ๋ด๊ฐ ์ค์ ํ ssid๊ฐ ์ ํ์๋์ง ํ์ธํ๋ค.
ssid๋ฅผ ์ ํํํ, ๋ด๊ฐ ์ค์ ํ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ฌ ์ ์ํ๋ค.
์๋์ด๋ ธ ํ๋ฉด
์น๋ธ๋ผ์ฐ์ ์์ ์๋์ด๋ ธ์ ์ ํ 192.168.4.1 ๋ก ์ ์ํ๋ค.
์ ๋ชฉ๋ก์์ wifi๋ฅผ ์ ํํ๋ฉด ์๋์ ๊ฐ์ด ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅํ ์ ์๋ ์ฐฝ์ด ๋ฌ๋ค.
Step3. AP์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ ์๋์ด๋ ธ์์ ๋ฐ๊ธฐ.
์ด ๋ถ๋ถ์ html์ ๋ํ ์ง์์ด ํ์ํ๋ค.
form ํ๊ทธ๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์์ ํ ๊ฒ์ด๋ค.
(1) ์๋ฒ ์์์์ form ํ๊ทธ๋ฅผ ์์ ํ ์ฃผ์๋ฅผ ๋ฑ๋กํ๋ค.
void setup(){
server.on("/action_page", handleForm); //action_page์ ์ ์ํ๋ฉด handleForm ํจ์๋ฅผ ์คํํ๋ค.
}
(2) handleForm ํจ์ ์ถ๊ฐ
void handleForm() {
String apName = server.arg("apName");
String apPw = server.arg("apPw");
Serial.print("apName:");
Serial.println(apName);
Serial.print("apPw:");
Serial.println(apPw);
String s = "<a href='/'> Go Back </a>";
server.send(200, "text/html", s); //Send web page
}
(3) prepareSelectWifiListPage() ํจ์ ํ์ธ
์ด ๋ถ๋ถ์์ ์ ํํ AP๊ฐ ์ถ๋ ฅ๋๋ ๋ถ๋ถ๊ณผ ํจ์ค์๋๋ฅผ ์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ์ form ํ๊ทธ๋ก ๋ฌถ์ฌ์๋ค.
๋ฐ๋ผ์ connect๋ฒํผ์ ๋๋ฅด๋ฉด formํ๊ทธ ๊ธฐ๋ฅ์ ํตํด /action_page ํ์ด์ง๋ก ๋์ด๊ฐ๋ฉด์ ํ๋ผ๋ฏธํฐ๊ฐ์ ๋ฐ์ ์ ์๊ฒ ๋๋ ๊ฒ์ด๋ค.
"<form method='get' action='/action_page'>\r\n"+
"<input type='text' id='ap' name='apName'> ์ ์ ํํ์
จ์ต๋๋ค. ์๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์. <br>\r\n"+
"<input type='password' id='pw' name='apPw'> <input type='submit' value='connect'>\r\n"+
"</form>\r\n"+
<์ถ๋ ฅํ๋ฉด>
WiFi ๋ฆฌ์คํธ ์กฐํ.
WiFi ์ ํ. ๋น๋ฐ๋ฒํธ ์ ๋ ฅํ, connect ๋ฒํผ ํด๋ฆญ ์ action_page๋ก ๋์ด๊ฐ๋ฉด์ ์๋์ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
์น๋ธ๋ผ์ฐ์ ์ ์ฃผ์์ฐฝ์ ๋ณด๋ฉด apName=smartfarm, apPw=1234567 ์ ํ์ธํ ์ ์๋ค.
์๋์ด๋ ธ ์ค์ผ์น ํ๋ฉด
handleForm ํจ์์์ ์ธ์๊ฐ์ผ๋ก ๋ค์ด์จ apName, apPw๊ฐ์ด ํ์ธ๋๋ ๊ฒ์ ์ ์ ์๋ค.
<๋ ํด์ผํ ๊ฒ>
์ด apName, apPW ๊ฐ์ ๊ฐ์ง๊ณ ๋ค์ STA ๋ชจ๋๋ก ์๋ฒ๋ฅผ ๋ฐ๋ก ์ด์์๋ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด์ผํ ๊ฒ.
์ฐธ๊ณ ์๋ฃ
https://circuits4you.com/2019/03/20/esp8266-receive-post-get-request-data-from-website/
๋๊ธ