- 利用本身ESP32,来建立服务器来进行通信,TCP连接,难在ESP32上面写出网页
- 利用云平台,来进行通信,复杂在树莓派写出网页()
我认为两个都需要验证一下,才能
太极创客
串口
#include <Arduino.h>
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello world!");
delay(1000);
}
点灯
#include <Arduino.h>
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
#define LED 2
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
Serial.println("Hello world!");
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH);
delay(500);
}
建立基本网络服务器
3-2-1 建立基本网络服务器 – 太极创客 (taichi-maker.com)
网络服务器有很多种类型,它们的功能也十分丰富。通常承担网络服务器工作的设备都是运算能力比较强大的电脑。我们的ESP866-NodeMCU虽然也能实现网络服务器的一些功能,但是毕竟它的运算能力是无法与那些昂贵的服务器电脑相媲美的,因此ESP8266-NodeMCU只能实现一些基本的网络服务功能。不过这些基本的网络服务功能已经足够我们开发物联网项目了。在接下来的几节教程里,我们将一起来学习如何让ESP8266-NodeMCU来实现网络服务功能。
网络服务是一个很宽泛的概念,我们在这里即将给您介绍的是网络服务中的网页服务功能。所谓网页服务就是专门用于网页浏览的服务。这个操作我相信所有看到这篇教程的朋友们都使用过,因为您现在正阅读的这篇教程就是通过网页服务传输到您面前的。
为了便于理解,我们一起回忆一下您在打开这篇教程曾经历了什么过程。首先,要想访问太极创客网站就要在浏览器地址栏输入太极创客的网站地址: www.taichi-maker.com。当您输入完地址并按下回车以后,浏览器会通过DNS服务查到太极创客网站服务器的IP地址。假设我们太极创客服务器地址为12.34.56.78。接下来浏览器就会向IP地址12.34.56.78的服务器发送http请求。当网站服务器收到了请求后,会把被请求的网页信息传输给浏览器,然后浏览器就会把收到的网页信息转换成网页显示在浏览器中。
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
void handleRoot();
void handleNotFound();
const char* ssid = "ikun"; //WiFi 名称
const char* password = "123456789"; //WiFi 密码
WebServer server(80); //服务器端口
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //连接到WiFi
while(WiFi.status() !=WL_CONNECTED){ //检查WiFi是否连接
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //esp的IP地址
server.begin();
server.on("/", handleRoot); //当访问服务器首页时,采用handleRoot函数处理
server.onNotFound(handleNotFound); //如果访问对象不存在时,采用handleNotFound函数处理
Serial.print("HTTP server started");
}
void loop(void){
server.handleClient(); //连接服务器
}
void handleRoot(){
//服务器首页界面,200表示访问成功,第二个参数表示文本类型,第三个参数是服务器内容
server.send(200, "text/plain", "Hello form esp32. And this is my test");
}
void handleNotFound(){
//访问对象不存在时,404表示不存在对象,
server.send(404, "text/plain", "404: Not found");
}
通过网络服务实现ESP32开发板基本控制
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
void handleRoot();
void handleNotFound();
void handleLED();
const char* ssid = "ikun"; //WiFi 名称
const char* password = "123456789"; //WiFi 密码
WebServer server(80); //服务器端口
#define LED 2
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //连接到WiFi
pinMode(LED, OUTPUT); //设置内置LED引脚为输出模式以便控制LED
while(WiFi.status() !=WL_CONNECTED){ //检查WiFi是否连接
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //esp的IP地址
server.begin();
server.on("/", HTTP_GET, handleRoot); // 设置服务器根目录即'/'的函数'handleRoot'
server.on("/LED", HTTP_POST, handleLED); // 设置处理LED控制请求的函数'handleLED'
server.onNotFound(handleNotFound); // 设置处理404情况的函数'handleNotFound'
Serial.print("HTTP server started");
}
void loop(void){
server.handleClient(); //连接服务器
}
void handleRoot(){
server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
}
void handleNotFound(){
//访问对象不存在时,404表示不存在对象,
server.send(404, "text/plain", "404: Not found");
}
void handleLED(){
digitalWrite(LED,!digitalRead(LED));// 改变LED的点亮或者熄灭状态
server.sendHeader("Location","/"); // 跳转回页面根目录
server.send(303); // 发送Http相应代码303 跳转
}
2024年3月19日测试成功
通过网络服务将开发板引脚状态显示在网页中
3-2-3 通过网络服务将开发板引脚状态显示在网页中 – 太极创客 (taichi-maker.com)
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
void handleRoot();
void handleNotFound();
const char* ssid = "ikun"; //WiFi 名称
const char* password = "123456789"; //WiFi 密码
#define buttonPin 0 // 按钮引脚D3
bool pinState; // 存储引脚状态用变量
WebServer server(80); //服务器端口
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //连接到WiFi
while(WiFi.status() !=WL_CONNECTED){ //检查WiFi是否连接
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //esp的IP地址
server.begin();
server.on("/", handleRoot); //当访问服务器首页时,采用handleRoot函数处理
server.onNotFound(handleNotFound); //如果访问对象不存在时,采用handleNotFound函数处理
Serial.print("HTTP server started");
}
void loop(){
server.handleClient(); // 处理http服务器访问
pinState = digitalRead(buttonPin); // 获取引脚状态
}
/* 以下函数处理网站首页的访问请求。此函数为本示例程序重点1
详细讲解请参见太极创客网站《零基础入门学用物联网》
第3章-第2节“通过网络服务将开发板引脚状态显示在网页中”的说明讲解。*/
void handleRoot() {
String displayPinState; // 存储按键状态的字符串变量
if(pinState == HIGH){ // 当按键引脚D3为高电平
displayPinState = "Button State: HIGH"; // 字符串赋值高电平信息
} else { // 当按键引脚D3为低电平
displayPinState = "Button State: LOW"; // 字符串赋值低电平信息
}
server.send(200, "text/plain", displayPinState);
// 向浏览器发送按键状态信息
}
// 设置处理404情况的函数'handleNotFound'
void handleNotFound(){ // 当浏览器请求的网络资源无法在服务器找到时,
server.send(404, "text/plain", "404: Not found"); // NodeMCU将调用此函数。
}
按下后,
不需要闪存在文件系统
文件系统
//***************************************************************/
//
// -------------------------------------------
// board: Espressif ESP32 Dev Moduie
// Flash Mode: "DIO"
// -------------------------------------------
//
//***************************************************************/
#include <Arduino.h>
//引用相关库
//引用相关库
#include "FS.h"
#include "SPIFFS.h"
void setup()
{
Serial.begin(115200);
Serial.println();
//挂载文件系统
if (SPIFFS.begin(true))
{
Serial.println("SPIFFS file system successfully mounted!");
}
//打开/建立 并写入数据
File file = SPIFFS.open("/test.txt", FILE_WRITE);
if (file)
{
Serial.println("Open/create the test.txt file in the root directory!");
}
char data[] = "hello world\r\n";
file.write((uint8_t *)data, strlen(data));
file.close();
//重命名文件
if (SPIFFS.rename("/test.txt", "/retest.txt"))
{
Serial.println("Renaming test.txt to retest.txt!");
}
//读取文件数据
file = SPIFFS.open("/retest.txt", FILE_READ);
if (file)
{
Serial.print("The file content is:");
while (file.available())
{
Serial.print((char)file.read());
}
}
//打印SPIFFS文件系统信息
Serial.printf("The total size of the SPIFFS file system is: %d (Bytes)\n", SPIFFS.totalBytes());
Serial.printf("The used size of the SPIFFS file system is: %d (Bytes)\n", SPIFFS.usedBytes());
}
void loop()
{
}
标签:void,WiFi,server,println,README,服务器,Serial
From: https://www.cnblogs.com/hongpeiyu/p/18095193