Disconnect and reconnect ESP8266

I have an issue with a NodeMCU esp8266, that is connected a shitty access point (sorry for the french),

The access point kicks clients without any apparent reason (even when Im connected from my phone, I see how it losses connection and connects again)

What I thought is to add a routine and disable thinger.io, by the thinger.stop() command, I thougth that will stop wifi and start it again, by the thinger.handle() command, and it will try to connect it again to network, but it doesn’t.

Someone knows how to disconnect and try to reconnect this device to a wifi network, and of course, to thinger platform without restarting it?

1 Like

Hi @ega,

the stop will disconnect the device from the network. If you do not call thing.handle again, it should not connect to the network. So, please, be sure you do not call thing.handle after the thing.stop, until you are ready to connect again.

Thanks for your answer

I’m avoiding to call thing.handle() when thing.stop() is called, Im stoping thinger 20 seconds every 10 minutes, my code is something like:

if (comm)     //all "thing.handle()" call is in one if like this
{
thing.handle();
}

if (10min && comm) //  "10min" is kind of timer, every 10 mins this routine runs
{
thing.stop();
comm=0;
}

if (20sec && !comm) // 20sec after 10mins previous routine is called
{
comm=1;
}

I was reading about to turn off wifi in esp8266, with lightsleep, so the microcontroler will turn off wifi but it keeps running the process

I will dig a little bit more on this, if I achieve something will post here

Thanks

I’m facing a somehow similar problem with an Arduino Yun.
I have the Yun using my celular as access point in order to connect to Thinger.
When for some reason I disconnect the cell obviously the connection to Thinger drops because the Yun is not connected to the internet anymore.
The problem is that the connection to Thinger is not reestablished even when the cell is reconnected and the Yun is up again (unless I perform a hard reset on the YUN)
Shouldn’t Thinger reconnect as well without the need of a hard reset?

Maybe I was doing something wrong, I did the code again and its working with no issues, even I turn off the wifi router and after the timer cycle runs, the device connects again to wifi and internet.

Thanks

Here my code, when there is no access internet, it start a mode and sensor still working.
And a commented some of code in the libraries, u could look serial monitor to see what u have to comment

  void loop() {
  unsigned long currentMillis = millis();
  if ((unsigned long) (currentMillis - previousMillis1) >= interval1)
  {
    WifiConnect();
    previousMillis1 = millis();
  }
 
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis2) >= interval2)
  {
    automatico();
    previousMillis2 = millis();
  }
  else {
    thing.handle();
  }


  
}

void SelectMode() {
  switch (SelectModo)
  {
    case false:
      automatico();
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }
      digitalWrite(LedModoAuto, ledState);

      break;
    case true:
      digitalWrite(LedModoAuto, HIGH);
      analogWrite(LedStrip, 100 - (brightness * 1));

      switch (brightness)
      {
        case 0:
          analogWrite(LedStrip, 100);
          break;
        case 1:
          analogWrite(LedStrip , 0);
          break;
        case 2:
          break;
      }
      break;
    case 2:
      break;
  }
}

int contador;
void automatico() {

  SensorState = digitalRead(PirSensor);

  Serial.print("SensorState:");
  Serial.println(SensorState);

  LDRVal = analogRead(A0);


  TimeFade = contador;
  SPLDR = stPointLDR * 10;

  Serial.print("Sensor Luz:");
  Serial.println(LDRVal);

  Serial.print("Conta:");
  Serial.println(contador);

  if (LDRVal >= SPLDR) {
    setFocoLed(0);
  } else if (contador < TimeMax && SensorState != HIGH) {
    setFocoLed((float)(255 - (float)(contador * TimeMul)));
    contador++;
  } else if (SensorState == HIGH) {
    setFocoLed(255);
    contador = 1;
  } else {
    setFocoLed(LOW);
  }
}

void setFocoLed(float porcentaje) {
  float intensidad = (porcentaje / (float)255) * (float) 100;
  float value = 100 - intensidad;
  analogWrite(LedStrip, value);
}

void WifiConnect() {
  if  (WiFi.status() == WL_CONNECTED) {
    SelectMode();
  } 
}> Blockquote
1 Like