-->
Page 1 of 2

Autoconnect wlan

PostPosted: Mon Mar 20, 2017 1:09 am
by chq
Hi everyone,

i'd like to programm an ESP-12E so that it autoconnects to a specific SSID if the connection breaks.

I made a code in Arduinolanguage (see below), but sometimes the connection breaks and only if i plug off the ESP and power again it wants to connect.

May someone help, please?

Greetz Chris

Code: Select all//  Optinonale Amica ESP-12E erweitern die Zentrale um PIR-Funktionalität in weiteren Räumen.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

// #define DEBUG Serial // Debugging

#ifdef DEBUG
#define debug(...) DEBUG.print(__VA_ARGS__)
#define debugln(...) DEBUG.println(__VA_ARGS__)
#define debugbegin(...) DEBUG.begin(__VA_ARGS__)
#else
#define debug(...)
#define debugln(...)
#define debugbegin(...)
#endif

// PIR-Sensor

const byte pirSensor = D1;   // PIR-Sensor an D1

// LED

unsigned long ledTime;  // LED-Einschaltzeit

// Netzwerk

WiFiUDP udp;
const char* ssid = "wiiii";
const char* password = "fire536";
unsigned int localUdpPort = 8888;

IPAddress allIP(192, 168, 0, 255); // IP-VerteilAdresse (alle Geräte erhalten alle UDP-Nachrichten)
const unsigned int allPort = 8888; // Port, auf dem alle angeschlossenen Komponenten miteinander kommunizieren

void setup()
{
  debugbegin(9600);
  debugln(LED_BUILTIN);

  pinMode(pirSensor, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    debug(".");
  }

  debugln("");
  debugln("WiFi connected");
  debugln("IP address: ");
  debugln(WiFi.localIP());
  udp.begin(localUdpPort);
}

void loop()
{
  if (pirCheck())
  {
    udpSend();
  }
  if (millis() - ledTime >= 100) digitalWrite(LED_BUILTIN, HIGH); // LED ausschalten
}

boolean pirCheck()
{
  static boolean old_pir_state = 1;       // Zuletzt ermittelter Zustand
  static boolean new_pir_state;           // Aktuell ermittelter Zustand

  new_pir_state = digitalRead(pirSensor);
  if (new_pir_state != old_pir_state)                  // Wenn PIR_Zustandswechsel..
  {
    old_pir_state = new_pir_state;
    return new_pir_state;
  }
  else return 0;
}

void udpSend()
{
  udp.beginPacket(allIP, allPort);
  udp.write("alarm");
  udp.endPacket();
  ledTime = millis();
  debugln("alarm");
  digitalWrite(LED_BUILTIN, LOW);
}

Re: Autoconnect wlan

PostPosted: Wed Mar 22, 2017 2:37 pm
by spike7638
Broadly speaking, your code says this:

Code: Select allvoid setup()
{
 ... do housekeeping...
... connect to Wifi ...
}

void loop()
{
  if  ... there's data ...
  {
    udpSend();
  }
  ...sometimes blink the LED
}


If you lose your connection to the Wifi, you're never going to know it. So you never get a chance to reconnect, etc.

You might want to do something (within loop() )where every so often you check that there's still a connection, something like this (untested, included just to show the gist)
Code: Select all  if (millis() - wifiCheckTime >= 100) {
     if (checkWiFiLoss() ){  // RETURNS "true" if the wifi is dead
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED)
        {
           delay(500);
           debug(".");
        }

      debugln("");
      debugln("WiFi connected");
       debugln("IP address: ");
      debugln(WiFi.localIP());
      udp.begin(localUdpPort);
  }


What does "checkWiFiLoss" look like? You might broadcast a UDP packet on localUdpPort, and then see whether anything is available on that port within, say, 10 msec or so; if not, then the connection has died, and you return "true". If so...then return "false".

Does this make sense?

The only place where you're making the WifiCOnnection right now is in "setup()", and "setup()" only gets called during startup (e.g., power-on).

Re: Autoconnect wlan

PostPosted: Thu Mar 23, 2017 2:55 am
by chq
Hi,

thank you for your pseudocode. Instead of broadcasting a UDP packet on localUdpPort and checking the incoming packets again, is there a more easier possibility? Can't believe, that i am the only one with this requirement.

Greetz Chris

Re: Autoconnect wlan

PostPosted: Thu Mar 23, 2017 12:37 pm
by spike7638
You want to know whether you're connected and can use UDP, right?

If not, that failure could be
    * your device (broken xmitter? low voltage? ...),
    * the wireless AP you're talking to (unplugged? overburdened by five people all streaming HD video? Broken firmware? Hacked? Plain old software bug manifesting itself after 2 days of operation?...), or
    * something else (cosmic radiation interfering with radio? Someone's moved a large slab of iron between you and the AP? ...)

In all cases, the thing you want to know is "does this whole thing still work"? So sending a message and seeing that you get it back again...that's a pretty good test. If it works, you can have some confidence that you can again send a message and get it back (or have some other entity get it).

There might be a way to "ping" .. but that only tests the lower layers of the network (see https://wiki.itadmins.net/network/tcp_udp_ping). If you really want to know if UDP is working, it's not a bad idea to check that UDP is working, at least to the degree observable from your computer.

I'm sure someone more expert in networking can explain why this is wrong ... but it works OK in practice. :)