생각보다 너무 간단히 붙여볼 수 있어서 깜짝 놀랬음.
1. WIFI 스캔하기
파일 > 예제 > WIFI > WiFiScan 선택하면 WiFi Scan 예제 실행해볼 수 있음
실행 화면: 시리얼 출력으로 아래 처럼 주기적으로 WIFI Scan 하고 신호감도를 보여줌
주요 코드
#include "WiFi.h"
void setup()
{
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
}
void loop()
{
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(WiFi.SSID(i));
Serial.print(WiFi.RSSI(i));
}
}
}
전체 코드 - 아두이노 스케치 코드는 아래 처럼 몇줄 되지 않음
더보기
/*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
#include "WiFi.h"
void setup()
{
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop()
{
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
2. WIFI Station 으로 동작 시키기 + Simple Web Server 동작
- 마찬가지로 예제에서 파일 > 예제 > WIFI > SimpleWiFiServer로 확인 가능
- 이 예제에서는 아래 보이는 ssid, password 를 시험 환경에 맞게 수정해줘야 함
- 그리고 예제에서는 GPIO5 번을 url 또는 웹페이지의 링크를 선택해서 HIGH, LOW 로 쓰고,
이를 통해 LED on/off 를 시켜볼 수 있는데 사용하는 보드에 맞게 수정 필요. - ESP32 DEVKIT V1 의 경우는 GPIO2 에 파란색 LED가 설치되어 있음
#include <WiFi.h>
const char* ssid = "yourssid";
const char* password = "yourpasswd";
WiFiServer server(80);
실행화면 - 시리얼 출력
실행화면 - 브라우져 화면
Simple Web Server 코드 주요 부분
#include <WiFiClient.h>
WiFiServer server(80);
void setup() {
WiFi.begin(ssid, password); // WIFI 연결 시도
server.begin(); // 웹서버 시작
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
전체 예제 코드
더보기
/*
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 5.
If the IP address of your shield is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* LED attached to pin 5
created for arduino 25 Nov 2012
by Tom Igoe
ported for sparkfun esp32
31.01.2017 by Jan Hendrik Berlin
*/
#include <WiFi.h>
const char* ssid = "MINDLES";
const char* password = "5000068903";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(5, OUTPUT); // set the LED pin mode
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
int value = 0;
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(5, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(5, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
3. WIFI AP 로 동작 시키기
마찬가지로 예제에서 파일 > 예제 > WIFI > WiFiAccessPoint 로 확인해보면 됨
이 예제에서는 아래 보이는 ssid, password 를 설정해줘야 하고, 서버 구동 부분은 SimpleWiFiServer 와 동일
#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "yourAP";
const char *password = "yourPassword";
AccessPoint 구동 주요 코드
void setup() {
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
}
'아두이노 > 아두이노+ESP32' 카테고리의 다른 글
ESP32 ESP IDF 설치하기 Windows 10 + VS Code (0) | 2022.07.10 |
---|---|
아두이노 ESP32 - USB로 Android와 데이터 주고받기, 안드로이드폰으로 간이 온도 습도 측정기 만들기 (1) | 2022.07.02 |
ESP32 브레드보드 2개로 연결하기 (0) | 2022.06.18 |
아두이노 ESP32 - RX-9(CO2 센서) 붙여 보기 - Analog 센서, 라이브러리 추가 (0) | 2022.06.16 |
ESP32 - 아두이노 시작하기 (0) | 2022.06.12 |
댓글