5

I use this code to connect Arduino MKR1000 to API through Web Socket, but it didn't connect to the server. Can any one help me to correct the code.

This is the Arduino code:

#include "Arduino.h"
#include <WiFi101.h>
#include <SPI.h>
#include <WebSocketClient.h>

char server[] = "192.168.2.128";
IPAddress server1(192, 168, 2, 128);
WebSocketClient webSocketClient;
WiFiClient client;

char path1[] = "/api/WebSocketTest";
int port1 = 8060;
char ssid[] = "**************";
char pass[] = "**************";

void setup() {
  pinMode(LDR, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(SWITCH, INPUT);
  Serial.begin(115200);
  delay(10);
  while (!Serial) {
  }

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

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

  Serial.println("");
  Serial.println("WiFi connected");
  printWifiStatus();
  delay(3000);
  Serial.println("setup done");
  delay(1000);


  //  webSocketClient.setDebug(&debug);
  webSocketClient.connect(server, port1, path1);
  if (webSocketClient.connected() == true) {
    Serial.println("Connected");
  }
  else {
    Serial.println("Not Connected");
  }
  webSocketClient.onMessage(onMessage);
  webSocketClient.onOpen(onOpen);
  webSocketClient.onError(onError);
}




void loop() {
  webSocketClient.monitor();
}




void onOpen(WebSocketClient webSocketClient) {
  Serial.println("EXAMPLE: onOpen()");
}



void onMessage(WebSocketClient webSocketClient, char* message) {
  Serial.print("Received: ");
  Serial.println(message);
}



void onError(WebSocketClient webSocketClient, char* message) {
  Serial.print("ERROR: ");
  Serial.println(message);
}




void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

And this is a part from the library used "WebSocketClient.h":

void WebSocketClient::connect(char hostname[], int port, char protocol[], char path[]) {
  _hostname = hostname;
  _port = port;
  _protocol = protocol;
  _path = path;
  _retryTimeout = millis();
  _canConnect = true;
}

Edit 1: The output:

Connecting to WiFi-Repeater1

WiFi connected
SSID: WiFi-Repeater1
IP Address: 192.168.2.147
signal strength (RSSI):-68 dBm
setup done
Not Connected
ERROR: Connection Failed!
ERROR: Connection Failed!
ERROR: Connection Failed!
ERROR: Connection Failed!
ERROR: Connection Failed!
ERROR: Connection Failed!
12
  • What output do you get from the printWifiStatus() function?
    – Majenko
    Commented Jul 13, 2016 at 12:32
  • 1
    i am connecting it to api, which is hosted from my laptop, which act as a server
    – MBS
    Commented Jul 13, 2016 at 12:43
  • 1
    as you see at the top of the code I define the ip address of the server, port number and the path
    – MBS
    Commented Jul 13, 2016 at 12:45
  • 1
    i try the same api with other applications and it works perfect
    – MBS
    Commented Jul 13, 2016 at 12:45
  • 1
    and i try it also with raspberry pi and it works 100%
    – MBS
    Commented Jul 13, 2016 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.