首页 > 解决方案 > 带有 W5500 以太网扩展板的 Arduino Uno 基本 WebSocket 服务器

问题描述

我对 Arduino 相当陌生,我正在尝试构建一个通用的 WebSocket 服务器,它将简单的 ASCII 值发送到通过以太网电缆连接到 Arduino 的 python 程序运行的 WebSocket 客户端。我不想构建一个完整的 WebServer,而我只想使用 WebSocket,以避免使用 HTTP。

我不确定应该为带有 Arduino Uno 的 W5500 防护罩使用哪个库。我也找不到任何关于如何在 Arduino 中构建基本 WebSocket 服务器的全面而简单的示例。任何人都可以为适当的库或好的示例代码提供一些建议吗?

提前致谢

编辑:这是我从 WebSockets_Generic.h 库中获取的通用 WebSocket 服务器的基本尝试,只是为了概述我想做的事情:

#include <Ethernet2.h>
#include <WebSockets_Generic.h>

WebSocketsServer webSocket = WebSocketsServer(81);

// Enter a MAC address and IP address for your controller below.

byte mac[] ={0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13};


// Select the IP address according to your local network
IPAddress ip(198, 162, 1, 177);

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
  switch (type)
  {
    case WStype_DISCONNECTED:
      //Serial.println( "[" + String(num) + "] Disconnected!");
      break;
    case WStype_CONNECTED:
      {
        Serial.println( "[" + String(num) + "] Connected!");
        //IPAddress ip = webSocket.remoteIP(num);
        //Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

        // send message to client
        webSocket.sendTXT(num, "Connected");
      }
      break;
    case WStype_TEXT:
      Serial.println( "[" + String(num) + "] get Text: " + String((char *) payload));

      // send message to client
      webSocket.sendTXT(num, "message here");

      // send data to all connected clients
       webSocket.broadcastTXT("message here");
      break;
    case WStype_BIN:
      Serial.println( "[" + String(num) + "] get binary length: " + String(length));
      
      //hexdump(payload, length);

      // send message to client
      // webSocket.sendBIN(num, payload, length);
      break;

      default:
        break;
  }
}

void setup()
{
  
  // Serial.begin(921600);
  Serial.begin(115200);
  while (!Serial);

  // start the ethernet connection and the server:
  // Use DHCP dynamic IP and random mac
  uint16_t index = millis();
  // Use Static IP
  //Ethernet.begin(mac[index], clientIP);
  Ethernet.begin(mac, ip);

  // server address, port and URL
  Serial.print("WebSockets Server @ IP address: ");
  Serial.println(Ethernet.localIP());
 
  webSocket.begin();
  webSocket.onEvent(webSocketEvent);
}

void loop()
{
  webSocket.loop();
}

这不会编译,而是会产生错误:

In file included from /home/user/Data/Code-pieces/Arduino-Flowmeter/test/test.ino:2:0:
/home/user/Arduino/libraries/WebSockets_Generic/src/:69:4: error: #error Version 2.x.x currently does not support Arduino with AVR since there is no support for std namespace of c++.
   #error Version 2.x.x currently does not support Arduino with AVR since there is no support for std namespace of c++.
    ^~~~~
/home/user/Arduino/libraries/WebSockets_Generic/src/WebSockets_Generic.h:70:4: error: #error Use Version 1.x.x. (ATmega branch)
   #error Use Version 1.x.x. (ATmega branch)
    ^~~~~
Multiple libraries were found for "Ethernet.h"
 Used: /home/user/Data/Code-pieces/arduino-1.8.14/libraries/Ethernet
 Not used: /home/user/Arduino/libraries/UIPEthernet
exit status 1
Error compiling for board Arduino Uno.

WebSockets_Generic.h
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

标签: websockettcparduinoethernet

解决方案


推荐阅读