Blynk esp8266 no inicia despues de corte de luz

Hola gente del foro, tengo el siguiente problema: cuando hay un corte de luz la placa se me desconecta de internet y tengo que resetearla para que se vuelva a conectar, el internet es por fibra optica y tarda bastante en conectar. Quisiera si me pueden orientar un poco. Muchas gracias. Saludos.

/**********************************************************************************
    TITLE:(IoT based Temperature Control System With ESP8266 + DS18B20 Dallas Temperature Sensor + 0.96 inch OLED Display
    + Auto and Manual Modes + Temperature Set Point and Hysteresis +  EEPROM + Real time feedback)
 **********************************************************************************/

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxx"

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxx";
char pass[] = "xxxxxxx

> Cita

";

//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Preferences.h>
Preferences pref;

#include <SPI.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Setpoint and hysteresis values (in degrees Celsius)
float setpoint = 0;
float hysteresis = 0;
float currentTemp = 0;

#define ONE_WIRE_BUS 2 //D7  of esp8266
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


// define the GPIO connected with Relays
#define HEATING_PIN 0 //D5
#define COOLING_PIN 3 //D6
#define wifiLed   3   //D0

//Change the virtual pins according the rooms
#define VPIN_Text           V0
#define VPIN_Mode           V1
#define VPIN_currentTemp    V2
#define VPIN_setpoint       V3
#define VPIN_hysteresis     V4
#define VPIN_heaterbtn      V5
#define VPIN_coolerbtn      V6

// Relay and Mode State
bool heaterState = LOW; //Define integer to remember the toggle state for heater
bool coolerState = LOW; //Define integer to remember the toggle state for cooler
bool modeState = LOW; //Define integer to remember the mode

int wifiFlag = 0;


char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer;


// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_heaterbtn) {
  heaterState = param.asInt();
  digitalWrite(HEATING_PIN, !heaterState);
  pref.putBool("Heater", heaterState);
}

BLYNK_WRITE(VPIN_coolerbtn) {
  coolerState = param.asInt();
  digitalWrite(COOLING_PIN, !coolerState);
  pref.putBool("Cooler", coolerState);
}

BLYNK_WRITE(VPIN_Mode) {
  modeState = param.asInt();
  pref.putBool("Mode", modeState);
}

BLYNK_WRITE(VPIN_setpoint) {
  setpoint = param.asFloat();
  pref.putBool("setemp", setpoint);
}

BLYNK_WRITE(VPIN_hysteresis) {
  hysteresis = param.asFloat();
  pref.putBool("Hyst", hysteresis);
}

void checkBlynkStatus() { // called every 2 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    Serial.println("Blynk Not Connected");
    digitalWrite(wifiLed, HIGH);
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, LOW);
    Blynk.virtualWrite(VPIN_Text, "IoT Temperature Controller");
  }
}

BLYNK_CONNECTED() {
  // update the latest state to the server 
  Blynk.virtualWrite(VPIN_Text, "IoT Temperature Controller");
  Blynk.virtualWrite(VPIN_Mode, modeState);
  Blynk.syncVirtual(VPIN_currentTemp);
  Blynk.syncVirtual(VPIN_setpoint);
  Blynk.syncVirtual(VPIN_hysteresis);
  Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
  Blynk.virtualWrite(VPIN_coolerbtn, coolerState);

}

void readSensor() {

  // Send the command to get temperatures
  sensors.requestTemperatures();
  currentTemp = sensors.getTempCByIndex(0);
}

void sendSensor()
{
  readSensor();
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(VPIN_currentTemp, currentTemp);
  Blynk.virtualWrite(VPIN_Text, "IoT Temperature Controller");
}

