AP:
PC1 AX5G_1 :CPSHScpshs1
用WiFi.h程式庫會有4種wifi模式可以用:
語法
WIFI模式
功能
WiFi.mode(WIFI_AP);
Access Point (AP); ESP32可以讓其他設備透過wifi接入(就像家裡的wifi基地台,可供手機連線)。
WiFi.mode(WIFI_STA);
Station(STA)
無線終端模式,也就是讓ESP32可以連接上其他的熱點(就像手機一樣,可以連上家裡wifi)。
WiFi.mode(WIFI_AP_STA);
AP+STA
將ESP32設置成兩個模式並存。
WiFi.mode(WIFI_OFF);
OFF
關閉wifi
https://crazymaker.com.tw/esp32-connect-to-wifi-network/
wifi訊號掃描<檔案位置>
#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);
}
#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);
}
EPS32 透過wifi 使用網頁瀏覽器控制 認識HTML
#include <WiFi.h>
const char* ssid = "ROBOT"; // 雙引號內,修改為你要 ESP32 連上的 WiFi 網路名稱 SSID
const char* password = "0936836003"; // 雙引號內,鍵入此網路的密碼
WiFiServer server(80); //設定網路伺服器 port number 為 80
// Variable to store the HTTP request
String header;
String output26State = "off"; //設定字串變數 output26State,顯示 GPIO26 狀態。如果您要增加可控制的 GPIO 數目,請在這裡增加
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200); //設定序列埠螢幕顯示的 baud rate 為 115200
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
Serial.print("Connecting to "); // 連上你所指定的Wi-Fi,並在序列埠螢幕中,印出 ESP32 web server 的 IP address
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // 看有沒有外部裝置 client (如手機),藉著瀏覽器 browser 連上 ESP32 web server
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
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
header += c;
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("Connection: close");
client.println();
// 接收 client 的指令,點亮或關閉 連上 GPIO 的 LED。瀏覽器上,不同的按鈕,會發出不同的指令 (URLs requests)
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// 設計 client 上的瀏覽器網頁格式,包括顏色、字型大小、有無邊框、字元等,這一部分是用 HTML 的語言寫成的
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// 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
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
中文字顯示亂碼時,要注意這編碼的問題。
設定 UTF-8 編碼
client.println("<meta charset=\"UTF-8\">");
EPS32透過wifi讀取PM2.5資料
#include <WiFi.h>
#include <HTTPClient.h>
char ssid[] = "AX"; //請修改
char password[] = "CPSHScpshs"; //請修改
char url[] = "http://opendata2.epa.gov.tw/AQI.json"; //PM2.5的網址
void setup() {
Serial.begin(115200);
delay(1000);
Serial.print("開始連線到無線網路SSID:");
Serial.println(ssid);
//1.設定WiFi模式
WiFi.mode(WIFI_STA);
//2.啟動WiFi連線
WiFi.begin(ssid, password);
//3.檢查連線狀態
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("連線完成");
}
void loop() {
//4.啟動網頁連線
Serial.print("啟動網頁連線");
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
Serial.print("httpCode=");
Serial.println(httpCode);
//5.檢查網頁連線是否正常
if (httpCode == HTTP_CODE_OK) {
//6.取得網頁內容
String payload = http.getString();
Serial.print("payload=");
//7.將資料顯示在螢幕上
Serial.println(payload);
}
http.end();
delay(10000);
}
網頁顯示溫溼度
https://blog.darkthread.net/blog/temp-humd-esp-web/