title: Message format websocket (Arduino + Esp8266)
body: <p><strong>Update</strong>
  Almost there I can receive messages I think. When the code is readable, I will put it in. Trying to send also..</p>
  
  <p><strong>Original question</strong></p>
  
  <p>I'm trying to connect my esp8266 (@38400 baud) ($3.50 wifi chip :)), to a Websocket. The chip is connected with a Arduino pro mini. This setup is OK and it works.</p>
  
  <p>I am able to do a handshake, thanks to some code (<a href="https://github.com/ejeklint/ArduinoWebsocketServer" rel="nofollow">https://github.com/ejeklint/ArduinoWebsocketServer</a>).</p>
  
  <p>So this is what the program has to do:</p>
  
  <ul>
  <li>Handle handshake            <strong>V</strong></li>
  <li>Receive message             <strong>Received some unknown chars</strong></li>
  <li>Sending (when I'm able to receive, I will find out how to send)</li>
  </ul>
  
  <p>I'm testing websocket with:
  <a href="http://www.websocket.org/echo.html" rel="nofollow">http://www.websocket.org/echo.html</a></p>
  
  <p>connecting with my wifi module
  ws://192.168.1.104:8000</p>
  
  <p>When I send 3 x the message "aaaa" to my Arduino I receive this:</p>
  
  <p>+IPD,0,10: |  |  | q |  | b | k |  | c |  |  |</p>
  
  <p>+IPD,0,10: |  |  | ¦ | ¡ | 0 | P | Ç | À | Q | 1 |</p>
  
  <p>+IPD,0,10: |  |  | _ | ò | ± | ? | > |  | Ð | ^ | |</p>
  
  <p>How can I decode this?</p>
  
  <pre><code>#include "sha1.h"
  #include "Base64.h"
  #include &lt;SoftwareSerial.h&gt;
  #include &lt;MemoryFree.h&gt;
  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 &lt; 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("&gt;");  // 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;
  }
  </code></pre>