void getRelayState()
{
  //Serial.println("reading data from NVS");
  modeState = pref.getBool("Mode", 0);
  Blynk.virtualWrite(VPIN_Mode, modeState);
  delay(200);
  heaterState = pref.getBool("Heater", 0);
  digitalWrite(HEATING_PIN, !heaterState);
  Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
  delay(200);
  coolerState = pref.getBool("Cooler", 0);
  digitalWrite(COOLING_PIN, !coolerState);
  Blynk.virtualWrite(VPIN_coolerbtn, coolerState);
  delay(200);
  setpoint = pref.getBool("setemp", 0);
  Blynk.virtualWrite(VPIN_setpoint, setpoint);
  delay(200);
  hysteresis = pref.getBool("Hyst", 0);
  Blynk.virtualWrite(VPIN_hysteresis, hysteresis);
  delay(200);
}

void setup()
{
  Serial.begin(115200);
  //Open namespace in read-write mode
  pref.begin("Relay_State", false);

  pinMode(HEATING_PIN, OUTPUT);
  pinMode(COOLING_PIN, OUTPUT);
  pinMode(wifiLed, OUTPUT);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(35, 0);
  display.println(" IoT ");
  display.setCursor(25, 20);
  display.println(" Temp. ");
  display.setCursor(0, 45);
  display.println("Controller");
  display.display();
  delay(2000); // Pause for 2 seconds
  
  //During Starting all Relays should TURN OFF
  digitalWrite(HEATING_PIN, !heaterState);
  digitalWrite(COOLING_PIN, !coolerState);

  sensors.begin();   // Enabling DS18B20 sensor
  digitalWrite(wifiLed, HIGH);

  //Blynk.begin(auth, ssid, pass);
  WiFi.begin(ssid, pass);
  timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  timer.setInterval(1000L, sendSensor); // Sending Sensor Data to Blynk Cloud every 1 second
  Blynk.config(auth);
  delay(1000);

  getRelayState(); // Get the last state of Relays and Set values of Temp & Hysteresis

  Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
  Blynk.virtualWrite(VPIN_coolerbtn, coolerState);
  Blynk.virtualWrite(VPIN_setpoint, setpoint);
  Blynk.virtualWrite(VPIN_hysteresis, hysteresis);
}

void loop()
{
  Blynk.run();
  timer.run();

  display.clearDisplay();
  display.setTextSize(1); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(" IoT Temp. Controller ");
  display.display();
  display.setTextSize(3); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 17);
  display.println(currentTemp);
  display.println(" ");
  display.drawRect(90, 17, 5, 5, WHITE);     // put degree symbol ( ° )
  display.setCursor(97, 17);
  display.println("C");
  display.display();

  Serial.println(hysteresis);
  display.setTextSize(1); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 57);
  display.print("Hys: ");
  display.print(hysteresis);
  display.display();
  Serial.println(setpoint);
  display.setTextSize(1); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(67, 57);
  display.print("Temp: ");
  display.print(setpoint);
  display.display();

  // Check the mode and control the heater and cooler accordingly
  if (modeState == 1) {
    display.setTextSize(1); // Draw 2X-scale text
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(32, 45);
    display.print("Auto Mode");
    display.display();
    // In auto mode, control the heater and cooler based on the current temperature and setpoint

    if (currentTemp > setpoint + hysteresis) {
      heaterState = 0;
      coolerState = 1;
      digitalWrite(HEATING_PIN, !heaterState);
      pref.putBool("Heater", heaterState);
      digitalWrite(COOLING_PIN, !coolerState);
      pref.putBool("Cooler", coolerState);
      Serial.println("Cooler ON");
      Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
      Blynk.virtualWrite(VPIN_coolerbtn, coolerState);

    } else if (currentTemp < setpoint - hysteresis) {
      heaterState = 1;
      coolerState = 0;
      digitalWrite(HEATING_PIN, !heaterState);
      pref.putBool("Heater", heaterState);
      Serial.println("Heater ON");
      digitalWrite(COOLING_PIN, !coolerState);
      pref.putBool("Cooler", coolerState);
      Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
      Blynk.virtualWrite(VPIN_coolerbtn, coolerState);
    } else {
      heaterState = 0;
      coolerState = 0;
      digitalWrite(HEATING_PIN, !heaterState);
      pref.putBool("Heater", heaterState);
      Serial.println("Heater OFF");
      digitalWrite(COOLING_PIN, !coolerState);
      pref.putBool("Cooler", coolerState);
      Serial.println("Cooler OFF");
      Blynk.virtualWrite(VPIN_heaterbtn, heaterState);
      Blynk.virtualWrite(VPIN_coolerbtn, coolerState);
    }
  }
  if (modeState == 0)
  {
    display.setTextSize(1); // Draw 2X-scale text
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(32, 45);
    display.print("Manual Mode");
    display.display();
  }

  delay(3000);
}

