Code: Select all
#include <EEPROM.h>
#include <WiFi.h>
#include <BluetoothSerial.h>
#define EEPROM_SIZE 512
#define led_verde 5
#define led_verme 4
#define jumper 15
TaskHandle_t pisca_verde;
//TaskHandle_t inter_verde;
TaskHandle_t pisca_verme;
//TaskHandle_t inter_verme;
void pisca_verme_fun( void * parameter ) {
while(true) {
digitalWrite(led_verme,HIGH);
delay(500);
digitalWrite(led_verme,LOW);
delay(500);
}
}
void pisca_verde_fun( void * parameter ) {
while(true) {
digitalWrite(led_verde,HIGH);
delay(500);
digitalWrite(led_verde,LOW);
delay(500);
}
}
/*void inter_verme_fun( void * parameter ) {
while(true) {
digitalWrite(led_verme,HIGH);
}
}
void inter_verde_fun( void * parameter ) {
while(true) {
digitalWrite(led_verde,HIGH);
}
}*/
bool write_eeprom(int ini, String text) {
bool res = false;
int tam = text.length();
EEPROM.write(ini,tam);
if (ini >= 0 && tam < EEPROM_SIZE) {
for (int i = ini+1 ; i <= ini+tam ; i++) {
char c = text.charAt(i-1-ini);
EEPROM.write(i,c);
res = true;
}
}
return res;
}
String read_eeprom(int ini) {
String search;
if(ini >= 0 && ini < EEPROM_SIZE) {
int tam = EEPROM.read(ini);
for (int i = ini+1; i <= ini+tam; i++) {
int c = EEPROM.read(i);
search += char(c);
}
}
return search;
}
bool conecta_wifi_eeprom() {
digitalWrite(led_verde,LOW);
Serial.println("USER: "+read_eeprom(0));
Serial.println("PASS: "+read_eeprom(34));
int attem = 0;
String ssid_buff = read_eeprom(0);
String pass_buff = read_eeprom(34);
xTaskCreate (
pisca_verme_fun,
"pisca_led_verme",
10000,
NULL,
1,
&pisca_verme
);
while(attem < 5 && digitalRead(jumper) != 0) {
WiFi.disconnect();
WiFi.begin(ssid_buff.c_str(),pass_buff.c_str());
Serial.print("Conectando...");
int t = 0;
while(WiFi.status() != WL_CONNECTED && t < 75 && digitalRead(jumper) != 0) {
Serial.print(".");
delay(500);
t++;
}
if(WiFi.status() == WL_CONNECTED) {
Serial.println("Conectado!");
return true;
} else {
Serial.println("Erro na conexão! - "+String(WiFi.status())+" - "+String(attem));
attem = attem+1;
}
}
vTaskDelete(pisca_verme);
digitalWrite(led_verme,HIGH);
Serial.println("esperando");
while(digitalRead(jumper) != 0) {}
digitalWrite(led_verme,LOW);
ESP.restart();
}
bool config_bluet_eeprom() {
digitalWrite(led_verme,LOW);
BluetoothSerial ESP_BT;
ESP_BT.begin("AutoHouse - conf.");
Serial.println("BLE ligado");
xTaskCreate (
pisca_verde_fun,
"pisca_led_verde",
10000,
NULL,
1,
&pisca_verde
);
int buff;
for(int t = 0; t < 3001; t++) {
String text = "";
while(ESP_BT.available()) {
buff = ESP_BT.read();
text += char(buff);
delay(20);
}
if (text != "") {
Serial.println(text); // format: ssid;pass
ESP_BT.println(text);
int pos_corte = text.indexOf(";");
String ssid = text.substring(0,pos_corte);
String pass = text.substring(pos_corte+1,text.length()-1);
Serial.println(ssid);
Serial.println(pass);
write_eeprom(0,ssid);
write_eeprom(34,pass);
EEPROM.commit();
delay(100);
break;
}
delay(20);
}
Serial.println("esperando");
vTaskDelete(pisca_verde);
digitalWrite(led_verde,HIGH);
while(digitalRead(jumper) == 0) { delay(100); }
/*digitalWrite(led_verde,LOW);*/
ESP.restart();
}
void setup() {
pinMode(jumper,INPUT_PULLUP);
pinMode(led_verme,OUTPUT);
pinMode(led_verde,OUTPUT);
EEPROM.begin(EEPROM_SIZE);
Serial.begin(115200);
if(digitalRead(jumper) == 0) {
config_bluet_eeprom();
} else {
conecta_wifi_eeprom();
}
}
void loop() {
delay(1000);
}
Code: Select all
USER: ***********
PASS: *******
Conectando..............................................................................Erro na conexão! - 6 - 0
Conectando..............................................................................Erro na conexão! - 6 - 1
Conectando..............................................................................Erro na conexão! - 6 - 2
Conectando..............................................................................Erro na conexão! - 6 - 3
Conectando..............................................................................Erro na conexão! - 6 - 4
esperando