首页 > 其他分享 >README

README

时间:2024-03-25 20:00:39浏览次数:14  
标签:void WiFi server println README 服务器 Serial

  1. 利用本身ESP32,来建立服务器来进行通信,TCP连接,难在ESP32上面写出网页
  2. 利用云平台,来进行通信,复杂在树莓派写出网页()

我认为两个都需要验证一下,才能

img

太极创客

串口

#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请求。当网站服务器收到了请求后,会把被请求的网页信息传输给浏览器,然后浏览器就会把收到的网页信息转换成网页显示在浏览器中。

image-20240319183721555


//***************************************************************/ 
// 
//           -------------------------------------------
//           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");
}

image-20240319184534182

通过网络服务实现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日测试成功

image-20240319190827239

通过网络服务将开发板引脚状态显示在网页中

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将调用此函数。
}

image-20240319192332590

按下后,

image-20240319192319684


不需要闪存在文件系统

文件系统



//***************************************************************/ 
// 
//           -------------------------------------------
//           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

相关文章

  • README
    目录volatile是一个类型修饰符,用于告诉编译器对象的值可能会在编译器无法检测到的情况下被改变。C++中的内联函数和宏的区别在于:内联函数是编译器在编译时将函数调用替换为函数体代码的一种优化方式,它保持了函数调用的语法并进行了类型安全检查;而宏是预处理器在编译前预处......
  • 一个现成的用python写的项目, 有GUI,https://github.com/mustafamerttunali/deep-learni
    安装该项目ENV:Win11Anaconda 1.安装Python3.7, 在Anaconda新建一个python3.7环境2.安装VC++buildtool14.0 以上版本,我从下面这个link下载的最新版是17.6.4https://visualstudio.microsoft.com/visual-cpp-build-tools/否则会遇到 3.修改一下requir......
  • harbor_readme
    一、私仓建立参考:步骤:https://blog.csdn.net/qwerty1372431588/article/details/113095997?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-5-113095997-blog-105926147.235v40pc_relevant_3m_sort_dl_base4&spm=1001.2101.3001.42......
  • mini-vocabook-rs README
    mini-vocabook-rsGitHub仓库地址:mini-vocabook-rsmini-vocabook-rs是一个基于Rust和Tauri的简易C/S架构单词本桌面应用,其在后端服务器采用RustAxum框架,在客户端组件库上采用Vuetify3及其MaterialDesign3设计语言。一、功能特性每日打卡:逐步学习目标单词本......
  • README
    工具介绍工具由来对于程序员等常常需要写文档的人来说,将本地markdown文档同步到云端博客平台,是一件比较繁琐的事情,首当其冲的是,大量的本地图片需要"互联网"化,即使网络上不乏有些工具能做到将图片自动上传到某些图床来解决这个问题,但是还是需要自己手动复制文档到对应的博......
  • 读程序员的README笔记19_读后总结与感想兼导读
    1. 基本信息程序员的README[美]克里斯·里科米尼(ChrisRiccomini)(美)德米特里·里人民邮电出版社,2023年7月出版1.1. 读薄率书籍总字数203千字,笔记总字数40984字。读薄率40984÷203000≈20.19%1.2. 读厚方向演进式架构SRE:Google运维解密软件设计的哲学......
  • 读程序员的README笔记17_构建可演进的架构(下)
    1. 可演进的API1.1. 随着需求的变化,你需要改变你的API,即代码之间的共享接口1.2. 改变API很容易,但很难做到正确1.3. 保持API小巧1.3.1. 小巧的API更易于理解和演进1.3.2. 只添加即刻需要的API方法或字段1.3.3. 带有许多字段的API方法应该有合理的默认值1.3.3.1. 开......
  • 读程序员的README笔记13_技术设计流程(上)
    1. 行为准则2. 设计过程的螺旋式上升2.1. 圆锥体中的箭头进一步螺旋式上升2.2. 你现在更确定你理解了问题空间2.3. 你的原型为你的解决方案提供了越来越多的信心2.4. 随着每一次迭代,设计文档变得更加清晰和详细3. 技术设计流程3.1. 当被要求对系统进行修改时,大......
  • 读程序员的README笔记12_On-Call
    1. 行为准则2. On-Call工程师2.1. On-Call工程师是应对计划外工作的第一道防线,无论是生产环境问题还是临时支持请求2.2. 将深度工作与运维工作分开,可以让团队中的大多数人专注于开发任务2.3. On-Call工程师只需专注于不可预知的运维难题和支持任务3. On-Call的工作方......
  • 读程序员的README笔记07_测试(下)
    1. 自己动手编写测试1.1. QA团队可以帮助你验证你的代码是否稳定,但千万不要把代码直接丢给他们,然后让他们做所有的测试1.2. 避免硬编码的值,不要重复代码1.3. 专注于测试基本功能而不是实现细节,这有助于代码库的重构1.3.1. 测试代码在重构后仍然可以运行1.4. 将测试的......