esp32做AP,ESP8266做tation,TCP遥控

 

 

 

//ESP32
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>


// AP模式配置(接收端自己创建热点)
const char *ap_ssid = "ESP8266_Remote"; // 热点的名称
const char *ap_password = "12345678";   // 热点的密码,至少8位

// TCP服务器配置
WiFiServer server(8888); // 在8888端口创建TCP服务器
WiFiClient client;

// 硬件配置
#define LED_PIN 2 // 板载LED或外接LED

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  delay(10);

  Serial.println();
  Serial.println("Setting up AP (Access Point)...");

  // 启动AP模式
  WiFi.softAP(ap_ssid, ap_password);

  // 获取并打印AP的IP地址(非常重要!遥控端需要连接这个IP)
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  // 启动TCP服务器
  server.begin();
  Serial.println("TCP server started on port 8888");
  Serial.println("Wait for remote to connect...");
}

void loop() {
  // 检查是否有新的客户端连接
  if (server.hasClient()) {
    // 如果有新客户端,检查当前客户端是否仍连接
    if (!client || !client.connected()) {
      if (client) client.stop(); // 断开之前的连接
      client = server.available(); // 接受新连接
      Serial.println("New remote connected!");
    }
  }

  // 检查已连接的客户端是否有数据可读
  if (client && client.connected()) {
    if (client.available()) {
      String command = client.readStringUntil('\n'); // 读取直到换行符
      command.trim(); // 去除多余空白字符

      Serial.print("Received command: ");
      Serial.println(command);

      // 解析并执行指令
      if (command == "LED_ON") {
        digitalWrite(LED_PIN, HIGH);
        client.println("LED is now ON");
        Serial.println("LED turned ON");
      } 
      else if (command == "LED_OFF") {
        digitalWrite(LED_PIN, LOW);
        client.println("LED is now OFF");
        Serial.println("LED turned OFF");
      }
      else if (command == "LED_TOGGLE") {
        digitalWrite(LED_PIN, !digitalRead(LED_PIN));
        client.println("LED toggled");
        Serial.println("LED toggled");
      }
      else if (command == "STATUS") {
        client.println(digitalRead(LED_PIN) ? "LED_STATUS:ON" : "LED_STATUS:OFF");
      }
      else {
        client.println("ERROR: Unknown command");
      }
    }
  }
  
  delay(10); // 短暂延迟
}

  

 

 

#include <ESP8266WiFi.h>

// 要连接的AP信息(必须与接收端设置的一致)
const char* ssid = "ESP8266_Remote";
const char* password = "12345678";

// 服务器信息(接收端的IP和端口)
// AP模式的默认IP通常是 192.168.4.1
const char* host = "192.168.4.1";
const int port = 8888;

// 硬件配置
#define BUTTON_PIN D3 // 按键引脚
#define BTN1_PIN D4
#define BTN2_PIN D5
WiFiClient client; void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BTN1_PIN,INPUT_PULLUP);
pinMode(BTN2_PIN,INPUT_PULLUP); delay(10); Serial.println(); Serial.print("Connecting to AP: "); Serial.println(ssid); // 连接到接收端创建的热点 WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.print("Local IP: "); Serial.println(WiFi.localIP()); // 连接到接收端的TCP服务器 Serial.print("Connecting to server: "); Serial.println(host); if (!client.connect(host, port)) { Serial.println("Connection to server failed!"); return; } Serial.println("Connected to server successfully!"); } void loop() { // 保持与服务器的连接 if (!client.connected()) { Serial.println("Connection lost. Reconnecting..."); if (client.connect(host, port)) { Serial.println("Reconnected!"); } else { delay(2000); return; } } // 检测按键按下 if (digitalRead(BUTTON_PIN) == LOW) { delay(50); // 消抖 if (digitalRead(BUTTON_PIN) == LOW) { Serial.println("Button pressed! Sending TOGGLE command."); // 发送指令到服务器 client.println("LED_TOGGLE"); // 等待并读取服务器响应(可选) delay(100); while (client.available()) { String line = client.readStringUntil('\n'); Serial.print("Server response: "); Serial.println(line); } // 等待按键释放 while (digitalRead(BUTTON_PIN) == LOW) { delay(10); } } } if(digitalRead(BTN1_PIN)==LOW) client.println("CMD1"); delay(10); // 防止忙等 }

  

posted @ 2025-09-17 16:06  多多和羊羊  阅读(14)  评论(0)    收藏  举报