ESP8266-01S WiFi 连接、串口控制和网络请求示例代码

本代码演示了如何使用 ESP8266-01S 连接 WiFi,并通过串口输入控制发送网络请求,以及如何定期获取网络时间并根据时间进行操作。

功能:

  1. 连接 WiFi,WiFi 名:'7-4',密码:'120712071207',并在循环中判断 WiFi 是否连接成功,失败则重新连接。
  2. 当串口检测到输入为 'X2' 或 'X3' 时,使用 GET 方式访问 https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=1%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4 或 https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=2%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4。
  3. 循环访问 http://tool.liumang.one/time/m.php 获取当前小时。
  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/m.php";
const String url1 = "https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=1%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4";
const String url2 = "https://xizhi.qqoq.net/XZ0a21dc90b486050f1bd29ff3d00b926d.send?title=2%E5%8F%B7%E7%82%B9%E6%BC%8F%E6%B0%B4";
const int checkInterval = 10000; // 检查WiFi连接状态的时间间隔
const int outputInterval = 3600000; // 输出"O"的时间间隔

unsigned long previousCheckTime = 0;
unsigned long previousOutputTime = 0;
bool wifiConnected = false;
int currentHour = -1;

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

  // 连接成功后,获取当前小时
  HTTPClient http;
  http.begin(server);
  int httpCode = http.GET();
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    currentHour = payload.toInt();
    Serial.print("Current hour: ");
    Serial.println(currentHour);
  }
  http.end();
}

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

  // 检查是否到了输出"O"的时间
  if (currentHour == 23) {
    unsigned long currentTime = millis();
    if (currentTime - previousOutputTime >= outputInterval) {
      Serial.println("O");
      previousOutputTime = currentTime;
    }
  }

  // 检查是否有串口输入
  if (Serial.available()) {
    String input = Serial.readStringUntil('
');
    if (input == "X2") {
      sendRequest(url1);
    } else if (input == "X3") {
      sendRequest(url2);
    }
  }

  // 检查WiFi连接状态
  unsigned long currentCheckTime = millis();
  if (currentCheckTime - previousCheckTime >= checkInterval) {
    previousCheckTime = currentCheckTime;
    if (WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi disconnected");
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
      }
      Serial.println("WiFi connected");
    }
  }
}

void sendRequest(String url) {
  HTTPClient http;
  http.begin(url);
  int httpCode = http.GET();
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    Serial.println(payload);
  }
  http.end();
}

说明:

  • 请确保已经安装了 ESP8266WiFi 库和 ESP8266HTTPClient 库。
  • 将代码中的 WiFi 名称和密码改为与你的网络相匹配。
  • 确保你的 ESP8266-01S 已正确连接到你的电脑,并通过串口监视器进行调试。

注意:

  • 代码中的 URL 地址为示例,请根据你的实际需求进行修改。
  • 该代码仅供参考,你可以根据自己的需要进行修改和扩展。
  • 建议在使用前进行充分测试,并确保代码的安全性。

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

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