Usa la librería WiFiManager que tiene autoconexión.
Mira los ejemplos que trae la librería.

Hola. Voy a hacer la prueba y comento. Gracias.

Prueba a cambiar esto:

WiFi.begin(ssid, pass);

por esto (no probado)

do{
    WiFi.begin(ssid, pass); // conectar
    delay(500); //esperar 0,5 segs 
}while (WiFi.status() != WL_CONNECTED); // repite si no conectado

Saludos

Hola bosoft voy a probar a ver que hace. Gracias. Saludos.

Hola probe y no funciona. directamente no se conecta a internet. Gracias.

Si no te da error al compilar debe funcionar. Yo lo tengo en varios ESP porque el router tarda en iniciar y funciona
Podrías ver en el monitor serial lo que ocurre por si hay otro problema añadido.

Serial.print("Conectando");
do{
    WiFi.begin(ssid, pass); // conectar
    delay(500); //esperar 0,5 segs 
    Serial.print(".");
}while (WiFi.status() != WL_CONNECTED); // repite si no conectado

Una vez el router ya está a pleno rendimiento (que no solo wifi) debe conectar y terminar de arrancar.

Saludos

Hola, no da error cuando compilo. Voy a probar de vuelta. Gracias. Saludos.

Hola buenas!!! Probe de vuelta y no se conecta, lo puse tal cual esta ahi y no se conecta, ni siquiera pone algun dato en el puerto serie. Gracias.

Es imposible. Algo más tienes en el sistema que no funciona. Y me refiero a "sistema" al router, wifi, internet o incluso paciencia.
Pero si no indica nada en el monitor.... debería mostrar 'Conectando' como mínimo.
¿Cuándo arranca la primera vez si funciona?
¿Estás seguro de que has reemplazado la línea del setup()?
Muy raro todo.

Saludos

Me ha pasado lo mismo. Me ha ocurrido casualmente con los nodemcu o sea ESP8266-2E pero cuando probé otro código con micropython conectó inmediatamente, conclusión: o es la versión del core o es la versión de la libreria ESP8266Wifi.
El problema tmb se dió con el nivel de seguridad mas alto WPA2-PSK creo. Cuando bajé de seguridad conectó.
Intenta bajar versión de ESP8266Wifi.h a ver si cambia.
Ve a Github y busca si hay issues (problemas) resueltos del tema (están todos en inglés) pero algún tal vez te ayude.

Borre el wifi.begin y puse lo que vos escribiste. y ahi no conecta. si vuelvo todo como estaba conecta bien. Voy a revisar lo que comento surbyte. Muchas gracias a los dos.

Ahí hay algo más.
Si no te aparece en el monitor "conectando", es que el programa se bloquea antes de llegar ahí. Pon Serial.prints en el setup y mira donde se para o hasta donde llega.
En cuanto a la versión de librería ESP8266, llevo desde hace más de un año en la 3.1.2 y no he tenido problemas de conexión.
Saludos

Voy a hacer esa prueba y comento. Gracias. Saludos.

Repito, si alguien hace una prueba es notable la diferncia de conexion entre micropython y arduino.
MicroPython es instantáneo y el codigo en C++ esta muchos segundos en algunos casos según el nivel de seguridad (aclaro) para hacerlo.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.