Estou procurando uma forma de fazer o Arduino, MKR1000, reconectar automaticamente após a ocorrência de perda da conectividade.
O código abaixo é um dos contidos nos exemplos da IDE, apenas retirei as linhas relativas a conexão do setup e as coloquei em uma função, adicionado apenas um teste acerca do status. Entretanto não está funcionando, pois quando desligo o roteador e ligo-o novamente o Arduino não reconecta.
#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
}
void loop() {
Serial.print ("WiFi.status = " );
Serial.println (WiFi.status());
if (WiFi.status() != 3) {
conectar();
}
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void conectar()
{
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWiFiData();
}
void printWiFiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
Creio que necessito de uma comando para resetar ou encerrar qualquer resquício de antiga conexão.
Seu código possui vários Serial.print para informação e debug, então faça o seguinte:
Deixe o roteador ligado e funcionando, abra o terminal do Arduino e inicie o programa e deixe rodando por uns 5 minutos.
Depois, desligue o roteador, espere 1 minuto e ligue novamente.
Espere mais uns 5 minutos com o programa rodando.
Então copie e cole aqui o que apareceu no terminal do Arduino, para analisarmos.
Pode tentar colocar alguns Serial.print em outros lugares para obter mais informações.
WiFi.status = 0 Attempting to connect to WPA SSID: JASS Attempting to connect to WPA SSID: JASS You're connected to the networkSSID: JASS BSSID: 80:58:F8:3F:4B:DF signal strength (RSSI):-41 Encryption Type:2 IP Address: 192.168.43.161 192.168.43.161 MAC address: F8:F0:05:F7:E8:A9 SSID: JASS BSSID: 80:58:F8:3F:4B:DF signal strength (RSSI):-42 Encryption Type:2 WiFi.status = 3 SSID: JASS BSSID: 80:58:F8:3F:4B:DF signal strength (RSSI):-41 Encryption Type:2 WiFi.status = 3 SSID: JASS BSSID: 80:58:F8:3F:4B:DF signal strength (RSSI):-42 Encryption Type:2
Assim que eu desligo o roteador:
WiFi.status = 6 You're connected to the networkSSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 IP Address: 0.0.0.0 0.0.0.0 MAC address: F8:F0:05:F7:E8:A9 SSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2
Ligo o roteador e não reconecta:
You're connected to the networkSSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 IP Address: 0.0.0.0 0.0.0.0 MAC address: F8:F0:05:F7:E8:A9 SSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 WiFi.status = 6 You're connected to the networkSSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 IP Address: 0.0.0.0 0.0.0.0 MAC address: F8:F0:05:F7:E8:A9 SSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 WiFi.status = 6 You're connected to the networkSSID: BSSID: 00:00:00:00:00:00 signal strength (RSSI):-100 Encryption Type:2 IP Address: 0.0.0.0 0.0.0.0 MAC address: F8:F0:05:F7:E8:A9
Observe este trecho do texto gerado pelo terminal (que se repete várias vezes):
WiFi.status = 6
You're connected to the networkSSID:
Não é impresso o texto "Attempting to connect to WPA SSID: ", pois o código não entra neste while:
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
Basicamente isso acontece pois você não atualiza a variável status em nenhuma parte do código antes de testar este while.
Para solucionar, exclua a variável global, excluindo esta linha:
int status = WL_IDLE_STATUS; // the WiFi radio's status
E mude esta parte:
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
para:
// attempt to connect to WiFi network:
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}