Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used this code to connect Arduino MKR1000 with API(server) using a websocket via WiFi.

 #include <WiFi101.h>
  #include <WebSocketClient.h>

  const char* ssid     = "********";
  const char* password = "********";
  char path[] = "/";
  char host[] = "wss://echo.websocket.org";

  WebSocketClient webSocketClient;

  WiFiClient client;

  void setup() {
    Serial.begin(115200);
    delay(10);


    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    delay(5000);


    // Connect to the websocket server
    if (client.connect("wss://echo.websocket.org", 443)) {
      Serial.println("Connected");
    } else {
      Serial.println("Connection failed.");
      while (1) {
        // Hang on failure
      }
    }

    // Handshake with the server
    webSocketClient.path = path;
    webSocketClient.host = host;
    if (webSocketClient.handshake(client)) {
      Serial.println("Handshake successful");
    } else {
      Serial.println("Handshake failed.");
      while (1) {
        // Hang on failure
      }
    }

  }


  void loop() {
    String data;

    if (client.connected()) {

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

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

      webSocketClient.sendData(data);

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

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

  }

And It always gives me this output:

   WiFi connected
   IP Address: 192.168.2.149
   Connected
   Waiting...
   Waiting...
   Handshake failed.
share|improve this question

You are using port 443 - so you should use connectSSL(). Also add the servers certificate to your mkr1000 as described on hackster.io/arichetta/add-ssl-certificates-to-mkr1000-93c89d

share|improve this answer
    
where to use it? Instead of what? – bilal1409 yesterday
    
Instead of client.connect("wss://echo.websocket.org", 443) – digipenKH yesterday
    
the same output – bilal1409 yesterday
    
Thats strange, possibly you also have to add the cert to your mrk1000 like desribed on: hackster.io/arichetta/add-ssl-certificates-to-mkr1000-93c89d – digipenKH 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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