Hello,
I have problem for Arduino MKR 1010 WiFi connect Node.js server using the example "SimpleWebSocket".
Code:
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
char ssid[] = "SSID"; // your network SSID (name)
char pass[] = "PASSWORD"; // your network password (use for WPA, or use as key for WEP)
char serverAddress[] = "192.168.0.111"; // server address
int port = 3000;
WiFiClient wifi;
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int count = 0;
void setup() {
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// 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);
}
void loop() {
Serial.println("starting WebSocket client");
client.begin();
while (client.connected()) {
Serial.print("Sending hello ");
Serial.println(count);
// send a hello #
client.beginMessage(TYPE_TEXT);
client.print("hello ");
client.print(count);
client.endMessage();
// increment count for next message
count++;
// check if a message is available to be received
int messageSize = client.parseMessage();
if (messageSize > 0) {
Serial.println("Received a message:");
Serial.println(client.readString());
}
// wait 5 seconds
delay(5000);
}
Serial.println("disconnected");
}
Output:
SSID: SSID
IP Address: 192.168.0.111
starting WebSocket client
disconnected