ESP8266-01S WiFi 连接、HTTP 请求和串口通信示例代码

该示例代码实现了以下功能:

  1. 连接指定 WiFi 网络 (SSID: '7-4', 密码: '120712071207'),并判断连接状态,断开则重新连接。
  2. 接收串口输入,当输入为 'X2' 或 'X3' 时,使用 GET 方式访问指定的 HTTPS URL 发送请求。
  3. 通过访问 'http://tool.liumang.one/time/h.php' 获取当前小时,并判断是否为 23 点。
  4. 如果当前小时为 23 点,则向串口输出两次 'O',且在整个 23 点时间内只输出两次。

代码示例

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "7-4";
const char* password = "120712071207";
const char* server = "http://tool.liumang.one/time/h.php";
const char* endpoint1 = "https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=1%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4";
const char* endpoint2 = "https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=2%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4";

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("WiFi connected");
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi connection lost. Reconnecting...");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
      Serial.println("Connecting to WiFi...");
    }
    Serial.println("WiFi connected");
  }
  
  if (Serial.available()) {
    String input = Serial.readStringUntil('\n');
    if (input.equals("X2") || input.equals("X3")) {
      if (input.equals("X2")) {
        sendRequest(endpoint1);
      } else if (input.equals("X3")) {
        sendRequest(endpoint2);
      }
    }
  }
  
  String currentTime = getTime();
  if (currentTime.equals("23")) {
    static bool outputFlag = false;
    if (!outputFlag) {
      Serial.println("O");
      outputFlag = true;
    }
  } else {
    static bool outputFlag = false;
    outputFlag = false;
  }
  
  delay(1000);
}

void sendRequest(const char* endpoint) {
  HTTPClient http;
  http.begin(endpoint);
  int httpResponseCode = http.GET();
  if (httpResponseCode == 200) {
    String response = http.getString();
    Serial.println(response);
  } else {
    Serial.print("Error: ");
    Serial.println(httpResponseCode);
  }
  http.end();
}

String getTime() {
  HTTPClient http;
  http.begin(server);
  int httpResponseCode = http.GET();
  String currentTime = "";
  if (httpResponseCode == 200) {
    String response = http.getString();
    int start = response.indexOf("<h1>") + 4;
    int end = response.indexOf("</h1>");
    currentTime = response.substring(start, end);
  } else {
    Serial.print("Error: ");
    Serial.println(httpResponseCode);
  }
  http.end();
  return currentTime;
}

代码说明

  • setup() 函数用于初始化串口和连接 WiFi 网络。
  • loop() 函数用于循环执行以下操作:
    • 判断 WiFi 连接状态,如果断开则重新连接。
    • 接收串口输入,并根据输入内容发送 HTTP 请求。
    • 获取当前小时,并判断是否为 23 点。
    • 如果当前小时为 23 点,则向串口输出 'O'。
  • sendRequest() 函数用于发送 HTTP GET 请求。
  • getTime() 函数用于获取当前小时。

注意

  • 该代码仅供参考,具体实现细节可能需要根据实际情况进行调整。
  • 由于 ESP8266 资源有限,建议将 endpoint1endpoint2 改为更简洁的 URL,以便节省内存和处理时间。
  • 代码中的 <h1></h1> 是用来提取网页源代码中的小时数字的标记,如果网站结构发生变化,则需要修改代码以适应新的结构。

总结

该示例代码演示了 ESP8266-01S 如何连接 WiFi 网络、发送 HTTP 请求以及接收串口数据并根据数据内容进行相应操作。希望这篇文章可以帮助你更好地理解 ESP8266 的应用。


原文地址: https://www.cveoy.top/t/topic/pmrZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录