用于记录学习过程遇到的各个知识点,主要是每个函数的作用。文章内容参考:太极创客。
你可能需要的头文件
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <FS.h>
// 初始化代码位置,如设置引脚用于输入或输出。
void setup() {
// put your setup code here, to run once:
}
// 这函数里的代码是一个死循环里的代码,这个函数会被不断调用。
void loop() {
// put your main code here, to run repeatedly:
}
串口相关
void setup() {
// 以9600波特率启动串口
Serial.begin(9600); // 这一行必须写在setup里。
//通过串口发送字符串,不以换行结尾。
Serial.print("Access Point:");
//通过串口发送字符串,以换行结尾。
Serial.println(ssid);
}
工作模式
AP模式工作
开发板自己创建一个WiFi
const char *ssid = "wangba";
const char *password = "12345678"; // 如果这里为空,则不需要密码就可连接。
void setup() {
Serial.begin(9600); // 这里的参数是波特率。
//esp8266以AP模式工作,并传入热点名称和密码,如果密码是空字符串则没有密码。
//AP模式下,8266会创建一个WiFi,相当于一个路由器。
WiFi.softAP(ssid, password);
// 通过串口输出WiFi名字。
Serial.println(WiFi.SSID());
// 通过串口输出开发板的ip地址。
Serial.println(WiFi.softAPIP());
}
无线终端模式
// 连接某个特定的WiFi。
const char *ssid = "xxxx";
const char *password = "88888888";
void setup() {
WiFi.begin(ssid, password);
int i = 0;
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(i++);
Serial.println(" please waiting...");
}
Serial.println("name: ");
Serial.println(WiFi.SSID());
Serial.println("IP: ");
Serial.println(WiFi.localIP());
}
// 预设多个WiFi,自动连接其中一个。
// 我并没有连接成功。
ESP8266WiFiMulti wifiMulti;
void setup() {
wifiMulti.addAP("xxxx", "12345678");
wifiMulti.addAP("xxxxx", "87654321");
wifiMulti.addAP("xxxxxx", "13572468");
int i = 0;
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(i++);
Serial.println(" please waiting...");
}
Serial.println("name: ");
Serial.println(WiFi.SSID());
Serial.println("IP: ");
Serial.println(WiFi.localIP());
}
基本网络服务器
开启一个web服务
const char *ssid = "xxxx";
const char *password = "88888888";
const int server_port = 8080;
// 开启web服务,并设置端口号为8080。
ESP8266WebServer esp8266_web_server(server_port);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.begin(ssid, password);
int i = 0;
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(i++);
Serial.println(" please waiting...");
}
// 启动服务
esp8266_web_server.begin();
// 绑定路径与处理函数。
esp8266_web_server.on("/", handleRoot);
// 绑定404处理函数
esp8266_web_server.onNotFound(handleNotFound);
Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.print(server_port);
}
void loop() {
// put your main code here, to run repeatedly:
// 处理http请求
esp8266_web_server.handleClient();
}
void handleRoot() {
// 发现中文在电脑浏览器里乱码,手机浏览器中没有乱码。
esp8266_web_server.send(200, "text/plain", "hello from esp8266\n欢迎来到esp8266服务页面。");
}
void handleNotFound() {
esp8266_web_server.send(404, "text/plain", "404 Not Found");
}
通过网页改变io口状态,关键代码如下。
pinMode(LED_BUILTIN, OUTPUT);
void handleRoot() {
esp8266_web_server.send(200, "text/html", "<form action=\"led\" method=\"GET\"> <input type=\"submit\" value=\"Toggle LED\"> </form>");
}
void handleLED() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
esp8266_web_server.sendHeader("Location", "/");
esp8266_web_server.send(303);
}
网页定时刷新,查看io口状态。
void loop() {
// put your main code here, to run repeatedly:
// 处理http请求
esp8266_web_server.handleClient();
button_state = digitalRead(button);
}
void handleRoot() {
// 这里创建一个按钮,当点击时跳转到led页面,执行对应函数。
esp8266_web_server.send(200, "text/html", sendHTML(button_state));
}
String sendHTML(bool buttonState){
String htmlCode = "<!DOCTYPE html> <html>\n";
htmlCode +="<head><meta http-equiv='refresh' content='1'/><meta charset=\"UTF-8\">\n";
htmlCode +="<title>ESP8266 Butoon State</title>\n";
htmlCode +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
htmlCode +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
htmlCode +="</style>\n";
htmlCode +="</head>\n";
htmlCode +="<body>\n";
htmlCode +="<h1>ESP8266 BUTTON STATE -- FLASH 按钮状态</h1>\n";
if(buttonState)
{htmlCode +="<p>Button Status: HIGH</p>\n";}
else
{htmlCode +="<p>Button Status: LOW</p>\n";}
htmlCode +="</body>\n";
htmlCode +="</html>\n";
return htmlCode;
}
闪存文件系统基本操作
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
String file_path = "/xyz/test.txt";
const char *ssid = "xxxx";
const char *password = "88888888";
const int server_port = 80;
ESP8266WebServer web_server(server_port);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("start 开始");
// 连接WiFi。
WiFi.begin(ssid, password);int i = 0;
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(i++);
Serial.println(" please waiting...");
}
web_server.begin();
web_server.on("/", handleRoot);
web_server.onNotFound(handleNotFound);
web_server.on("/show_file", handleShowFile);
// 格式化SPIFFS。
SPIFFS.format();
Serial.println("SPIFFS format finish.");
// 启动文件系统,并判断是否启动成功。
if (SPIFFS.begin()) {
Serial.println("SPIFFS started.");
} else {
Serial.println("SPIFFS Failed to started.");
}
File data_file = SPIFFS.open(file_path, "w");
data_file.println("Hello iot world");
data_file.close();
Serial.println("finished writing data to SPIFFS.");
}
void loop() {
// put your main code here, to run repeatedly:
web_server.handleClient();
}
void handleRoot() {
web_server.send(200, "text/plain", "hello");
}
void handleNotFound() {
web_server.send(404, "text/plain", "404 Not Found");
}
void handleShowFile() {
String file_content = "";
if (SPIFFS.exists(file_path)) {
File data_file = SPIFFS.open(file_path, "r");
for (int i = 0; i < data_file.size(); i++) {
// 一次读出一个字符。
file_content += (char)data_file.read();
}
}
web_server.send(200, "text/plain", file_content);
}
其它函数
// 读取文件夹目录。
Dir dir = SPIFFS.openDir(folder_name); // 建立“目录”对象
while (dir.next()) { // dir.next()用于检查目录中是否还有“下一个文件”
Serial.println(dir.fileName()); // 输出文件名
}
//从闪存中删除file_name文件
if (SPIFFS.remove(file_name)){
Serial.print(file_name);
Serial.println(" remove sucess");
} else {
Serial.print(file_name);
Serial.println(" remove fail");
}
通过ArduinoIDE向开发板传文件,参考【Arduino IDE 2】Windows平台安装ESP8266 NodeMCU LittleFS Uploader(文件上传插件)。
如果一次没有成功多尝试几次,尝试时尽量复制、粘贴,不要自己手敲,还可以将波特率改小些。