Autoconnect wlan
Posted: Mon Mar 20, 2017 1:09 am
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
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);
}