1

更新 几乎那里我可以收到我认为的消息。当代码可读时,我将其放入。尝试发送也..

原始问题

我正在尝试将我的 esp8266(@38400 波特)(3.50 美元 wifi 芯片 :))连接到 Websocket。该芯片与 Arduino pro mini 相连。此设置没问题,并且可以正常工作。

多亏了一些代码( https://github.com/ejeklint/ArduinoWebsocketServer),我能够握手。

所以这就是程序必须做的:

  • 处理握手 V
  • 接收消息 收到一些未知字符
  • 发送(当我能够接收时,我会知道如何发送)

我正在测试 websocket: http ://www.websocket.org/echo.html

连接我的wifi模块ws://192.168.1.104:8000

当我向我的 Arduino 发送 3 x 消息“aaaa”时,我收到:

+IPD,0,10: | | | 问 | | 乙 | ķ | | c | | |

+IPD,0,10: | | | ¦ | ¡ 0 | 磷 | Ç | À | 问 | 1 |

+IPD,0,10: | | | _ | Ø | ± | ? | > | | ? | ^ | |

我该如何解码?

#include "sha1.h"
#include "Base64.h"
#include <SoftwareSerial.h>
#include <MemoryFree.h>
SoftwareSerial debug(8, 9); // RX, TX

void setup() {
  Serial.begin(38400);
  debug.begin(38400);
  delay(50);
  debug.println("start");
  Serial.println("AT+RST");
  delay(5000);
  Serial.println("AT+CWMODE=1");  // NO CHANGE
  delay(1500);
  Serial.find("OK");
  Serial.println("AT+CIPMUX=1");
  Serial.find("OK");
  delay(3000);
  Serial.println("AT+CIPSERVER=1,8000");
  boolean server = Serial.find("OK");
  delay(3000);
  Serial.println("AT+CIFSR");   // Display the ip please
  boolean r = readLines(4);
  debug.println("eind setup");
  debug.println(server);
  boolean found = false;

  while(!found)   // wait for the link
    found = Serial.find("Link");
  debug.println("link builded, end setup");
}


void loop() {
  String key = "";
  boolean isKey = Serial.find("Key: ");
  if(isKey) {
    debug.println("Key found!");
      while(true) {
        if(Serial.available()) {
          char c = (char)Serial.read();
          if(c == '=') {
            doHandshake(key + "==");
            key = "";
            break;
          } 
          if(c != '\r' || c != '\n') {
            key = key + c;
          }
        }
      }
// _________________________ PROBLEMO ____________________________________
while(true) {  // So far so good. Handshake done Now wait for the message
         if(Serial.available()) {
          char c = (char)Serial.read();
          debug.print(c);
          debug.print(" | ");
         }
      }
  }
// _________________________ /PROBLEMO ____________________________________
}

boolean readLines(int lines) {
  boolean found = false;
  int count = 0;
  while(count < lines) {
    if(Serial.available()) {
      char c = (char)Serial.read();
      if(c != '\r') {
        debug.write(c);
      } else {
        count++;
      } 
    }
  }
  return true;
}

bool doHandshake(String k) {
    debug.println("do handshake: " + k);
    char bite;
    char temp[128];
    char key[80];
    memset(temp, '\0', sizeof(temp));
    memset(key, '\0', sizeof(key));

    byte counter = 0;
    int myCo = 0;
    while ((bite = k.charAt(myCo++)) != 0) {
      key[counter++] = bite;
    }
    strcat(key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); // Add the omni-valid GUID
    Sha1.init();
    Sha1.print(key);
    uint8_t *hash = Sha1.result();
    base64_encode(temp, (char*)hash, 20);
    debug.print(temp);

    int cc = -1;
    while(temp[cc++] != '\0') {} // cc is length return key
    cc = 165 + cc; // length return key + 165 keys for rest of header

    Serial.print("AT+CIPSEND=0,");
    Serial.println(129);  // +30   // was 129
    boolean found = false;
    while(!found)
      found = Serial.find(">");  // Wait until I can send
    Serial.print("HTTP/1.1 101 Switching Protocols\r\n");
    Serial.print("Upgrade: websocket\r\n");
    Serial.print("Connection: Upgrade\r\n");
    Serial.print("Sec-WebSocket-Accept: ");
    Serial.print(temp);
    Serial.print("\r\n\r\n");
    return true;
}
4

3 回答 3

1

我没有使用 websockets 的经验,但我认为 websockets 使用 UTF-8 而 Arduino 终端使用 ASCII。我没有看到您在 UTF-8 和 ASCII 之间的代码转换。

于 2014-11-11T14:35:56.797 回答
0

我现在可以从 websocket >> arduino 发送消息。但是发送不起作用:(。

boolean getFrame() {
    debug.println("getFrame()");
    byte bite;
    unsigned short payloadLength = 0;

    bite = Serial.read();        
    frame.opcode = bite & 0xf; // Opcode
    frame.isFinal = bite & 0x80; // Final frame?
    bite = Serial.read();
    frame.length = bite & 0x7f; // Length of payload        
    frame.isMasked = bite & 0x80;

    // Frame complete!
    if (!frame.isFinal) {
        return false;
    }
    // First check if the frame size is within our limits.
    if (frame.length > 126) {       
        return false;
    }

    // If the length part of the header is 126, it means it contains an extended length field.
    // Next two bytes contain the actual payload size, so we need to get the "true" length.
     if (frame.length == 126) {
        byte exLengthByte1 = Serial.read();
        byte exLengthByte2 = Serial.read();
        payloadLength = (exLengthByte1 << 8) + exLengthByte2;
     } 
     // If frame length is less than 126, that is the size of the payload.
     else {
        payloadLength = frame.length;        
     }

     // Check if our buffer can store the payload.
     if (payloadLength > MAX_RECEIVE_MESSAGE_SIZE) {
        debug.println("te groot");
        return false;
     }

    // Client should always send mask, but check just to be sure    
    if (frame.isMasked) {
        frame.mask[0] = Serial.read();
        frame.mask[1] = Serial.read();
        frame.mask[2] = Serial.read();
        frame.mask[3] = Serial.read();
    }

    // Get message bytes and unmask them if necessary
    for (int i = 0; i < payloadLength; i++) {
        if (frame.isMasked) {
            frame.data[i] = Serial.read() ^ frame.mask[i % 4];
        } else {
            frame.data[i] = Serial.read();
        }
    }

    for (int i = 0; i < payloadLength; i++) {
        debug.print(frame.data[i]);
        if(frame.data[i] == '/r')
          break;
    } 
    return true;
}

// !!!!!!!!!! NOT WORKING
boolean sendMessage(char *data, byte length) {    
      Serial.print((uint8_t) 0x1); // Txt frame opcode
      Serial.print((uint8_t) length); // Length of data
      for (int i = 0; i < length ; i++) {
          Serial.print(data[i]);
      }
      delay(1);
      return true;
}

https://github.com/zoutepopcorn/esp8266-Websocket/blob/master/arduino_websocket.ino

现在唯一的问题是来自 arduino > websocket 的 websocket 格式不正常 :(。但我认为这是另一个问题/问题。与 'ws://192.168.1.101:8000/?encoding=text' 的 WebSocket 连接失败:一个或更多保留位打开:reserved1 = 0、reserved2 = 1、reserved3 = 1

于 2014-11-20T09:46:32.120 回答
0

您正在查看的是与 HTTP 标头连接的第一个 Web Socket 帧 ( | | | q | | b | k | | c | | |)。(+IPD,0,10:) 用管道 (|) 分隔的数据难以理解,因为它不是 ASCII,也不是 UTF8。您必须将最后一个冒号 (:) 之后的数据显示为 BINARY。那么它应该是完全有意义的。我正在做同样的事情。只有当我将总数据显示为二进制时,我才“知道”。我正在使用网络上的“Web Sockets rock”演示。它只是将“Web Sockets rock”发送到您指定的服务器的回声。我将服务器地址更改为我的 ESP8266 的 IP 并开始查看帧。我为自己做了一点分析(和你做的一样),看看 ESP8266 在握手成功后会发回什么。

+IPD,0,21:r¨$v%ÍF%ËVÃW    (NOTE: Garbage after the :) 

我希望在那里的某个地方找到“Web Sockets rock”。

这是我从接收缓冲区中提取的转换为二进制的列表-

0 2B    0010 1011
1 49    0100 1001
2 50    0101 0000
3 44    0100 0100
4 2C    0010 1100
5 30    0011 0000
6 2C    0010 1100
7 32    0011 0010  (Ascii for 21 bytes to follow)
8 31    0011 0001  (Ascii for 21 bytes to follow)
9 3A    0011 1010  (Colon) 
10 -7F  1000 0001  (Start of actual FRAME) 
11 -71  1000 1111
12 72   0111 0010
13 -58  1010 1000
14 24   0010 0100
15 76   0111 0110
16 25   0010 0101
17 -33  1100 1101
18 46   0100 0110  
19 25   0010 0101
20 1D   0001 1101
21 -35  1100 1011
22 4F   0100 1111
23 13   0001 0011
24 6    0000 0110
25 -78  1000 1000
26 56   0101 0110
27 19   0001 1001
28 11   0001 0001
29 -3D  1100 0011
30 57   0101 0111

字段说明-(从冒号后的第一个字节开始。81)

// First byte has FIN bit and frame type opcode = text
// Second byte mask and payload length
// next four bytes for masking key
// So total of 6 bytes for the overhead
// The size of the payload in this case is "F" = 15  (the 4th nibble) 
// So total of bytes are (6+15) = 21
// The first  byte is saying> FIN bit is set. This is last frame in sequence. The OP code is 1 = TEXT data.
// The second byte is saying> MASK bit is set. The following data will be masked. The data length is "F" = 15
// The 3rd, 4th, 5th, 6th bytes is the masking key. In this case 72, A8, 24, 76.
于 2017-04-24T10:57:58.943 回答