2024-11-29 20:28:25 +00:00
|
|
|
#include <WiFi.h>
|
|
|
|
|
#include <HTTPClient.h>
|
|
|
|
|
#include "EepromService.h"
|
|
|
|
|
#include "SerialService.h"
|
|
|
|
|
|
|
|
|
|
#ifndef WifiService
|
|
|
|
|
#define WifiService
|
|
|
|
|
|
2024-12-03 16:19:16 +00:00
|
|
|
String WIFI_SSID = "Agrobot";
|
|
|
|
|
String WIFI_PASSWORD = "4321agro";
|
2024-11-29 20:28:25 +00:00
|
|
|
|
|
|
|
|
bool WifiIsConnected = false;
|
|
|
|
|
|
|
|
|
|
bool connectToWiFi() {
|
|
|
|
|
char ssid[MAX_SSID_LEN];
|
|
|
|
|
char password[MAX_PASS_LEN];
|
|
|
|
|
loadWiFiCredentials(ssid, password);
|
|
|
|
|
|
|
|
|
|
PrintTela("Conectando a ", false);
|
|
|
|
|
PrintTela(ssid);
|
|
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
int timeout = 10000; // Tempo máximo de espera
|
|
|
|
|
int startTime = millis();
|
|
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED && millis() - startTime < timeout) {
|
|
|
|
|
delay(500);
|
|
|
|
|
PrintTela(".", false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
|
PrintTela("\nWiFi conectado! IP: ", false);
|
|
|
|
|
PrintTela(WiFi.localIP().toString().c_str());
|
|
|
|
|
WifiIsConnected = true;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
PrintTela("\nFalha na conexão.");
|
|
|
|
|
WifiIsConnected = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InicializarConexaoWifi(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns) {
|
|
|
|
|
EEPROM.begin(EEPROM_SIZE); // Iniciar EEPROM com espaço para SSID e senha
|
|
|
|
|
|
|
|
|
|
// Carregar credenciais e tentar conectar
|
|
|
|
|
char ssid[MAX_SSID_LEN];
|
|
|
|
|
char password[MAX_PASS_LEN];
|
|
|
|
|
|
2024-12-03 16:19:16 +00:00
|
|
|
//saveWiFiCredentials(WIFI_SSID.c_str(), WIFI_PASSWORD.c_str());
|
|
|
|
|
|
2024-11-29 20:28:25 +00:00
|
|
|
loadWiFiCredentials(ssid, password);
|
|
|
|
|
|
|
|
|
|
PrintTela(ssid);
|
|
|
|
|
|
|
|
|
|
// Configurar IP fixo
|
|
|
|
|
if (!WiFi.config(ip, gateway, subnet, dns)) {
|
|
|
|
|
Serial.println("Falha ao configurar IP estático!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ssid[0] != '\0') { // Verificar se há SSID salvo
|
|
|
|
|
WifiIsConnected = connectToWiFi();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool verificaInternet() {
|
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
PrintTela("Wi-Fi não está conectado.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Teste de conexão com um servidor externo
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
if (client.connect("www.google.com", 80)) { // Tenta conectar ao Google na porta 80
|
|
|
|
|
client.stop(); // Fecha a conexão imediatamente após verificar
|
|
|
|
|
PrintTela("Conexão com a Internet ativa.");
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
PrintTela("Falha na conexão com a Internet.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|