Go Down

Topic: How to reconnect the MKR to WIFi (Read 7805 times) previous topic - next topic

D6equj5

@ D6equj5

Did you do the firmware update ?

That fixes almost all the connection issues.Even my BETA version MKR is exceptionally stable since the upgrade.



Excellent, thank you. No I did not "do the firmware upgrade" (presumably not a dance). 
I gave as much info, to go on, as was necessary. Usually I found giving lots of unnecessary code, distracts the helper into critisising the coding style and belittling the requester so I tend not to give code where not necessary.
Thanks again.
Thank you
D6

ballscrewbob

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Hoffmakl

I don't understand how you solve the problem.


I'm using:
  • - MKR1000 - ATMEL 2015 AMW25-MR510PB Rev.R3
  • - IDE 1.8.10.0
  • - WiFi101 Firemware WINC 1501 Model B (19.5.2)  - current available firmware


I have found that the status (status = WiFi.begin(ssid, pass);) always remains in the staus == WL_CONNECTED.
(as described by Aquaponics2016)

When I reboot my Router (WiFi - Access Point for the MKR1000) - the status is "WL_CONNECTED"  and will not changed to WL_CONNECT_FAILED, or WL_CONNECTION_LOST or WL_DISCONNECTED.

After the reboot of the router the WiFi connection will not be re-established.
Is that works as designed?
- no experience with C++, my first steps with MKR1000 -

iirArduino

Hi,
I'm having a very similar problem. I have 2 MKR Wifi 1010 and 2 ESP8266 communicating through my WiFi.
I have a Mosquitto MQTT broker in my Raspberry Pi to exchange messages between the MKRs and ESPs.
The 2 ESP8266 work without any problems, but the 2 MKRs get blocked after a few hours.
The MKRs are sometimes able to reconnect, but after some hours in the end they fail and get blocked. It's not anly that they don't reconnect, but the also stop working with the rest of the code.
I've updated the firmware in both MKRs, but no improvements.

Has anybody found a way to reconnect always?


Code: [Select]

#include <WiFiNINA.h>                          // For the ESP8266 I've used ESP8266WiFi.h
#include <PubSubClient.h>


const char* ssid = "CASA4";
const char* password = "mypassword";
const char* mqtt_server = "192.168.0.192";
const int LED_VERDE = 7;                          // Pin for LED green.
const int LED_ROJO = 8;                           // Pin for LED Red.
const int PULSADOR = 9;                          // Pin for the pushbutton.
unsigned long instante = 0;                       // For the debounce.
boolean gatillo = LOW;
int veces = 0;                                    // To count the times I connect to the WiFi.
long lastReconnectAttempt = 0;

WiFiClient espClient;
PubSubClient client(espClient);                

char msg[50];                                     //************--------------do I need this? ***
String clientId = "mkr01";          

void basculo() {
  digitalWrite(LED_ROJO, gatillo);
  if (gatillo == HIGH) {
    client.publish("mkr01/pulsador", "1");       // Publish "1" in "mkr01/pulsador" topic
  }
  else {
    client.publish("mkr01/pulsador", "0");       // Publish "0" in "mkr01/pulsador" topic
  }
  Serial.println(gatillo);
  gatillo = !gatillo;
}

void setup_wifi() {                      // Connect to WiFi.
  delay(10);

  Serial.println();
  Serial.print("Conectando a ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {    
  Serial.print("Incoming message: [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the green LED if a 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(LED_VERDE, HIGH);
  }
  else {
    digitalWrite(LED_VERDE, LOW);
  }
}

boolean reconnect() {
  if (client.connect("mkr01")) {    
    // To see how many times do I re-connect:
    veces++;
    Serial.print("Veces re-conectado: ");            
    Serial.println(veces);                          

  // Once connected, publish an announcement...
    client.publish("mkr01/pulsador", "MKR01 connected ");
    // ... and resubscribe
    client.subscribe("+/pulsador");          // Subscribe to all topics that contain "pulsador".
  }
  return client.connected();
}


void setup() {

  pinMode(LED_VERDE, OUTPUT);
  pinMode(LED_ROJO, OUTPUT);
  pinMode(PULSADOR, INPUT_PULLUP);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  client.subscribe("+/pulsador");
}

void loop() {
    //  ***********This comes from the example 'mqtt_reconnect_nonblocking'
    if (!client.connected()) {
      long now = millis();
      if (now - lastReconnectAttempt > 5000) {
        lastReconnectAttempt = now;
        // Attempt to reconnect
    reconnect();
    if (reconnect()) {
          lastReconnectAttempt = 0;
        }
      }
    }


 client.loop();                                                    

  if (millis() > instante + 250) {                      // wait 250 ms for debounce.
    if (!digitalRead(PULSADOR)) {
      basculo();
    }
    instante = millis();
  }
}

gbafamily

Code: [Select]
if (client.connect("mkr01")) {

Every mqtt client must use a different/unique client name. mosquitto blocks clients with duplicate names. This is true for ESP and MKR.

iirArduino

Yes, this I know. I have different names: mkr01, mkr02, esp03 and esp04.

Go Up