4

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!
VE7JRO
  • 2,497
  • 15
  • 24
  • 29
MBS
  • 171
  • 1
  • 8
  • What output do you get from the `printWifiStatus()` function? – Majenko Jul 13 '16 at 12:32
  • WiFi connected SSID: WiFi-Repeater1 IP Address: 192.168.2.147 signal strength (RSSI):-68 dBm – MBS Jul 13 '16 at 12:41
  • And to what are you connecting? – Majenko Jul 13 '16 at 12:41
  • the problem is how to enter hostname, port number, and path – MBS Jul 13 '16 at 12:42
  • No, the problem is you cannot connect to some unknown service on some unknown computer. You already specify the hostname, port number, etc. – Majenko Jul 13 '16 at 12:43
  • 1
    i am connecting it to api, which is hosted from my laptop, which act as a server – MBS Jul 13 '16 at 12:43
  • No, you aren't connecting. That's the problem. It says not connected. I would guess the problem is with the configuration of your laptop - server software configuration, firewall configuration, etc. – Majenko Jul 13 '16 at 12:44
  • 1
    as you see at the top of the code I define the ip address of the server, port number and the path – MBS Jul 13 '16 at 12:45
  • 1
    i try the same api with other applications and it works perfect – MBS Jul 13 '16 at 12:45
  • 1
    and i try it also with raspberry pi and it works 100% – MBS Jul 13 '16 at 12:46
  • had you specified this port number **8060** while creating the server on your laptop? – Vaibhav May 13 '19 at 09:30

0 Answers0