Post topics, source code that relate to the Arduino Platform

User avatar
By johnlerrington
#89291 I have a Lolin NodeMCU set up to repeatedly ping devices on my local network, and on the web.
It uses the ESP8266WiFi library.

Until there is a disconnect it works fine; however I cant see how to get it to reconnect.
Full project is described here

http://www.skillbank.co.uk/arduino/pinglogger.htm

and the relevant bits of the sketch -
Code: Select all#include <ESP8266WiFi.h>            //WiFi functions
...
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
...


and in setup()

Code: Select all //monitor Wifi connection and handle D0

  gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP & event)
  {
    Serial.print("Station connected, IP: ");
    Serial.println(WiFi.localIP());
    digitalWrite(dPin[1], 0); //blue LED
  });

  disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected & event)
  {
    Serial.println("Station disconnected");
    digitalWrite(dPin[1], 1); //blue LED
  });

  bool stationConnected = WiFi.begin(ssid, pass);
  delay(200); //allow time to connect
  // Wait until connection completed
  Serial.print("Connecting to AP...");
  while (WiFi.status() != WL_CONNECTED)  {
    delay(200);
    Serial.print(".");
  }
  Serial.println("Ok\n");
  digitalWrite(dPin[0], 0); //indicate wifi connected -  white LED


I feel I should be doing a reconnect inside the "disconnectedEventHandler" - or inside my loop() but not sure exactly how. Full sketch attached for context