TL;DR Server works fine when arduino is connected to local wifi, doesn't when arduino is used as AP, need fix or to identify the problem
Hi, I've been running into some issues when using arduino MKR WiFi1010 as an access point, very rarely it won't let me connect neither my laptop nor my phone to the AP, but that's not a big issue, since after the connection to the AP it stays connected;
The real issue is trying to connect a client on said devices to the server running on the arduino.
Supposedly the server works fine since I have no problems connecting, sending and receiving messages when arduino is connected to the local wifi, but it runs into a number of issues when used as an AP, from the client not connecting to it for several attempts(sometimes i try for whole hours), excessive amounts of delay both when receiving and sending messages, to the complete loss of messages.
At first(last month) it did work without issues, then it simply stopped cooperating.
These problems happen both with my code and with the wifiNINA AP_SimpleWebServer example.
I've run into this issue with two separate arduino MKR WiFi1010, both equipped with MRK can shield and an adafruit AM2320 sensor, one arduino with WiFi101 / WiFiNINA Firmware version 1.5.0 and one with version 1.4.8
Maybe of note(?), I've alimented the arduino with the usb cable when using the code below, and with a DC power supply(tested with both 12V and 24V) when connected to the rest of the devices it communicates through can with.
Here's a version of my code(without the can and sensor stuff) that displays the problems mentioned above.
Of note, in my code I'm using a webSocket server library i got at GitHub - ocrdu/NINA-Websocket: A websocket library for the Arduino Nano 33 IoT and similar ones
#include <SPI.h>
#include <WiFiNINA.h>
#include <WebSocketServer.h>
#define WEBSOCKET_PORT 8080
#define SERVER_MESSAGE_FREQUENCY 2000
#define WIFI_MODE 0
char* ssid = "your_ssid";
char* pass = "your_pass";
WiFiServer socketServer(WEBSOCKET_PORT);
WebSocketServer webSocketServer;
WiFiClient client;
bool clientConnected = false;
int status = WL_IDLE_STATUS;
unsigned long timestamp = 0;
void setup() {
Serial.begin(9600);
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
return;
}
#if WIFI_MODE == 0
WiFi.beginAP(ssid, pass);
Serial.print(ssid);Serial.print(" ");Serial.println(pass);
while(1){
for (int i = 0; i < 10 && WiFi.status() != WL_AP_LISTENING; i++)
{
Serial.print(".");
delay(250);
}
if(WiFi.status() != WL_AP_LISTENING)
{
Serial.println("\nnot connected");
}
else
{
Serial.println("\nconnected");
break;
}
}
#else
WiFi.begin(ssid, pass);
while(1){
for (int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++)
{
Serial.print(".");
delay(250);
}
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("\nNo WiFi");
}
else
{
Serial.println("\nWiFi connected");
break;
}
}
#endif
//wait 10 seconds for connection
delay(10000);
socketServer.begin();
Serial.print("WebSockets Server Running and Ready");Serial.print("IP address: ");Serial.print(WiFi.localIP());Serial.print(", Port: ");Serial.println(WEBSOCKET_PORT);
}
void loop() {
delay(250);
#if WIFI_MODE == 0
if(status != WiFi.status())
{
status = WiFi.status();
if (status == WL_AP_CONNECTED)
Serial.println("Device connected to AP");
else
Serial.println("Device disconnected from AP");
}
#endif
if(!client.connected())
{
if(clientConnected)
{
clientConnected = false;
Serial.println("client disconnected");
}
client = socketServer.available();
delayMicroseconds(10);
if(!client)
return;
if (!webSocketServer.handshake(client))
{
Serial.println("Failed handshake");
return;
}
Serial.println("Handshake successfull");
clientConnected = true;
}
if(millis() - timestamp > SERVER_MESSAGE_FREQUENCY)
{
webSocketServer.sendData("eeeeeeeeeeeeeeeee");
Serial.println("sent message");
timestamp = millis();
}
String data = webSocketServer.getData();
Serial.print(data);
if(data == "a")
{
webSocketServer.sendData("aaaaaaaaaaaaaaaaaaaaaaa");
Serial.println(" Sent data");
}
}
Here's the barebone client i'm using to test it
<!DOCTYPE html>
<html>
<head>
<title>Client</title>
<style>
body{background-color: black;}
#title{font-size: 60px;color: aqua;display: flex;justify-content: center;margin-top: 2%;}
.container{width: 40%;display: flex;justify-content: center;margin-left: 30%; margin-top: 5%;}
#Connect{font-size: 20px; background-color: green; height: 40px; width: 150px;}
#Send{font-size: 20px; background-color: orange; height: 40px; width: 150px;}
#Disconnect{font-size: 20px; background-color: red; height: 40px; width: 150px;}
#MessageInput{width: 60%; display: flex; justify-content: center;margin-left: 30%; margin-top: 5%; background-color: darkgrey; font-size: 40px; margin-bottom: 5%}
#IpInput{width: 60%; display: flex; justify-content: center;margin-left: 30%; margin-top: 5%; background-color: darkgrey; font-size: 40px; margin-bottom: 5%}
</style>
</head>
<body onload='OnLoad()'>
<div id='title'>Client</div>
<div class='container'>
<div class='col-md-4'>
<input id='IpInput' name='IpInput' type='text' placeholder='Ip...' class='form-control input-md'>
<input id='MessageInput' name='MessageInput' type='text' placeholder='Message...' class='form-control input-md'>
</div>
<div class='col-md-8'>
<button id='Connect' name='Connect' class='btn btn-success' onclick=OnConnect()>Connect</button>
<button id='Send' name='Send' class='btn btn-danger' onclick=OnSend()>Send</button>
<button id='Disconnect' name='Disconnect' class='btn btn-danger' onclick=OnDisconnect()>Disconnect</button>
</div>
</div>
<script>
var url = 'ws://192.168.001.001:8080/';
var webSocket = new WebSocket(url);
webSocket.onmessage = (event) => {
console.log("onMessageeee:");
console.log(event.data);
};
webSocket.onclose = (event) => {
console.log("onClosingggg:");
};
function OnLoad()
{
console.info("e");
}
function OnConnect()
{
url = document.getElementById('IpInput').value;
url = 'ws://' + url + ':8080/';
console.log("on Connect to "+url);
if(webSocket.readyState != WebSocket.OPEN)
{
console.log("openingSocket... :");
webSocket = new WebSocket(url);
}
webSocket.onmessage = (event) => {
console.log(event.data);
};
return false;
}
function OnSend()
{
if(webSocket.readyState == WebSocket.CLOSED)
webSocket = new WebSocket(url);
commandLine = document.getElementById('MessageInput').value;
if(webSocket.readyState != WebSocket.OPEN)
{
webSocket.onopen = (event) =>
{
webSocket.send(commandLine);
};
}
else
webSocket.send(commandLine);
webSocket.onmessage = (event) => {
console.log(event.data);
};
return false;
}
function OnDisconnect()
{
console.log("on Disconnect");
if(webSocket.readyState == WebSocket.OPEN)
webSocket.close();
return false;
}
</script>
</body>
</html>
I need to fix this issue since i need it to work as an AP, or to know if somehow both my arduinos are defective or broke somewhere between when they worked and now, or even something else.
Thanks in advance.