Problem with arduino mkr1010 and erratic connection loss


#1

Hi,
I have been having problems for a while now with connections to ubidots being disconnected and unable to reconnect unless I restart my mkr1010. The time that a connection lasts varies from minutes to hours but eventually within a 24 hr period it will not connect again. Any guidance would help please.
I am.using industrial.ubidots connection and have tried SSL and normal port 80 connections with both giving similar failures. I am connecting via wifi and send data every 2 seconds or so.


#2

Greetings, if the device is able to connect to the cloud after a reset the issue should be related with your firmware rather than with Ubidots. My advise is to adapt the basic http client example of the board to send data to Ubidots in order to avoid any memory issue in your routine. You can reference some examples of http client at our docs too.

All the best


#3

Thank you for the feedback , i will give that a try , basically i discovered this morning that code gets stuck on the lines of connecting to client which is the standard ubidots code.
if (client.connect(SERVER, HTTPPORT)). it stays here waiting for connection.
I also noticed time taken to establish connection on SSL option is approx 5 seconds while on normal port 80 is 300ms


#4
void sendData(char* payload) 
{
  int contentLength = strlen(payload);
  if (client.connect(SERVER, HTTPPORT)) {
    Serial.println("connected to server");
    client.print(F("POST "));
    client.print(PATH);    
    client.print(DEVICE_LABEL); 
    client.print(F("/"));
    client.print(HTTP_VERSION);
    client.print(F("Host: "));
    client.print(SERVER);
    client.print(F("\r\n"));   
    client.print(F("User-Agent: "));
    client.print(AGENT);
    client.print(F("\r\n"));
    client.print(F("X-Auth-Token: "));
    client.print(TOKEN);
    client.print(F("\r\n"));
    client.print(F("Connection: close\r\n"));
    client.print(F("Content-Type: application/json\r\n"));
    client.print(F("Content-Length: "));
    client.print(contentLength);
    client.print(F("\r\n\r\n"));
    client.print(payload);
    client.print(F("\r\n"));
    Serial.print(F("POST "));
    Serial.print(PATH);    
    Serial.print(DEVICE_LABEL); 
    Serial.print(F("/"));
    Serial.print(HTTP_VERSION);
    Serial.print(F("Host: "));
    Serial.print(SERVER);
    Serial.print(F("\r\n"));
    Serial.print(F("User-Agent: "));
    Serial.print(AGENT);
    Serial.print(F("\r\n"));
    Serial.print(F("X-Auth-Token: "));
    Serial.print(TOKEN);
    Serial.print(F("\r\n"));
    Serial.print(F("Connection: close\r\n"));
    Serial.print(F("Content-Type: application/json\r\n"));
    Serial.print(F("Content-Length: "));
    Serial.print(contentLength);
    Serial.print(F("\r\n\r\n"));
    Serial.print(payload);
    Serial.print(F("\r\n"));

    waitServer();
    getResponseServer();
  }
}

#5

Hi there, the examples of the docs were tested using an Ethernet board, so I am not sure if it will work without issues using another board. My advise is to adapt the basic Arduino example that I previous shared with you.

This is true and it is not something related with Ubidots but with the TLS protocol, keep in mind that you are using a microcontroller and to handle a secure connection demands processing time. Also,using HTTPs will derive in a higher power consumption of your device.

All the best


#6

Ok thanks , but the one I am using is specifically for mkr1010 from github of ubidots .
The two units I used last night failed but at different times , thisnis so weird and have tried to change my send void and even if I check for client connect and if not connected . Do a client.stop then do client connect again I cannot get it to reconnect. Is there a way to flush memory by using a command ?


#7

Hi there,

Can you let me know from where did you get this library? We do not have officially any library that supports that board.

If you mean to free variables memory, you may use the delete and free() commands from C++; if you mean to flush the tcp client buffer you may use the flush() method.

All the best


#8

HI,
used this code

thanks for other help will try that.


#9

i have got two units running and both fail at different times, i have even given them different tokens to see if that was related somehow, once the unit fails and client connection is lost there is no way to recover seems only way is to reboot. so i feel it could be some memory issue? have added the flush() as well as free() to one of the units and see what happens.

if (client.connect(SERVER, HTTPPORT))
{
timercomplete = millis();
connectiontime = timercomplete - timerstart ;
Serial.println(“Connected Succesfully…”);
Serial.print(“Time Taken : “);
Serial.print(connectiontime);
Serial.print(” ms”);
timercomplete = 0;
timerstart = 0;
sendData(payload);
blinkokled(1, 250);
previousmodbusqueryMillis = currentmodbusqueryMillis;
}
else
{
clientconnectioncnt = clientconnectioncnt + 1;
Serial.println(“Failure connecting to HTTP Server”);
Serial.print("Number of client reconnections : ");
Serial.print(clientconnectioncnt);
Serial.println(“Stoping Client connection…”);
client.stop();
Serial.println(“Client Disconnected”);
client.flush();
Serial.println(“Reconnecting Wifi…”);
connectwifi();
blinkstatusled(5, 100);
Serial.println(“Trying to Reconnect to client again…”);
client.connect(SERVER, HTTPPORT);

}

#10

now im not sure memory has anything to do with it , you can see by the free ram which does not change

wifi status : 3

NTP Server Time : 15:51:15

signal strength (RSSI):-49 dBm
Free RAM = 22947
Connecting to Cloud client…
Connected Succesfully…
Time Taken : 443 ms
Sending Data…
Data Sent


#11

Hi there, just to clarify, you are following a raw example but that is not a library developed by us. Said this, please allow me to make some tests to the script in order to debug any issue.

All the best


#12

no problem at all , fully understand that and the liability of using it as well, would be interested to know the outcome because i have exhausted all options and now going back to esp32 for now .


#13

Hi, I have the same problem since a month ago. I have three MKR1010 running with ubidots with that problem, when start the connection via WiFi everything is Ok sending data to Ubidots, but after minutes or hours (up to 12 hrs, it’s unpredictable) the device stops sending data and need to restart in order to reconnect and send data again (in my case is unplug and plug to current) but it is an annoying activity. I think the problem is related to the mkr1010 but not sure if it’s firmware, memory or a timer in code. Until now I have time to try to solve this, so available to share some news.

Regards


#14

Oh good to see I am not the only one , I tried everything and as far as I can debug in my ability I could not see why. Tried different access points also , tried LTE verse adsl … even wifi disconnects and reconnects and it will not client connect http until I reboot it. I have changed back to esp32 for now.


#15

Hi there @Motiontronic @CodeKobe, after a review of the example in the help center, we noticed that the socket opened by the Arduino was not being closed, due to this, in some moment the available number of sockets is taken by the script and the device is not able to open a new connection again. This is solved just invoking the method stop() from the TCP client instance. Please just copy and paste the updated code from the article.

Thanks for the report of this issue.

All the best


#16

Thanks . I eventually moved to esp32 and works well. Maybe I will in the future move back to mkr sam d processor.