need help. I have this code, which when using wifimanager to connect to the wifi network, it provides me with two more options to save the iot's ID and KEY directly into memory.
At first, when I log in and configure it for the first time in wifimanager, the esp8266 connects normally to the iotcloud server. however, when I reset, it is to read from memory and use the credentials. The esp8266 loads from memory, everything is correct, but it does not connect to the server. help!!!
#include <ESP8266WiFi.h> // Biblioteca para ESP8266
#include <FS.h>
#include <Arduino.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <WiFiManager.h>
#define Restap 16
void resetwifi();
bool verificarArquivo();
String dadosLidos1 ;
String dadosLidos2 ;
String SSID = "";
String PASS = "";
String DEVICE_KEY;
String DEVICE_LOGIN_NAME;
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID.c_str(), PASS.c_str());
void carregarConfiguracoes();
void conectwifi();
void conectwifi2();
void impressao();
void initProperties(){
String DEVICE_KEY = String (dadosLidos1.c_str());
String DEVICE_LOGIN_NAME = String (dadosLidos2.c_str());
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
Serial.println("passou ArduinoCloud.begin ");
}
void setup() {
Serial.begin(9600);
if (SPIFFS.begin()) {
Serial.println("Sistema de arquivos SPIFFS inicializado com sucesso.");
} else {
Serial.println("Falha ao montar o sistema de arquivos SPIFFS. Tentando formatar...");
ESP.restart();
}
if (verificarArquivo() == true) {
Serial.println("TEM GRAVADOS");
carregarConfiguracoes()
conectwifi2();
initProperties();
} else {
Serial.println("NAO TEM GRAVADOS");
conectwifi();
initProperties();
}
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
delay(5000);
Serial.println("saio setup");
}
//========================================
void loop() {
ArduinoCloud.update();
} //FIM DO LOOP
bool verificarArquivo() {
return SPIFFS.exists("/dados.txt");
}
//------------------------------------------
void carregarConfiguracoes() {
Serial.println("carregarConfiguracoes");
File file = SPIFFS.open("/dados.txt", "r");
if (!file) {
Serial.println("Erro ao abrir o arquivo 'dados.txt'");
return;
}
while (file.available()) {
String line = file.readStringUntil('\n');
if (line.startsWith("Chave: ")) {
dadosLidos1 = line.substring(7);
} else if (line.startsWith("ID: ")) {
dadosLidos2 = line.substring(4);
}
}
file.close();
Serial.println("Configurações carregadas com sucesso");
}
void salvarDados() {
// Abre o arquivo "dados.txt" em modo de escrita
File file = SPIFFS.open("/dados.txt", "w");
// Verifica se o arquivo foi aberto corretamente
if (!file) {
Serial.println("Erro ao abrir o arquivo");
return;
}
// Escreve os dados no arquivo
file.println("Chave: " + dadosLidos1);
file.println("ID: " + dadosLidos2);
file.close();
Serial.println("Dados salvos com sucesso");
}
void conectwifi() {
Serial.println("conectwifi");
WiFiManager wm;
// Cria parâmetros personalizados para chave, ID e porta
WiFiManagerParameter custom_key("key", "Chave", dadosLidos1.c_str(), 40);
WiFiManagerParameter custom_id("id", "ID", dadosLidos2.c_str(), 40);
// Adiciona os parâmetros personalizados ao WiFiManager
wm.addParameter(&custom_key);
wm.addParameter(&custom_id);
wm.setConfigPortalTimeout(60);
bool res;
res = wm.autoConnect("Teste AP", ""); // AP protegido por senha
if (!res) {
Serial.println("Failed to connect");
ESP.restart();
} else {
Serial.println("connected...yeey :)");
dadosLidos1 = custom_key.getValue();
dadosLidos2 = custom_id.getValue();
Serial.print("Wifi usada: ");
Serial.println(WiFi.SSID());
Serial.print("Senha usada: ");
Serial.println(WiFi.psk());
if (carregarConfiguracoes()){
salvarDados();
}
}
}