Wemos和Raspberry之间通过WiFi的慢速通信

2024-09-29 21:55:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有PTZ controller、Wemos D1 Mini(基于ESP8266-12F)和Raspberry,我想使用Wemos从PTZ读取数据,并通过wifi发送到Raspberry。这是我在RPi上的代码:

import socket
s = socket.socket()
# host = socket.gethostname()
host = '192.168.0.26'
port = 9999
s.connect((host, port))

try:
    while True:
        response1 = s.recv(1024).decode("utf-8")
        print(response1)

except KeyboardInterrupt:
    s.close()

还有我在Wemos上的代码:

#include "ESP8266WiFi.h"

int msg = 0;
String str = String(0,HEX);
bool startReading = false;
String command = "";

const char *ssid = "MyName";
const char *password = "MyPassword";

WiFiServer wifiServer(9999);

void setup() {

  Serial.begin(9600);
  Serial.setDebugOutput(true);

  for(uint8_t t = 4; t > 0; t--) {
      Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
      Serial.flush();
      delay(1000);
  } 
  delay(1000);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }

  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());

  wifiServer.begin();
}

void loop() {

  WiFiClient client = wifiServer.available();

  if (client) {

    while (client.connected()) {

      msg = Serial.read();
      if (msg != -1) {
        command = String(msg,HEX);
        client.print(command);
      }
      delay(1);
    }

    client.stop();
    Serial.println("Client disconnected");

  }
}

一切正常,从PTZ重新读取数据是立即的,但向RPi发送数据是缓慢的,我可以看到明显的延迟。缩短与路由器的距离并不能改善这种情况。我试过使用#include <WebSocketsServer.h>,但这个图书馆更糟。我的问题是如何提高沟通速度?将服务器放在rpi而不是wemos上会有帮助吗?还有其他适合我使用的图书馆吗


Tags: clienthoststringserialmsgsocketcommandwifi

热门问题