1

I am working for arduino websocket client with CC3000 wifi board from adafruit. I have usde this websocket example as a reference and modified to work with cc3000 wifi board. I am not able to connect to echo.websocket.org. My code shows client is not connected and unavailable to communicate after sending the upgrade headers. Here is the serial monitor response for the code:

echo.websocket.org -> Errno: 1
174.129.224.73-------------------------------------
Client connected
Sending websocket upgrade headers
Analyzing response headers
client not connected
client not available

here is the Arduino sckech:

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <WebSocketClient.h>
#include "utility/debug.h"

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "UNSTOPPABLE"           // cannot be longer than 32 characters!
#define WLAN_PASS       "8878333344"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define IDLE_TIMEOUT_MS  3000      // Amount of time to wait (in milliseconds) with no data 
// received before closing the connection.  If you know the server
// you're accessing is quick to respond, you can reduce this value.

// What page to grab!
#define WEBSITE      "echo.websocket.org"
//#define WEBPAGE      "/testwifi/index.html"

// Here we define a maximum framelength to 64 bytes. Default is 256.
#define MAX_FRAME_LENGTH 64

// Define how many callback functions you have. Default is 1.
#define CALLBACK_FUNCTIONS 1



Adafruit_CC3000_Client www;
WebSocketClient webSocketClient;
/**************************************************************************/
/*!
 @brief  Sets up the HW and the CC3000 module (called automatically
 on startup)
 */
/**************************************************************************/

uint32_t ip;

void setup(void)
{
  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n")); 

  Serial.print("Free RAM: "); 
  Serial.println(getFreeRam(), DEC);

  /* Initialise the module */
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }

  // Optional SSID scan
  // listSSIDResults();

  Serial.print(F("\nAttempting to connect to ")); 
  Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  /* Display the IP address DNS, Gateway, etc. */
  while (! displayConnectionDetails()) {
    delay(1000);
  }

  ip = 0;
  // Try looking up the website's IP address
  Serial.print(WEBSITE); 
  Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }

  cc3000.printIPdotsRev(ip);

  // Optional: Do a ping test on the website
  /*
  Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");  
   replies = cc3000.ping(ip, 5);
   Serial.print(replies); Serial.println(F(" replies"));
   */

  /* Try connecting to the website.
   Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
   */
  www = cc3000.connectTCP(ip, 80);
  if (www.connected()) {
    /*    www.fastrprint(F("GET "));
     www.fastrprint(WEBPAGE);
     www.fastrprint(F(" HTTP/1.1\r\n"));
     www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
     www.fastrprint(F("\r\n"));
     www.println();*/
  } 
  else {
    Serial.println(F("Connection failed"));    
    return;
  }

  Serial.println(F("-------------------------------------"));

  /* Read data until either the connection is closed, or the idle timeout is reached. */
  /*  unsigned long lastRead = millis();
   while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
   while (www.available()) {
   char c = www.read();
   Serial.print(c);
   lastRead = millis();
   }
   }
   www.close();
   Serial.println(F("-------------------------------------"));*/

  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  /*  Serial.println(F("\n\nDisconnecting"));
   cc3000.disconnect();*/
  // Handshake with the server
  webSocketClient.path = "/";
  webSocketClient.host = WEBSITE;

  if (webSocketClient.handshake(www)) {
    Serial.println("Handshake successful");
  } 
  else {
    Serial.println("Handshake failed.");
    while(1) {
      // Hang on failure
    }  
  }  
}

void loop(void)
{
  String data; 
  if (www.connected()) {

    /*   data = webSocketClient.getData();

     if (data.length() > 0) {
     Serial.print("Received data: ");
     Serial.println(data);
     }*/

    // capture the value of analog 1, send it along
    pinMode(1, INPUT);
    data = String(analogRead(1));

    webSocketClient.sendData(data);

  } 
  else {

    Serial.println("Client disconnected.");
    while (1) {
      // Hang on disconnect.
    }
  }

  // wait to fully let the client disconnect
  delay(3000);
}

If anybody can guide me where I am wrong then it would be great help.I have taken reference code from brandenhall/Arduino-Websocket

5
  • Can you please add a wireshark sniff of what is going on?
    – Lesto
    Commented Jan 13, 2015 at 7:45
  • Thanks for reply I am not getting what to do with wireshark. In my case my client code is not communicating with echo server. As I have put debug print, it got disconnected from the client after sending upgrade header. Commented Jan 13, 2015 at 22:10
  • Then print back the response from the server. And also what you are sending. (That is what wireshark do)
    – Lesto
    Commented Jan 13, 2015 at 22:13
  • here the problem is i am not recieving anything from server. My client disconnected from the server after sending the Upgrade headers. that's why in wireshark i am not getting anything from server I think so. Commented Jan 14, 2015 at 7:32
  • Then post what YOU are sending!
    – Lesto
    Commented Jan 14, 2015 at 7:41

1 Answer 1

1

here the problem is i am not receiving anything from server. My client disconnected from the server after sending the Upgrade headers. that's why in wire-shark i am not getting anything from server I think so. here is code which do handshake with server.The problem is i am not receiving anything from server. My client disconnected from the server after sending the Upgrade headers. that's why in wire shark i am not getting anything from server I think so. here is code which do handshake with server:

bool WebSocketClient::analyzeRequest() { String temp; int bite; bool foundupgrade = false; unsigned long intkey[2]; String serverKey; char keyStart[17]; char b64Key[25]; String key = "------------------------"; randomSeed(analogRead(0)); for (int i=0; i<16; ++i) { keyStart[i] = (char)random(1, 256); } base64_encode(b64Key, keyStart, 16); for (int i=0; i<24; ++i) { key[i] = b64Key[i]; } Serial.println(F("Sending websocket upgrade headers")); socket_client->print(F("GET ")); socket_client->print(path); socket_client->print(F(" HTTP/1.1\r\n")); socket_client->print(F("Upgrade: websocket\r\n")); socket_client->print(F("Connection: Upgrade\r\n")); socket_client->print(F("Host: ")); socket_client->print(host); socket_client->print(CRLF); socket_client->print(F("Sec-WebSocket-Key: ")); socket_client->print(key); socket_client->print(CRLF); socket_client->print(F("Sec-WebSocket-Protocol: ")); socket_client->print(protocol); socket_client->print(CRLF); socket_client->print(F("Sec-WebSocket-Version: 13\r\n")); socket_client->print(CRLF); Serial.println(F("Analyzing response headers")); //here it got disconnected while (socket_client->connected() && !socket_client->available()) { delay(100); Serial.println("Waiting..."); } // TODO: More robust string extraction while ((bite = socket_client->read()) != -1) { temp += (char)bite; if ((char)bite == '\n') { #ifdef DEBUGGING Serial.print("Got Header: " + temp); #endif if (!foundupgrade && temp.startsWith("Upgrade: websocket")) { foundupgrade = true; } else if (temp.startsWith("Sec-WebSocket-Accept: ")) { serverKey = temp.substring(22,temp.length() - 2); // Don't save last CR+LF } temp = ""; } if (!socket_client->available()) { delay(20); } } key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; uint8_t *hash; char result[21]; char b64Result[30]; Sha1.init(); Sha1.print(key); hash = Sha1.result(); for (int i=0; i<20; ++i) { result[i] = (char)hash[i]; } result[20] = '\0'; base64_encode(b64Result, result, 20); // if the keys match, good to go return serverKey.equals(String(b64Result)); }

Not the answer you're looking for? Browse other questions tagged or ask your own question.