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!
printWifiStatus()
function?